Increase request timeout for Express Apps

In working on a Node.js microservice that had some long-running queries, I needed a way to increase the request timeout for the Express application.

This isn't documented well, if at all, in the Express documentation so here's how you do it.

Simply put, you need to configure the timeout value on the HTTP Server that express generates when you call the listen method.

For example:

// create an express app
const app = express();

// add a route that delays response for 3 minutes
app.get('/test', (req, res) => {  
  setTimeout(() => {
  }, 180000)

// start the server
const server = app.listen(8080);

// increase the timeout to 4 minutes
server.timeout = 240000;  

This technique works for both raising and lowering the request timeout.

comments powered by Disqus