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

Ben*_*eib 4 node.js node-static

我是node.js的新手.尝试在请求结束时让控制台进行打印.我尝试去localhost:8080和localhost:8080 /但终端没有打印.知道为什么吗?这样做是因为当我运行这个例子时,因为当我尝试在http://tutorialzine.com/2012/08/nodejs-drawing-game/上运行演示时,终端说套接字已启动,但它没有呈现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);
Run Code Online (Sandbox Code Playgroud)

mic*_*nic 7

您似乎正在使用Node.js 0.10.x,并且在新版本中,您必须恢复可读流以使它们发出事件:

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);
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说很有把握,我的系统升级到了最新的node.js版本,然后我的node.js应用程序突然出现了问题.:/ (2认同)