當發送從我的Node.js服務器腳本的瀏覽器,谷歌瀏覽器,我得到這樣的警告:如何在Node.js中發送文件之前設置MIME類型?
資源解釋爲腳本,但MIME類型 文本傳輸/純
我谷歌周圍,發現這是一個服務器端問題,即我認爲我應該設置正確的MIME類型的東西,然後再發送它們。這裏的HTTP服務器的處理程序:
var handler = function(req, res)
{
url = convertURL(req.url); //I implemented "virtual directories", ignore this.
if (okURL(url)) //If it isn't forbidden (e.g. forbidden/passwd.txt)
{
fs.readFile (url, function(err, data)
{
if (err)
{
res.writeHead(404);
return res.end("File not found.");
}
//I think that I need something here.
res.writeHead(200);
res.end(data);
});
}
else //The user is requesting an out-of-bounds file.
{
res.writeHead(403);
return res.end("Forbidden.");
}
}
問:如何糾正我的服務器端代碼,以正確配置MIME類型?
(注:我已經找到https://github.com/broofa/node-mime,但它只是讓我確定MIME類型,而不是 「套」 了。)
是的,純節點,沒有表達。 – corazza 2012-08-15 15:23:41
好吧,唯一缺少的就是你找出腳本的MIME類型,並將它彈出到每個響應的正確標題中。 – rdrey 2012-08-15 15:27:46
做到了,看到我的答案。 :) – corazza 2012-08-15 16:13:08