繁体   English   中英

具有任何url参数的Express.js路由器“ catchall”加载索引

[英]Express.js Router “catchall” load index with any url param

我使用以下代码在根目录中使用server.js设置了一个基本项目:

app.use('/', express.static(__dirname + '/public/'));

另外,我有一个公用文件夹,该目录根目录中有index.html,styles和scripts文件夹。

我希望我的Web应用程序可以将用户发送到index.html,而不管他们可能具有任何url参数。 例如:如果用户尝试转到localhost:8888 / notarealpage,它仍会加载index.html(不带重定向),因此我仍然可以在location.href属性中引用“ notarealpage”。

您可以使用:

app.get('*', function (req, res) {
    res.sendFile(path.resolve(__dirname, 'public/index.html'));
});

这样,无论网址如何,它都会发送您的index.html。

请注意,您可能必须微调sendFile参数。

添加

app.get('*', function (req, res) {
    res.sendFile((__dirname + '/public/index.html'));
});

到server.js中我的路由的末尾就达到了目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM