Why is setTimeout blocking my Node.js app?
<p>Take this code, typical node js example of a Http server, to which I've added a delay of 5 seconds to simulate some async work taking place somewhere else:</p>

<pre><code>const http = require('http');

const hostname = '127.0.0.1';
const port = 8080;

http.createServer((req, res) =&gt; {
  setTimeout(()=&gt;{
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end('Hello World\n');
  },5000);
}).listen(port, hostname, () =&gt; {
  console.log(`Server running at http://${hostname}:${port}/`);
});
</code></pre>

<p>What I would expect is that when I open 5 tabs, let's say with a delay of half a second between opening each, the server should "respond" to each tab more or less with this timings:</p>

<pre><code>t=0s - I open first tab
t=0.5s - I open second tab
t=1s - I open third tab
...
t=5s - First tab stops loading, server has replied
t=5.5s - Second tab stops loading, server has replied
t=6s - Third tab stops loading, server has replied
t=6.5s - Fourth tab stops loading, server has replied
t=7s - Fifth tab stops loading, server has replied
</code></pre>

<p>However, the behaviour I'm seeing is the following: </p>

<pre><code>t=0s - I open first tab
t=0.5s - I open second tab
t=1s - I open third tab
...
t=5s - First tab stops loading, server has replied
t=10s - Second tab stops loading, server has replied
t=15s - Third tab stops loading, server has replied
t=20s - Fourth tab stops loading, server has replied
t=25s - Fifth tab stops loading, server has replied
</code></pre>

<p>As if the subsequent requests weren't starting to run until the first one has finished. Am I missing something here? I thought the whole point of Node JS was to be able to run async taks from a single thread?</p>