4

Running an express server with route handler like this:

  app.get('*', (req, res) => {
   console.log('Received request');

   setTimeout(() => {
     res.end();
     console.log('Ended request');
   }, 3000);
  });

If one visits the route '/foo' and then immediately '/bar' (in two different tabs), this will be output via console.log:

Received request
Received request
Ended request
Ended request

But if you do the same except visiting '/foo' in both tabs, the server waits for the first request before handling the second:

Received request
Ended request
Received request
Ended request

I believed all method requests were handled asynchronously, but this seems to be an exception. Could anyone provide an in-depth explanation on why this happens? I have not been able to find any info in the express documentation.

exNihilo
  • 43
  • 4
  • 2
    This is probably a browser optimization. See: https://stackoverflow.com/a/27514611/2652134 – Brahma Dev Feb 18 '18 at 17:41
  • I can confirm Brahma Dev response. I just made a little node.js client that contacts a simple server like the one you just defined and both requests were handled in parallel. – oniramarf Feb 18 '18 at 18:00

2 Answers2

1

Testing using Windows 10 Node.js v8.1.3 and Chrome as well as MS Edge browsers showed something interesting. Using your original posted code and making the http request using Chrome gave the same delayed second response behavior that you described. Modifying the code to include writing responses to the request client like:

app.get('*', (req, res) => {
   console.log('Received request');
   res.write("Request received\n");
   setTimeout(() => {
       res.write("Ending request response");
       res.end();
       console.log('Ended request');
   }, 3000);
});

gave the expected result.

Neither the first nor second http response was delayed by the first code version when made with MS Edge. Based on that, it looks like the comment from Brahma Dev about the browser cache optimization is correct.

stackuser83
  • 2,012
  • 1
  • 24
  • 41
0

Below is the results I have got and which is expected as of Async(Node Way)

To Make it Simple I have modified code as

app.get('*', (req, res) => {
  var requestURL = req.url;
  console.log('Received request : ' + requestURL + ' on Time : ' + new Date().toISOString());
  setTimeout(() => {
    res.end();
    console.log('Ended request : ' + requestURL + ' on Time : ' + new Date().toISOString());
  }, 15000);
});

I have fired the 4 URLS with different tabs and in Firefox at same time with URLS as

  1. http://localhost:3000/footab1
  2. http://localhost:3000/bar
  3. http://localhost:3000/bar
  4. http://localhost:3000/booztab1

Here the difference what we can see that NodeJs is firing all the URL which are unique are fired instantaneously without waiting for the 1st Request to get complete hence its behaving as proper and in my above test since I have used /bar two times browser called only once because you are hitting the Same endpoint in two tabs

If you were using other Sync Programming languages such as PHP,JAVA you would expect result as Received request>Ended request then your 2nd Request would have processed

Received request : /bar on Time : 2018-02-18T18:17:50.250Z Received request : /booztab1 on Time : 2018-02-18T18:17:50.253Z Received request : /footab1 on Time : 2018-02-18T18:17:50.253Z Ended request : /bar on Time : 2018-02-18T18:18:05.268Z Ended request : /booztab1 on Time : 2018-02-18T18:18:05.268Z Ended request : /footab1 on Time : 2018-02-18T18:18:05.268Z Received request : /bar on Time : 2018-02-18T18:18:05.268Z Ended request : /bar on Time : 2018-02-18T18:18:20.290Z

Mahesh G
  • 1,226
  • 4
  • 30
  • 57