2

I am very new to node.js and first thing that I could not easily google is following. Consider the most typical node.js example:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

How will node.js know that it should not exit after last line has executed? Coming from .NET I have a simplified assumption that the process exits when all of its non-background threads are terminated. As node.js is single-threaded, I believe that there should be some logic monitoring event subscriptions which will decide whether there are still events which may be triggered, or otherwise node.js can terminate. Unfortunately, I could not find any docs on that. Could anyone help?

2 Answers 2

2

If you are from .NET background, you might have written some of the ASP.NET web application hosted over IIS. In this case your .NET framework creates a pool of application thread to handle the incoming request, your page gets executed and the response is sent back to the client. Thus your page execution is being handled by .NET framework. In whole process IIS keeps on listening the incoming request and handle over the request to appropriate workers/threads.
Node.JS works in same manner where you just instructed the Node to how to process the request. When you create a server, node opens the socket to listen for incoming request. However since there is no close command written, your server will keep on running.

To understand how node handles/process the request, please visit
http://strongloop.com/strongblog/node-js-performance-event-loop-monitoring/ http://strongloop.com/strongblog/node-js-event-loop/

To learn how to close the server visit http://nodejs.org/api/net.html#net_server_close_callback

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! 'event loop' is the keyword I needed. In the docs I found the following: Node simply enters the event loop after executing the input script. Node exits the event loop when there are no more callbacks to perform.
@ironic exactly. Just to add a point. In case of a server creation since you have registered a callback, node will not exit in general, unless you close it programatically. You may try by omitting the callback from your code. In that case your server will be closed instantly.
2

what ? this code does not exit! instead it starts listening for any http requests.

when it exits?

else if there are no request handlers defined, and if it is no longer listening to any events and all the code statements are executed then it exits for exaple: a simple server consisting of just one statement is:

console.log('Hiii');

this will exit as soon as the last statement is executed because we are not expecting it to listen for any events.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.