最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node JS, createServer, and the Event Loop - Stack Overflow

programmeradmin0浏览0评论

Behind the scenes in node, how does the http module's createServer method (and its callback) interact with the event loop? Is it possible to build functionality similar to createServer on my own in userland, or would this require a change to node's underlying system code?

That is, my general understanding of node's event loop is

  1. Event loop ticks
  2. Node looks for any callbacks to run
  3. Node runs those callbacks
  4. Event loops ticks again, process repeats ad-infinitum

What I'm still a little fuzzy on is how createServer fits into the event loop. If I do something like this

var http = require('http');

// create an http server  and handle with a simple hello world message
var server = http.createServer(function (request, response) {
    //...
});

I'm telling node to run my callback whenever an HTTP request es in. That doesn't seem patible with the event loop model I understand. It seems like there's some non-userland and non-event loop that's listening for HTTP requests, and then running my callback if one es in.

Put another way — if I think about implementing my own version version of createServer, I can't think of a way to do it since any callback I schedule will run once. Does createServer just use setTimeout or setInterval to constantly recheck for an ining HTTP request? Or is there something lower level, more efficient going on. I understand I don't need to fully understand this to write efficient node code, but I'm curious how the underlying system was implemented.

(I tried following along in the node source, but the going is slow since I'm not familiar with the node module system, or the legacy assumptions w/r/t to coding patterns deep in the system code)

Behind the scenes in node, how does the http module's createServer method (and its callback) interact with the event loop? Is it possible to build functionality similar to createServer on my own in userland, or would this require a change to node's underlying system code?

That is, my general understanding of node's event loop is

  1. Event loop ticks
  2. Node looks for any callbacks to run
  3. Node runs those callbacks
  4. Event loops ticks again, process repeats ad-infinitum

What I'm still a little fuzzy on is how createServer fits into the event loop. If I do something like this

var http = require('http');

// create an http server  and handle with a simple hello world message
var server = http.createServer(function (request, response) {
    //...
});

I'm telling node to run my callback whenever an HTTP request es in. That doesn't seem patible with the event loop model I understand. It seems like there's some non-userland and non-event loop that's listening for HTTP requests, and then running my callback if one es in.

Put another way — if I think about implementing my own version version of createServer, I can't think of a way to do it since any callback I schedule will run once. Does createServer just use setTimeout or setInterval to constantly recheck for an ining HTTP request? Or is there something lower level, more efficient going on. I understand I don't need to fully understand this to write efficient node code, but I'm curious how the underlying system was implemented.

(I tried following along in the node source, but the going is slow since I'm not familiar with the node module system, or the legacy assumptions w/r/t to coding patterns deep in the system code)

Share Improve this question asked Aug 18, 2013 at 22:49 Alana StormAlana Storm 166k95 gold badges419 silver badges622 bronze badges 3
  • Node has lower level networking support nodejs/api/net.html, so you should be be able to create your own HTTP implementation if that's what you mean. Node's event system is something you can build into your classes by implementing an EventEmitter. nodejs/api/events.html – user2437417 Commented Aug 18, 2013 at 23:02
  • ...ultimately if your question is how the underlying system is implemented, then I don't know any better way than to read the source code. – user2437417 Commented Aug 18, 2013 at 23:05
  • That's very true @CrazyTrain, but to really learn a code case inside out could take weeks, in not months. Sometimes having someone point you in the right direction can save you a tremendous amount of time. – Alana Storm Commented Aug 19, 2013 at 3:16
Add a ment  | 

2 Answers 2

Reset to default 3

http.createServer is a convenience method for creating a new http.Server() and attaching the callback as an event listener to the request event. Of course the node http library implements the protocol parsing, as well.

There is no constant polling of the event loop, node is waiting for the C++ tcp bindings to receive data on the socket, which then marshall that data as a buffer to your callback.

If you were to implement your own http parser, you would start with a net.Server object as your base. See node's implementation here: https://github./joyent/node/blob/master/lib/_http_server.js#L253

The events library does the generation and handling of events as mentioned by CrazyTrain in ments. It has EventEmitter class which is used for servers, sockets and streams etc.

Event-loop like you said an infinite loop executing the callbacks after every tick. The callback provided with the http server is an eventhandler, specifically for event request.

var server = http.createServer(function (request, response) //request handler

Eventhandlers can be executed multiple times. http.server is an instance of EventEmitter. The way it works ining requests is that it first parses an ining request. When parsed, it emits the request event. The eventemitter then executes the callback for request with the parameters supplied.

You are right that EventEmitter is not a part of event loop. And it needs to be implemented by the developer of the module or library, only using the handlers provided by user of the module. But most importantly, it provides the necessary mechanism to implement events.

发布评论

评论列表(0)

  1. 暂无评论