3

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?

Ajedi32
  • 45,670
  • 22
  • 127
  • 172
faressoft
  • 19,053
  • 44
  • 104
  • 146
  • You haven't ended a pipe then or given a Reponse.End assuming with listening you mean a router like node.js app, can you give an example of code that keeps running? – Strah Behry Apr 28 '16 at 19:27
  • You need some kind of process that keeps the application alive. `setInterval(function() { console.log("Hello World.") }, 1000);` – hampusohlsson Apr 28 '16 at 19:42
  • https://github.com/mafintosh/why-is-node-running – dude Apr 28 '16 at 19:48

2 Answers2

3

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.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Yup, you are right. I think that makes my answer partially wrong. Isn't the event loop works the way I have said? – Sk Arif Apr 28 '16 at 20:29
  • Your answer details the other side of the event loop (how it makes things appear parallel), which, while correct, is irrelevant to the question "Why does it keep the process from terminating?" – Madara's Ghost Apr 28 '16 at 20:30
  • This answer is not entirely accurate. Of course node won't stop when there are still events in the queue, but even when it is empty it *sometimes* stops and sometimes not. That's the actually interesting thing. – Bergi Apr 28 '16 at 20:55
  • I don't think he asked anything about process termination. All he asked is - "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?" - and I answered according to that. – Sk Arif Apr 28 '16 at 20:55
  • @Bergi Yeah, I kinda wanted to keep it simple for OP, you're welcome to expand that or write your own answer though, I'll admit it's not a field I'm 100% fluent in. – Madara's Ghost Apr 28 '16 at 20:56
  • @SkArif "Keeps running" === "Not terminating" – Madara's Ghost Apr 28 '16 at 20:56
  • 1
    @MadaraUchiha: Nah, I'm not going to answer (like [here](http://stackoverflow.com/a/27530712/1048572)), I'm just closing as a dupe :-) – Bergi Apr 28 '16 at 20:57
  • Even better! I'll know for next time, thanks! – Madara's Ghost Apr 28 '16 at 20:57
  • @Madarauchiha if there is a server listening to some port and event loop finishes it's work then it will wait for new event, it will not terminate the server, that's your answer. What I said is, event loop has nothing to do with keeping a server running. Did I say anything wrong? – Sk Arif Apr 28 '16 at 21:07
  • But the event loop *won't* finish, as long as the server is listening for requests. That's the point, and that's why it won't exit. – Madara's Ghost Apr 28 '16 at 21:08
  • Where did I say event loop will finish? Are you saying that the server is listening because the event loop is awake/not finished? – Sk Arif Apr 28 '16 at 21:12
0

I don't think event loop has anything with keeping a server running. When you are expecting for some request over http you have to listen to some port where your users will request. You have to keep listening unless you want to stop getting request from your users to your server.

On the other hand Event loop is something when user-1 sends request your server takes the request and starts working on it. But thanks to node.js it doesn't hold the port...it keeps listening. When user-2 sends a request, your server takes the request and start working on it as it did for user-1. When your server is done with user-1's request, it sends a responds with necessary information to user-1. And it will do the same thing for user-2. But if the server finish working with user-2's request before finishing user-1's request, it will send user-2 a response and then to user-1.

Now think about thousand's of request, there will be a loop for sending back the response. That loop is called event loop.

Hope this answer makes sense. Thanks!!

Sk Arif
  • 1,110
  • 6
  • 17