2014-09-12 45 views
0

好吧,我想出了一個解決方案,但我想知道是否有人能幫助我理解爲什麼發生這種情況。ExpressJS 4.0背景圖像CSS Mime類型設置爲文本

我app.js有:

app.use(express.static(__dirname + '/public')); 

和預期靜態文件服務。

除此之外,當我試圖在我的CSS添加背景圖片:

html { 
    background: url(img/tree.jpg) no-repeat center center fixed; 
    -webkit-background-size: cover; 
    -moz-background-size: cover; 
    -o-background-size: cover; 
    background-size: cover; 
} 

我有錯誤:

Resource interpreted as Image but transferred with MIME type text/html: "http://localhost:3000/css/img/tree.jpg". localhost/:1 

現在,當我更改CSS來 /IMG /樹。 jpg

該文件服務正確 - 所以這裏是一個問題 - 在/css/img/tree.jpg不應該有任何文件,所以爲什麼服務器認爲圖像是與文本標頭髮送inst剛回來的資源沒有找到?

謝謝你的幫助!

回答

0

好吧,明白了這一點。

我有另一個路由設置爲服務角度頁面,以消耗我的REST API。

app.get('*', function(request, response) { 
     var options = { 
      root: __dirname + '/public/' 
    } 
    response.sendFile('./views/index.html', options); 
}); 

這意味着,當爲背景圖像的CSS文件中設置的路徑錯誤我認爲,快遞只是一往直前,並試圖滿足了index.html文件。這意味着CSS頁面說文件是一個圖像,但服務器說這個文件是html。這就是爲什麼標題出現錯誤,並且應用程序嘗試加載圖像時不僅存在斷開的鏈接。