Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set server timeout in express.js

I believe in node.js, there's a server.timeout setting. Do you know how to set the server timeout in express.js?

like image 380
iPhoneJavaDev Avatar asked Mar 06 '23 05:03

iPhoneJavaDev


1 Answers

var timeout = express.timeout // express v3 and below
var timeout = require('connect-timeout'); //express v4

app.use(timeout(120000));
app.use(haltOnTimedout);

function haltOnTimedout(req, res, next){
   if (!req.timedout) next();
}

Or without other modules

app.use(function(req, res, next){
   res.setTimeout(120000, function(){
      console.log('Request has timed out.');
         res.send(408);
      });

      next();
 });

inspired by Express.js Response Timeout

like image 114
bereket gebredingle Avatar answered Apr 08 '23 02:04

bereket gebredingle