我花了很长时间在我的应用程序上混淆了为什么错误的index.html文件显示,我找到了解决方案,但我不明白为什么。
我有两个html文件:
一个在public
目录中,另一个在dist
目录中
在我的Express路线中,我最初写道:
app.use(express.static(path.join(__dirname, 'public')));
/* ...some express routes... */
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
我以为我可以静态服务public
目录中的文件(如css和图像),但是当没有指定其他路由时,我可以在index.html
中加载dist
文件。 / p>
但是当我运行我的应用时,我会看到来自index.thml
的{{1}}文件。这不是我想要的。所以我改变了#34; public"的顶线。到" dist"像这样:
public
现在我的应用加载了正确的index.html。
这里发生了什么? Express是否默认为" public"即使我列出了" dist"在res.sendFile?为什么这是解决方案?
感谢您帮助我理解!