javascript - Node JS, createServer, and the Event Loop -
behind scenes in node, how http
module's createserver
method (and callback) interact event loop? possible build functionality similar createserver
on own in userland, or require change node's underlying system code?
that is, general understanding of node's event loop
- event loop ticks
- node looks callbacks run
- node runs callbacks
- event loops ticks again, process repeats ad-infinitum
what i'm still little fuzzy on how createserver
fits event loop. if this
var http = require('http'); // create http server , handle simple hello world message var server = http.createserver(function (request, response) { //... });
i'm telling node run callback whenever http request comes in. doesn't seem compatible event loop model understand. seems there's non-userland , non-event loop that's listening http requests, , running callback if 1 comes in.
put way — if think implementing own version version of createserver
, can't think of way since callback schedule run once. createserver
use settimeout
or setinterval
recheck incoming http request? or there lower level, more efficient going on. understand don't need understand write efficient node code, i'm curious how underlying system implemented.
(i tried following along in node source, going slow since i'm not familiar node module system, or legacy assumptions w/r/t coding patterns deep in system code)
http.createserver
convenience method creating new http.server()
, attaching callback event listener request
event. of course node http library implements protocol parsing, well.
there no constant polling of event loop, node waiting c++ tcp bindings receive data on socket, marshall data buffer
callback.
if implement own http parser, start net.server
object base. see node's implementation here: https://github.com/joyent/node/blob/master/lib/_http_server.js#l253