How do the 'request' and 'response' events in Node.js work?

0

Everyone knows how to use request and response properly as http.createServer callback parameters, and that we use these two parameters as objects within the callback.

I searched the DOC's API and there it says that both are instances of the object http.IncomingMensage and that, in turn, is created by http.Server (request) and http.ClientRequest (response) ...

I was confused because in DOC they refer to http.Server as an event sender and http.ClientRequest as an object constructor. I tried to understand the native code in the node's lib and there I found all of them in their places but I still did not understand.

Doubt is: What happens behind a standard server build using request and response ? What exactly are they?

asked by anonymous 13.01.2015 / 18:58

1 answer

3

I think the way the Node documentation was written is confusing you. It says yes that there is an event called request , and then it shows the following:

function (request, response) { }

This part is the listener signature of this event. That is, when you create a server with http.createServer([requestListener]) , the requestListener you should spend is a function with this signature, a function that receives as a first argument a request object (of type http.IncomingMessage ) and a response object% (of type http.ServerResponse ).

In other words, what http.createServer([requestListener]) does is associate an event listener with the "request" event. Every time a request is made, the server issues this event, and the listener will be invoked with the arguments indicated.

    
13.01.2015 / 19:28