Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Express Server connection times out when server processes a request more than 120seconds

I am stuck with request timeout error whenever sending a request to NodeJS express server that takes more than 120seconds for responding to client.

I tried with increasing the server timeout with request.setTimeOut(200000);, but it works only when accessed from localhost.. and times out thorough the IP address. And same problem when runs in a website too.

Please find my server code to hold the request for more than 120 seconds:

var express = require('express')
var app = express();
app.set('port', process.env.PORT || 3003);
app.listen(app.get('port'));
app.post('/cmfm', function (req, res) {
    req.setTimeout(600000);
    //req.socket.setKeepAlive(true,1);    
    var startTime = (new Date()).getTime();
    var response = {};
    response.data = 1;
    console.log('request starts to process.. ');
    setTimeout(function () {
        var endTime = (new Date()).getTime();
        console.log('Total Time taken to run --> ' + (endTime - startTime));
        res.json(response);
    }, 180000);
});

Client side Code to request the server:

$.ajax({
    url: "/cmfm",
    type: "POST",
    contentType: "application/json", // content type is must    
    cache: false,
}).done(function (resultdata) {
    alert('timout passed - ' + resultdata.data);
})
.fail(function () {
    alert('Timout failed');
});

From

  • localhost:3003
    It works as expected. ajax done is triggered after 180seconds

  • IPADDRESS:3003
    It triggers ajax.fail() after 120seconds exactly without finishing the setTimeOut Callback fn in server.

Please help me to get out of this problem.

like image 660
user2749751 Avatar asked Jan 24 '26 05:01

user2749751


1 Answers

Not sure but you can try to set the timeout client side

$.ajax({
  url: "/cmfm",
  type: "POST",
  contentType: "application/json",
  timeout: 200000,
  cache: false,
}).done(function (resultdata){
  alert('timout passed - ' + resultdata.data);
}).fail(function() {
  alert('Timout failed');
});

Otherwise seems like the connection timeout is triggered by something between the client and the server (ex. proxy, firewall, ecc...)

like image 142
Gabriele Lana Avatar answered Jan 26 '26 20:01

Gabriele Lana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!