在Nodejs
中创建简单的Web服务器时,我遇到了一个奇怪的问题。 http服务器运行正常,接受请求和响应。但是,出于某种原因,它总是希望为所有内容发送content-type:
text/plain
。例如,.js
和.css
文件总是降为text/plain
,而它们通常应以text/css
或application/javascript
的形式发送。 Chrome,我用来测试它的浏览器,总是抱怨资源的MIME类型:
Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://localhost:3000/test.css".
Resource interpreted as Script but transferred with MIME type text/plain: "http://localhost:3000/test-client.js".
这最终意味着css
永远不会应用于页面。我添加了一些日志记录,似乎http响应正在发送正确的MIME类型。
我已经创建了我正在做的准系统版本。希望有人可以指出我编码的缺陷:
test.js
var http = require('http'),
fs = require('fs'),
url = require('url'),
path = require('path');
var contentTypes = {
'.html': 'text/html',
'.css': "text/css",
'.js': 'application/javascript'
};
http.createServer(function(request, response) {
// get file based on pathname
var uri = url.parse(request.url).pathname,
filename = path.join(__dirname, uri);
fs.exists(filename, function(exists) {
// if root directory, append test.html
if (fs.statSync(filename).isDirectory()) {
filename += 'test.html';
}
// figure out MIME type by file ext
var contentType = contentTypes[path.extname(filename)];
fs.readFile(filename, function(err, file) {
// errors?
if (err) {
response.writeHead(404, {'Content-type:': 'text/plain'});
response.write(err + "\n");
response.end();
} else {
console.log('MIME TYPE for: ', filename , contentType);
response.setHeader('Content-Type:', contentType);
response.writeHead(200);
response.write(file);
response.end();
}
});
});
}).listen(3000, function(){
console.log("server started and listening on port 3000");
});
的test.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="test.css" type="text/css" />
</head>
<body>
<h1>Test</h1>
<div id="test"></div>
<script type="text/javascript" src="test-client.js"></script>
</body>
</html>
test.css
h1 {
color: red;
}
测试client.js
var div = document.getElementById('test');
div.innerHTML = 'test client ran successfully';
答案 0 :(得分:4)
我认为问题是您在设置标头后:
之后使用了不必要的Content-Type
。你应该做response.setHeader('Content-Type',contentType);
或者我认为更好的做法:response.writeHead(200,{'Content-Type':contentType});
答案 1 :(得分:0)
并且你必须将fs.readFile
包装成一个闭包,否则一些文件(特别是最后一个文件)将被多次读取,而其他文件则根本不被读取。 contentType
将不会按您的意愿设置。这是因为fs.readFile
使用的回调策略。当html文件只加载一个外部文件时,问题不会出现,但是当外部文件(css,js,png)加载多个时,它将显示为我上面指出的。 (我无法使用我的Gmail登录,因此我以访客身份发帖)
所以你的代码应该做如下改动:
;(function (filename, contentType) {
fs.readFile(filename, function(err, file) {
// do the left stuff here
});
}(filename, contentType));
答案 2 :(得分:0)
我遇到了同样的问题,但我的问题略有不同。我最初使用这种方法来设置 Content_Type
:
res.writeHead(200, { 'Content_Type': contentType });
用以下内容替换后,错误消失了:
res.setHeader('Content-Type',contentType)