expressjs / timeout

Request timeout middleware for Connect/Express

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Override timeout for a route

adityagoel19 opened this issue · comments

Hi,

I have app level timeout set with the following code:
app.use(connectTimeout('30s'));

I want to override the 30s timeout in a particular route and increase it's value. I tried passing it in middleware but it doesn't work.

router.post("/process", connectTimeout('1000s'), async (req, res) => {

This route still times out in 30 seconds. Please tell me how to do this?

I've run into this same problem.

Found something that works.
Following clears the global app timeout in a particular route.
req.clearTimeout();
And then you write your own setTimeout function to handle things in a particular route.

I think you've got to be careful with clearTimeout since...

The timeout is completely removed and will not fire for this request in the future.

Ultimately I think this entire module is a bit out of date particularly since it's so easy to create your own middleware. E.G.:

	app.use( function(req, res, next) {
	  res.setTimeout(120000, function() {
	    return next( new Error('408: Timeout') );
	  });
	  return next();
	});


	// overwrite it like so:
	router.get("/long/timeout" function(req, res, next) {

	  req.setTimeout(300000, function() {
	    return next( new Error('408: Timeout') );
	  });
	  
	  res.send('Success')
	});