我是一个完整的节点新手,我遇到了一个问题:
var http = require("http");
var url = require('url');
var fs = require('fs');
var io = require('socket.io');
var server = http.createServer(function(request, response){
console.log('Connection');
var path = url.parse(request.url).pathname;
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('Hello World');
break;
case '/socket.html':
fs.readFile(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("This doesn't exist - 404");
console.log(error);
} else {
console.log('hi!'); // **************<---- I'm getting here
response.writeHead(404, {'Content-Type': 'text/html'});
response.write(data, 'utf8');
}
});
break;
default:
response.writeHead(404);
response.write("This page does not exist - 404");
console.log(path);
break;
}
response.end();
});
server.listen(8001);
io.listen(server);
我正在进入'else'中的console.log(),但response.write根本不输出任何东西,如果我console.log(data)我得到