我在node.js中的http.createserver无法正常工作?

问题描述:

大家好,我今天才开始学习node.js,并在互联网上搜索了很多东西,然后尝试在node.js中进行编码,我用这两个代码向我展示了相同的结果,但最后一个是显示错误在我的浏览器上,诸如找不到页面"之类的东西.所以请向我解释原因?

Hello guys i just started learning node.js today and search a lot off stuff on the internet , then try to code in node.js i use these two codes to show me the same result but the last one is show the error on my browser something likes "can not find the page".So please explain to me why?

// JScript source code
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');

这是可行的,但

// Include http module.
var http = require("http");

// Create the server. Function passed as parameter is called on every request made.
// request variable holds all request parameters
// response variable allows you to do anything with response sent to the client.
http.createServer(function (request, response) {
   // Attach listener on end event.
   // This event is called when client sent all data and is waiting for response.
   request.on("end", function () {
      // Write headers to the response.
      // 200 is HTTP status code (this one means success)
      // Second parameter holds header fields in object
      // We are sending plain text, so Content-Type should be text/plain
      response.writeHead(200, {
         'Content-Type': 'text/plain'
      });
      // Send data and end response.
      response.end('Hello HTTP!');
   });

}).listen(1337, "127.0.0.1");

这是行不通的

为什么?

最后一个无效的链接 http://net.tutsplus.com/tutorials/javascript-ajax/node-js-对于初学者/感谢您提供所有答案,但是我仍然不了解这些问题.最后一个不工作的只是request.on?

The link of the last one that's not working http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/ Thank you for all the answers, but i still don't understand about the problems. the last one that is not working just has request.on?

request http.IncomingMessage 的实例,该实例实现了 stream.Readable 界面.

request is an instance of http.IncomingMessage, which implements the stream.Readable interface.

文档位于 http://nodejs.org/api/stream.html#stream_event_end说:

事件:结束"

当不再提供更多数据时将触发此事件.

Event: 'end'

This event fires when no more data will be provided.

请注意,除非完全消耗掉数据,否则不会触发end事件.可以通过切换到流动模式或重复调用 read()直到结束来完成此操作.

Note that the end event will not fire unless the data is completely consumed. This can be done by switching into flowing mode, or by calling read() repeatedly until you get to the end.

var readable = getReadableStreamSomehow();
readable.on('data', function(chunk) {
  console.log('got %d bytes of data', chunk.length);
})
readable.on('end', function() {
  console.log('there will be no more data.');
});

因此,在您的情况下,因为您既不使用 read()也不订阅 data 事件,则 end 事件将永远不要开火.

So in your case, because you don't use either read() or subscribe to the data event, the end event will never fire.

添加

 request.on("data",function() {}) // a noop

在事件监听器中可能会使代码正常工作.

within the event listener would probably make the code work.

请注意,仅当HTTP请求具有正文时,才需要将请求对象用作流.例如.用于PUT和POST请求.否则,您可以认为请求已完成,然后发送数据即可.

Note that using the request object as a stream is only necessary for when the HTTP request has a body. E.g. for PUT and POST requests. Otherwise you can consider the request to have finished already, and just send out the data.

如果您发布的代码是从其他站点直接获取的,则该代码示例可能基于Node 0.8.在节点0.10中,流的工作方式发生了变化.

If the code your posting is taken literally from some other site, it may be that this code example was based on Node 0.8. In Node 0.10, there have been changes in how streams work.

来自 http://blog.nodejs.org/2012/12/20/stream2/

警告:如果您从不添加数据"事件处理程序或调用resume(),则它将永远处于暂停状态,并且永远不会发出结束"信号.因此,您发布的代码可以在Node 0.8.x上运行,但不能在Node 0.10.x上运行.

WARNING: If you never add a 'data' event handler, or call resume(), then it'll sit in a paused state forever and never emit 'end'. So the code you posted would have worked on Node 0.8.x, but does not in Node 0.10.x.