Ashfaq Ur Rahman N
Ashfaq Ur Rahman N

Reputation: 322

How NodeJs implements listen() function of http package?

I'm trying to understand how nodeJs implements listen() for it's http server. I tried looking into the source code but it was not so helpful.

Can anybody explain how listen() function runs forever (Is there a infinite loop or setInterval that makes it happen?)

var http = require('http');

//create a server object:
http.createServer(function (req, res) {
  res.write('Hello World!'); //write a response to the client
  res.end(); //end the response
}).listen(8080); //the server object listens on port 8080

Upvotes: 0

Views: 56

Answers (1)

programmerRaj
programmerRaj

Reputation: 2078

I don't think a timer is needed to keep the process running, because it is an I/O event - https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#poll.

Between each run of the event loop, Node.js checks if it is waiting for any asynchronous I/O or timers and shuts down cleanly if there are not any.

Upvotes: 1

Related Questions