2014-09-27 90 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

相關問題