Node.js简单服务器请求结束事件问题

我是node.js的新手 尝试让请求结束时打印控制台。 我尝试去localhost:8080和localhost:8080 /但没有在terminal打印。 任何想法为什么? 这样做是因为当我运行这个例子,因为当我尝试在http://tutorialzine.com/2012/08/nodejs-drawing-game/运行演示terminal说,套接字启动,但它不呈现index.html页面。 所以我无法弄清楚为什么这个代码为其他静态文件服务没有为我工作。

var static = require('node-static'); // // Create a node-static server instance to serve the './public' folder // // var file = new(static.Server)('./'); require('http').createServer(function (request, response) { request.addListener('end', function () { console.log("ended"); }); }).listen(8080); 

看来你正在使用Node.js 0.10.x,在新版本中,你必须恢复可读stream,使它们发出事件:

 require('http').createServer(function (request, response) { var body = ''; request.setEncoding('utf8'); request.on('readable', function () { body+= this.read(); } request.on('end', function () { console.log('ended'); console.log('Body: ' + body); }); request.resume(); }).listen(8080); 

您应该在请求处理程序中调用node-static服务,以便您可以获取index.html

 var static = require('node-static'); var fileServer = new static.Server('./'); require('http').createServer(function (request, response) { fileServer.serve(request, response); //add this line request.addListener('end', function () { console.log("ended"); }); }).listen(8080);