我在 node.js 中的 http.createserver 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19363439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
My http.createserver in node.js doesn't work?
提问by Sarin Suriyakoon
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. the last one that is not working just has request.on?
最后一个无效的链接 http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/感谢您的所有答案,但我仍然不明白这些问题. 最后一个不起作用的只是 request.on?
采纳答案by Myrne Stol
requestis an instance of http.IncomingMessage, which implements the stream.Readableinterface.
request是 的一个实例http.IncomingMessage,它实现了stream.Readable接口。
Documentation at http://nodejs.org/api/stream.html#stream_event_endsays:
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.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()重复调用直到结束。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 dataevent, the endevent 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. E.g. for PUT and POST requests. Otherwise you can consider the request to have finished already, and just send out the data.
请注意,仅当 HTTP 请求具有主体时才需要将请求对象用作流。例如对于 PUT 和 POST 请求。否则,您可以认为请求已经完成,只需将数据发送出去。
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.
如果您发布的代码实际上是从其他站点获取的,则此代码示例可能基于 Node 0.8。在 Node 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'. So the code you posted would have worked on Node 0.8.x, but does not in Node 0.10.x.
警告:如果您从不添加“数据”事件处理程序或调用 resume(),那么它将永远处于暂停状态并且永远不会发出“结束”。因此,您发布的代码可以在 Node 0.8.x 上运行,但在 Node 0.10.x 中不起作用。
回答by hexacyanide
The function you are applying to the HTTP server is the requestListenerwhich supplies two arguments, request, and response, which are respectively instances of http.IncomingMessageand http.ServerResponse.
您应用于 HTTP 服务器的函数是 ,requestListener它提供两个参数request、 和response,它们分别是http.IncomingMessage和 的实例http.ServerResponse。
The class http.IncomingMessageinherits the endevent from the underlying readable stream. 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.
该类从底层可读流http.IncomingMessage继承end事件。可读流不处于流动模式,因此永远不会触发结束事件,从而导致永远不会写入响应。由于在请求处理程序运行时响应已经是可写的,因此您可以直接编写响应。
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Hello HTTP!');
}).listen();

