2014-09-27 30 views
0

我使用快递服务器服务我的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); 

Full gulpfile.js is here

当我导航到http://localhost:8000时,我的Angular应用程序重定向到http://localhost:8000/home,并且app.css被正确地服务(我在响应中获得了CSS)。

但是,如果我刷新页面(http://localhost:8000/home),则app.css的响应为index.html

为什么会发生这种情况,您将如何解决这个问题?

DEMO HERE

+0

您是否正在观看网络流量,并看到CSS在第一页上正确拉出,并且不是来自缓存?这里的代码看起来像这样说:“对于任何文件的任何请求,sendFile index.html。你不应该传递请求文件名而不是常量字符串吗? – EricLaw 2014-09-27 12:12:49

+1

你的index.html文件的代码是什么,特别是包含css文件的部分?你应该使用绝对路径,而不是相对的。 – 2014-09-27 13:10:07

回答

1

您需要使用绝对路径。

<link rel="stylesheet" type="text/css" href="/app.css"> 

否则从/ home表示app.css的请求将是home/app.css而不是/app.css。

+0

谢谢,这解决了问题! – 2014-09-27 22:12:11

相关问题