我在node.js. WriteHead()
函数中的新功能不断给我typeError。虽然我使用了response.wrtieHead(200,{'Context-Type':'text/plain'})
并给出了相同的错误。请帮忙。
这是我的代码。
var http = require('http');
var fs = require('fs');
function send404Response(response){
response.writeHead(200,{"content-Type":"text/plain"});
response.write("404 paage");
response.end();
}
function onRequest(request,response){
if(response.method== 'GET' && response.url('/') ){
response.writeHead(200, {"content-Type":"text/html"});
fs.createReadStream("./index.html").pipe(response);
}
else{
send404Response();
}
}
http.createServer(onRequest).listen(8888);
console.log("server is running");
答案 0 :(得分:1)
我想您忘记在send404Response函数中发送回复:
var http = require('http');
var fs = require('fs');
function send404Response(response){
response.writeHead(200,{"content-Type":"text/plain"});
response.write("404 paage");
response.end();
}
function onRequest(request,response){
if (request.method== 'GET' && request.path == '/') {
response.writeHead(200, {"content-Type":"text/html"});
fs.createReadStream("./index.html").pipe(response);
}
else{
send404Response(response);
}
}
http.createServer(onRequest).listen(8888);
console.log("server is running");
希望它有所帮助!