What determines whether a Node.js app is kept running?
When the code is something like this:
console.log('Hello World');
This phrase will be printed and the app will immediately exit.
But sometimes when we have something like a web server that is listening it keeps working.
What is the general rule that determines whether the app will keep running or not? Is it something related to the event loop or something like that?
The event loop is (partially) responsible for the process to be held open by node.
In short, the event loop works by code queuing events on the event loop, and as long as there are events queued, the process won't exist. Once all events have been exhausted and your code has finished running, the process, by default, exists there, as there is no more code to run, and no more events to process.
Anything that "works in the background" is actually scheduling an event for the event loop to process, and check whether it's done. Some things (like an http server, and various event emitters) will cause the process to never exit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With