'Node.js server handling multiple requests at same time

I have been trying for hours to get the Node.js server to handle 2 requests parallelly but have no success.

Here is my full code:

var http = require('http');

http.createServer(function (req, res) {
    console.log("log 1");
    handleRequest().then(() => {
        console.log("request handled");
        res.write('Hello World!');
        res.end();
    });
    console.log("log 2");
}).listen(8080);

const handleRequest = () => {
    const p = new Promise((resolve, reject) => {
        setTimeout(() => resolve('hello'), 10000);
    })
    return p;
}

When i run this i immediately open 2 tabs in my browser (Chrome) and watch the logs in the IDE. Here is the logs i'm getting:

log 1 Fri Mar 12 2021 23:27:39 GMT+0300 (GMT+03:00)
log 2 Fri Mar 12 2021 23:27:39 GMT+0300 (GMT+03:00)
request handled
log 1 Fri Mar 12 2021 23:27:49 GMT+0300 (GMT+03:00)
log 2 Fri Mar 12 2021 23:27:49 GMT+0300 (GMT+03:00)
request handled

For individual requests, it seems that my "async" code works as I expected. At first, logs printed, after 10 seconds, request handling completed. But as you can see in the timestamps, despite I am opening 2 tabs just after another (sending two requests same time), they are not handled parallelly. Actually, I was hoping to get logs like this:

log 1 Fri Mar 12 2021 23:27:39 GMT+0300 (GMT+03:00)
log 2 Fri Mar 12 2021 23:27:39 GMT+0300 (GMT+03:00)
log 1 Fri Mar 12 2021 23:27:39 GMT+0300 (GMT+03:00)
log 2 Fri Mar 12 2021 23:27:39 GMT+0300 (GMT+03:00)
request handled
request handled

It seems my second request is not handled until the first one completely done. What I am doing wrong here? Can you please give me some ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source