I just created a NodeJS Express server to understand the working of NodeJS. I have learned that NodeJS can handle a large number of API requests simultaneously given that CPU intensive tasks are not done by the same thread.
However, my server is not even able to handle 2 requests simultaneously, that too without any CPU intensive tasks. I am pretty sure that I am missing something here. Here is my code:
const http = require('http');
const express = require('express');
const app = express();
const waitForSomeTime = () => new Promise((resolve) => {
setTimeout(() => resolve(), 5000);
});
app.use(async (req, res) => {
console.log('Request received');
await waitForSomeTime();
console.log('Response sending')
return res.send('DONE');
})
app.set('port', 3000);
const server = http.createServer(app);
server.listen(3000, () => { });
Currently, when I hit the API with 2 requests parallelly, the server resolves the first one after 5 seconds and then accepts the 2nd request. The total time taken is 10 seconds. I tried this application in my local Mac system and in a Linux VM, which had 4 cores.
Ideally, shouldn't NodeJS accept the 2nd request as soon as it initiates the timer of the 1st request. Thereby resolving both the requests in 5 seconds
Is there something I need to configure in the machine settings? or anywhere?