我使用快递服务器服务我的Angular应用程序。为什么表示在请求app.css时返回index.html?
var express = require('express');
var server = express();
server.use(express.static('./app'));
server.all('*', function(req, res) {
res.sendFile('index.html', { root: './app' });
});
server.listen(8000);
当我导航到http://localhost:8000
时,我的Angular应用程序重定向到http://localhost:8000/home
,并且app.css
被正确地服务(我在响应中获得了CSS)。
但是,如果我刷新页面(http://localhost:8000/home
),则app.css
的响应为index.html
。
为什么会发生这种情况,您将如何解决这个问题?
您是否正在观看网络流量,并看到CSS在第一页上正确拉出,并且不是来自缓存?这里的代码看起来像这样说:“对于任何文件的任何请求,sendFile index.html。你不应该传递请求文件名而不是常量字符串吗? – EricLaw 2014-09-27 12:12:49
你的index.html文件的代码是什么,特别是包含css文件的部分?你应该使用绝对路径,而不是相对的。 – 2014-09-27 13:10:07