Express JS Post query

Could you please reply to this query posted in the screeshot

In this instance, ’data’ is the name of an event listener added as an argument when you use the emitter.on() function. The second argument, data, is the actual data being added to the store variable.

okay.

But we are not using any emitter here …right ? we are using req.on . … how do you explain it ?

confused with your reply.

The way I understand it is that you are using an event emitter, in this case it’s the request (req) that you’re attaching an event listener to. I’m probably not explaining it well, but it if you go to the top of that page I linked to, it may become clearer.

link ? there is no link

The link is in the text emitter.on()

1 Like

okay…

I understand you are saying req here is event emitter. … an event emitter is called by emit () …right ? there is no req.emit(‘data’) here. … How the code would get execute then ?

Here is a link how event emitters are defined and listerns are called : https://prnt.sc/hrxetj
my hunch is this code is not using emit() here.

I still find it confusing . Need help

Hey winzip,

What’s happening here is that you’re attaching a listener on the request object, which will be triggered every time express receives POST data to the ‘/ajaxcall’ route. ‘data’ is just one of several events that the request object can emit.

Interesting …awesome, …thanks

will it be possible to know what are other events which request object can auto emit ?

Well the express request object is basically an extended “vanilla” node request object, which again implements the readable stream interface (and streams again are all instances of event emitters). Thus, possible emitted events are (at least) those of the node request object and the node readstream:

https://nodejs.org/api/http.html#http_class_http_incomingmessage
https://nodejs.org/api/stream.html#stream_class_stream_readable

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.