Sling Academy
Home/Node.js/How to Set Response Timeouts in Express.js

How to Set Response Timeouts in Express.js

Last updated: December 28, 2023

Overview

Express.js is a popular lightweight web framework for Node.js designed for building web applications and APIs. Handling response timeouts in Express.js is crucial for maintaining a reliable and performant service. Timeouts help prevent the server from waiting indefinitely for a non-responsive client or a hanging process. In this tutorial, we will discuss how to manage response timeouts in Express.js effectively. This helps to ensure that resources are freed up and that users get timely feedback if something goes wrong.

Setting Up Basic Response Timeouts

To set a basic timeout for responses in Express.js, you can use the native Node.js setTimeout function on the response object:

app.use((req, res, next) => {
   res.setTimeout(5000, () => {
     // This will be called after 5 seconds if the response does not complete.
     console.error('Request has timed out.');
     res.status(408).send('Request timeout.');
   });

next();
});

This middleware will apply a 5-second timeout to every request that reaches your server. Note that the request will keep processing unless we explicitly end it. The setTimeout only triggers the callback function after the specified time.

Integrating with Third-Party Middleware

For more robust timeout handling, you might consider integrating a third-party middleware such as connect-timeout. This middleware allows you to set a timeout threshold for your request and automatically ends the request when the timeout is hit.

const timeout = require('connect-timeout');
 // Set a timeout limit for all requests
 app.use(timeout('5s'));

// Handle timeout error
app.use((err, req, res, next) => {
if (req.timedout && err.timeout === 5000) {
res.status(503).send('Service unavailable. Please retry.');
}
});

With connect-timeout, you can also halt the Node.js event loop and prevent any further code from executing after the timeout period.

Advanced Timeout Configurations

In more advanced scenarios, you might want timeouts to be set dynamically or want to manage timeouts on a per-route basis. You can modify the middleware to take a dynamic parameter:

function dynamicTimeout(duration) {
   return (req, res, next) => {
     res.setTimeout(duration, () => {
       console.error('Dynamic request timeout.');
       res.status(408).send('Request timeout.');
     });
     next();
   };
 }

// Then use it like this:
app.get('/slow-route', dynamicTimeout(10000), (req, res) => {
// Handle the route normally, knowing it has a longer timeout.
});

Here, the /slow-route has a custom timeout set to 10 seconds. This allows for greater flexibility when dealing with endpoints that may have varying response times.

Timeouts with Asynchronous Processing

If your app involves heavy asynchronous processing, using a simple timeout may not be enough. You may need to check if a request is still pending before trying to send a response. You can use the request’s req.timedout property to achieve this:

app.get('/data-intensive', timeout('30s'), async (req, res) => {
   try {
     const data = await heavyDataProcessing();
     if (!req.timedout) {
       res.send(data);
     }
   } catch (error) {
     if (!req.timedout) {
       res.status(500).send('Internal server error');
     }
   }
 });

This approach helps prevent a server from trying to send multiple responses to a client if things go wrong after the timeout period but before the request completes.

Conclusion

In conclusion, having a proper timeout management strategy in place is essential for developing reliable and user-friendly applications with Express.js. By utilizing both built-in Node.js features and third-party middlewares, you can create a robust system that handles response timeouts seamlessly. Remember to always test your timeout logic thoroughly to avoid unexpected behavior in production.

Next Article: Node.js & Express: Implementing Route Guards with Middleware (3 Ways)

Previous Article: How to Download a File from NodeJS Server using Express

Series: Node.js & Express Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates