简体   繁体   English

我在node.js中的http.createserver不起作用?

[英]My http.createserver in node.js doesn't work?

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? 大家好我刚刚开始学习node.js并在网上搜索了很多东西,然后尝试在node.js中编码我使用这两个代码向我显示相同的结果,但最后一个是在我的浏览器上显示错误喜欢“无法找到页面”的东西。所以请向我解释原因?

// 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/');

This is working but 这是有效的

// 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");

This one is not working 这个不起作用

Why? 为什么?

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. 最后一个不起作用的链接http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/感谢您的所有答案,但我仍然不明白这些问题。 the last one that is not working just has request.on? 最后一个不起作用的是request.on?

request is an instance of http.IncomingMessage , which implements the stream.Readable interface. requesthttp.IncomingMessage一个实例,它实现了stream.Readable接口。

Documentation at http://nodejs.org/api/stream.html#stream_event_end says: http://nodejs.org/api/stream.html#stream_event_end上的文档说:

Event: 'end' 事件:'结束'

This event fires when no more data will be provided. 如果不再提供数据,则会触发此事件。

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. 这可以通过切换到流动模式,或通过重复调用read()直到结束来完成。

 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.'); }); 

So in your case, because you don't use either read() or subscribe to the data event, the end event will never fire. 因此,在您的情况下,因为您不使用read()或订阅data事件, end事件将永远不会触发。

Adding 添加

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

within the event listener would probably make the code work. 在事件监听器内可能会使代码工作。

Note that using the request object as a stream is only necessary for when the HTTP request has a body. 请注意,仅在HTTP请求具有正文时才需要将请求对象用作流。 Eg for PUT and POST requests. 例如,PUT和POST请求。 Otherwise you can consider the request to have finished already, and just send out the data. 否则,您可以考虑已经完成的请求,并发送数据。

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. 如果您的发布的代码是从其他网站直接获取的,则可能是此代码示例基于Node 0.8。 In Node 0.10, there have been changes in how streams work. 在节点0.10中,流的工作方式发生了变化。

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

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'. 警告:如果您从未添加“数据”事件处理程序或调用resume(),那么它将永远处于暂停状态并且永远不会发出“结束”。 So the code you posted would have worked on Node 0.8.x, but does not in Node 0.10.x. 因此,您发布的代码将在Node 0.8.x上运行,但不在Node 0.10.x中。

The function you are applying to the HTTP server is the requestListener which supplies two arguments, request , and response , which are respectively instances of http.IncomingMessage and http.ServerResponse . 您应用于HTTP服务器的函数是requestListener ,它提供两个参数, requestresponse ,它们分别是http.IncomingMessagehttp.ServerResponse实例。

The class http.IncomingMessage inherits the end event from the underlying readable stream. http.IncomingMessage从底层可读流继承end事件。 The readable stream is not in flowing mode, so the end event never fires, therefore causing the response to never be written. 可读流不处于流动模式,因此结束事件永远不会触发,因此永远不会写入响应。 Since the response is already writable when the request handler is run, you can just directly write the response. 由于响应在运行请求处理程序时已经可写,因此您可以直接写入响应。

var http = require('http');

http.createServer(function(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/plain'
  });
  res.end('Hello HTTP!');
}).listen();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM