最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node Express does not handle parallel requests - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share asked Sep 1, 2020 at 5:27 Zeeshan ShamsuddeenZeeshan Shamsuddeen 6276 silver badges18 bronze badges 9
  • Can you try with cluster mode stackoverflow./questions/29163839/… And article pm2.keymetrics.io/docs/usage/cluster-mode – prasanth Commented Sep 1, 2020 at 5:29
  • 2 Are you sure it is node that is not handling parallel requests or your testing code waiting for the response before sending a second request? – slebetman Commented Sep 1, 2020 at 5:30
  • @prasanth I have not tried with pm2 cluster mode. I was trying to understand the working of NodeJS – Zeeshan Shamsuddeen Commented Sep 1, 2020 at 5:31
  • @ZeeshanShamsuddeen pm2 also one of the node package.. Mostly referred tool for nodejs production deployment is pm2. it default have cluster mode. node js single threaded one. so its not handling parallel request – prasanth Commented Sep 1, 2020 at 5:33
  • @slebetman Currently I tried using Google Chrome browser, let me try using Postman – Zeeshan Shamsuddeen Commented Sep 1, 2020 at 5:34
 |  Show 4 more ments

1 Answer 1

Reset to default 13

The issue was because I had used the browser to stimulate simulatenous hits to the server. Google chrome stalls subsequent requests if they are done to the same API which was the reason I thought my server had issues.

发布评论

评论列表(0)

  1. 暂无评论