How do 'request' and 'Response' events work on Node.js?

Asked

Viewed 1,648 times

0

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

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

I was confused because in the DOC they refer to http.Server as an event broadcaster and the http.ClientRequest as an object builder. I tried to understand native code in Node’s lib and there I found them all in their places but still didn’t understand.

The question is: what happens behind a standard server build using the request and response? What exactly are?

Obs, I know how to build eventListeners and Emitters using the http.events simply.

Source code: http.Clientrequest, http. Server, http.Incomingmensage, http.createServer

  • As far as I know they are simple objects that you use to manipulate the request and the answer. They are not events. I don’t think I understand your question...

  • http://nodejs.org/api/http.html#http_class_http_server, here for example refers to request as an event that is instance of Incomingmensage @bfavaretto

  • I think I understand your confusion, I’ll answer.

  • Managed to Resolve That Question?

1 answer

4

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

function (request, response) { }

That part is the signature of Listener event. That is, when you create a server with http.createServer([requestListener]), the requestListener that you must pass is a function with that signature, a function that takes as a first argument an object request (of the kind http.IncomingMessage) and an object response (of the kind http.ServerResponse).

In other words, what the http.createServer([requestListener]) makes associating an Event Listener to the "request" event. Every time a request is made, the server emits this event, and the Reader will be invoked with the given arguments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.