Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
non-blocking node.js express web server

Maybe this is a possible duplicate, but even after reading many questions here on stackoverflow, i still cant find a explanation for my problem. My problem/question in general is, how nodejs and special express.js is handling parallel http requsts without blocking. Below you can find a short example that represents the problem:

var express = require("express");
var app = express();

const PORT = 4003;


var server = app.listen(PORT);
console.log("Server running at http://127.0.0.1:" + PORT + "/");

app.get('/threadtest/request1', sendResponse1);

app.get('/threadtest/request2', sendResponse2);

async function sendResponse1(req, res){
    await waitSomeTime2();
    res.send("done waiting");
}


function sendResponse2 (req, res){
    res.send('Hello2');
}

const waitSomeTime = () => new Promise((resolve) =>{
    setTimeout(() => resolve(), 5000);
});

const waitSomeTime2 = () => new Promise((resolve) =>{
    calculate(() => resolve());
});

async function calculate(){
    var i = 0;
    for(i = 0; i < 10000000000; i++){
    }
    return i.toString();
}

The webserver works fine as long there are no tasks that need some calculation time. For testing purposes, i added the calculate function (in the real application this is some kind of parsing the request and a database request).

When i directly called calculate() in the sendResponse1 function, the server could not handle any other requests until calculate was done and the response send. My first attempt was using an async function as a callback, but that did not change anything. The server is still blocked.

After some googling i found the codesnippet called waitSomeTime here on stackoverflow. Calling this after the await in sendResponse1 works as expected. The response to a get-request at request1 delays about 5 seconds but the server can handle other request while waiting. To be honest, i dont fully understand what exactly happens in waitSomeTime but i tried to integrate my calculate function, but now the server is blocked again.

The aproach to make the handler function async seems correct to me, (i have seen this many times so far) but i can´t find a way to make the calculate function non-blocking. I did not use async/await this much so far, so please excuse if i did somthing fundamental wrong. Maybe someone can help me with this or explain how this is done correctly. As said, the calculate function is only a dummy load but the goal is, that the server is not blocking other request if one of them takes way longer.

Thanks in advance

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!