从Node.js服务器向浏览器发送脚本时,在Google Chrome浏览器中,出现以下警告:
资源被解释为脚本,但以MIME类型文本/纯文本传输
我用Google搜索了一下,发现这是服务器端的问题,也就是说,我认为在发送事物之前,我应该为事物设置正确的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类型,而不是“设置”它。)