Error: socket hang up

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

It is not clearly defined in documentation and not 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.

Here is the example for same:

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

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

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

// increase the timeout to 5 minutes
server.timeout = 300000;  

And you are done – this will work just fine.

Reference https://nodejs.org/api/http.html#http_server_timeout

Leave a Reply

Your email address will not be published. Required fields are marked *