The code below is a simple Node.js web server that responds to a request when the URL is matched.
Researching online about node.js it is stated that once you start your script (node index.js
) callbacks will be placed in their appropriate phase, then after your script is parsed the node process will enter the Event Loop and execute appropriate callbacks specific to a phase. Node will exit if there are no more callbacks to be executed.
So my question is if the request handler is run the first time when I visit the home page "/"
OR Hello Page "/hello
", how come node is still running even after subsequent requests.
const http = require('http');
const server = http.createServer((req,res) => {
if(req.url === "/") {
res.end("Home Page")
}
else if(req.url === "/hello") {
res.end("Hello Page")
}
else {
res.end("Page Not Found")
}
})
server.listen(5000)
I expect that once the Request Handler is executed it should be removed from whichever Phase it has been put into, hence node should exit. So what is keeping the Node program from exiting?