I need some clarity on understanding Node.JS. To the best of my knowledge, my entire server.js app works on an event-based, non-blocking but single-threaded model. Unlike apache, it doesn't spawn a new thread for each new request, right?<p>But if you consider the below code:<p>setTimeout(function () {
for (var i = 0; i < 1000000000; i++){}
}, 1000);<p>Imagine that the above code is inside my server script. I've placed a 1 second timeout which is non blocking, but after that I've placed a great loop that blocks the entire thread for a long time.<p>In between that loop, if a new request comes, then what will happen? Node.JS will create a new instance of my server, right? So, Node.JS itself can't be single threaded because its already busy with my loop. So overall, how many threads is my entire app (including the V8 engine) consuming?
Node.js internally has a small pool of threads. Processing of response/request is by design handled in one thread designated to handle event loop.<p>Therefore - if your handler blocks, the _handling_ of requests will be blocked. Your server will still be able to ingest new requests.<p>The downside of this is that requests are stored internally in a queue so if your handler blocks for a long time, you can bring the whole node down.<p>Just in case you want a longer read on this:
<a href="https://www.toptal.com/nodejs/top-10-common-nodejs-developer-mistakes" rel="nofollow">https://www.toptal.com/nodejs/top-10-common-nodejs-developer...</a><p>Or if you want to go really bare internals:
<a href="http://www.journaldev.com/7462/node-js-processing-model-single-threaded-model-with-event-loop-architecture" rel="nofollow">http://www.journaldev.com/7462/node-js-processing-model-sing...</a>
This might be interesting for you. Sorry not Node expert myself, but it seems to touch some of your question.
<a href="http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/" rel="nofollow">http://blog.mixu.net/2011/02/01/understanding-the-node-js-ev...</a>