Take the 2-minute tour ×

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 comes in. That doesn't seem compatible 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 comes 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 incoming 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
Node has lower level networking support nodejs.org/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.org/api/events.html –  Crazy Train Aug 18 '13 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. –  Crazy Train Aug 18 '13 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. –  Alan Storm Aug 19 '13 at 3:16
add comment

2 Answers

up vote 2 down vote accepted

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.com/joyent/node/blob/master/lib/_http_server.js#L253

share|improve this answer
Thanks Ben, with that information I think my actual question is "Where/how does node wait for the C++ tcp bindings to receive data on the socket?" Is that what the call to (the seemingly global and undefined) listen function is for github.com/joyent/node/blob/master/lib/net.js#l294 ? Or is this too long to explain in a comment? –  Alan Storm Aug 18 '13 at 23:22
It waits inside libuv library. If you are using linux, the code is somwhere here: github.com/joyent/libuv/blob/master/src/unix/linux-core.c#L75 –  Andrey Sidorov Aug 19 '13 at 0:36
This might be a good start to understand epoll syscall: en.wikipedia.org/wiki/Epoll –  Andrey Sidorov Aug 19 '13 at 0:37
@AndreySidorov Useful info, but not quite what I'm looking for. I want the point where either the c programmer or the javascript programmer says "When this this network event happens, send the data to this bit of javascript code" –  Alan Storm Aug 19 '13 at 2:55
add comment

The events library does the generation and handling of events as mentioned by CrazyTrain in comments. 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 incoming requests is that it first parses an incoming 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.

share|improve this answer
add comment

Your Answer


