Node JS, createServer and Event Loop

Behind the scenes in a node, how createServer http module createServer (and its callback) interact with the event loop? Is it possible to build functionality similar to createServer alone in the user area, or will it require a change in the node system code?

That is, my general understanding of the node event loop is

  • Event loop
  • Node is looking for any callbacks to run
  • Node performs these callbacks
  • Event loops repeat again, process repeats ad-infinitum

What I'm still a little fuzzy 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 suggest node execute my callback whenever an HTTP request arrives. This is not like the event loop model that I understand. It seems that there are several non-userland and non-event loops that listen for HTTP requests and then trigger my callback if you enter.

Put another way - if I’m thinking about implementing my version of the createServer version, I can’t figure out how to do this, since any callback I plan will run once. Does createServer only setTimeout or setInterval to constantly recheck an incoming HTTP request? Or is there something lower, more efficient. I understand that I do not need to fully understand this in order to write efficient node code, but I am curious how the underlying system was implemented.

(I tried following in Node source , but this is slow, since I am not familiar with the node modular system or the deprecated w / r / t assumptions for encoding templates deep in the system code)

+4
source share
2 answers

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

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

If you are going to implement your own http parser, you will start with the net.Server object as the base. See the node implementation here: https://github.com/joyent/node/blob/master/lib/_http_server.js#L253

+2
source

The events library creates and processes events, as CrazyTrain mentioned in the comments. It has an EventEmitter class that is used for servers, sockets and threads, etc.

The event loop, as you said, is an infinite loop that executes callbacks after each tick. The callback provided by the http server is an event handler, especially for requesting an event.

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

Eventhandlers can be run multiple times. http.server is an instance of EventEmitter. The way inbound requests work is that it parses the incoming request first. Upon analysis, it emits a request . Then the eventemitter makes a callback for the request with the provided parameters.

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

+1
source

Source: https://habr.com/ru/post/1497706/


All Articles