我从http模块的非常简单的nodejs response.write上得到了一些恼人的错误。 response.write不输出参数。请帮帮我!
var server = http.createServer(function(request, response){
console.log("Connection");
var pathname = url.parse(request.url).pathname;
switch(pathname) {
case '/':
response.writeHead(200, {"Content-Type": "text/html"});
response.write("hello world");
break;
case '/socket.html':
fs.readFile(__dirname + pathname, function(error, data) {
if(error) {
response.writeHead(404);
response.write("Oops this doesn't exist - socket.html");
}else {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data, 'utf8');
}
});
break;
default :
response.writeHead(404);
response.write("Oops this doesn't exist - default");
break;
}
response.end();
上面的代码块是我的server.js文件的一部分。
答案 0 :(得分:1)
你应该使用response.send(msg)而不是response.write(),因为response.write本身需要以response.end()结束。所以基本上,
Response.send("something");
或
Response.write("something");
Response.end();
每个人都没事。