Express.js路由器“catchall”加载索引与任何url参数
我有一个基本的项目与我的根用这个代码中的server.js设置:
app.use('/', express.static(__dirname + '/public/'));
此外,我有一个公共文件夹与该目录中的index.html,样式和脚本文件夹。
我希望我的web应用程序发送用户到index.html,无论他们可能有任何url参数。 例如:如果用户试图去localhost:8888 / notarealpage,它仍然加载index.html(没有redirect),所以我仍然可以引用location.href属性中的'notarealpage'。
您可以使用:
app.get('*', function (req, res) { res.sendFile(path.resolve(__dirname, 'public/index.html')); });
这样,它会发送你的index.html不pipeurl。
请注意,您可能需要微调sendFile
参数。
添加
app.get('*', function (req, res) { res.sendFile((__dirname + '/public/index.html')); });
到server.js我的路线结束了诀窍。