我是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);
解决方法
您似乎正在使用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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。