Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

connect-timeout

Package Overview
Dependencies
Maintainers
8
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-timeout

timeout middleware

  • 1.6.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
8
Created

What is connect-timeout?

The connect-timeout npm package is a middleware for Connect/Express applications that allows you to set a timeout for HTTP requests. If a request takes longer than the specified timeout, it will be aborted and a timeout error will be sent to the client.

What are connect-timeout's main functionalities?

Setting a basic timeout

This feature allows you to set a basic timeout for all incoming HTTP requests. If a request takes longer than the specified time (in this case, 5 seconds), it will be aborted and a timeout error will be sent to the client.

const express = require('express');
const timeout = require('connect-timeout');

const app = express();

// Set a timeout of 5 seconds
app.use(timeout('5s'));

app.get('/', (req, res) => {
  // Simulate a long-running request
  setTimeout(() => {
    res.send('Hello, world!');
  }, 6000);
});

app.use((err, req, res, next) => {
  if (req.timedout) {
    res.status(503).send('Service unavailable. Please try again later.');
  } else {
    next(err);
  }
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Custom timeout handler

This feature allows you to define a custom timeout handler. If a request times out, the custom handler will send a specific message to the client.

const express = require('express');
const timeout = require('connect-timeout');

const app = express();

// Set a timeout of 5 seconds
app.use(timeout('5s'));

app.get('/', (req, res) => {
  // Simulate a long-running request
  setTimeout(() => {
    res.send('Hello, world!');
  }, 6000);
});

// Custom timeout handler
app.use((req, res, next) => {
  if (req.timedout) {
    res.status(503).send('Custom timeout message: Service unavailable.');
  } else {
    next();
  }
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Conditional timeout

This feature allows you to set a conditional timeout based on the request path. In this example, the '/slow' endpoint has a longer timeout of 10 seconds, while other endpoints do not have a timeout.

const express = require('express');
const timeout = require('connect-timeout');

const app = express();

// Set a conditional timeout
app.use((req, res, next) => {
  if (req.path === '/slow') {
    timeout('10s')(req, res, next);
  } else {
    next();
  }
});

app.get('/slow', (req, res) => {
  // Simulate a long-running request
  setTimeout(() => {
    res.send('This is a slow endpoint.');
  }, 11000);
});

app.get('/fast', (req, res) => {
  res.send('This is a fast endpoint.');
});

app.use((err, req, res, next) => {
  if (req.timedout) {
    res.status(503).send('Service unavailable. Please try again later.');
  } else {
    next(err);
  }
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Other packages similar to connect-timeout

FAQs

Package last updated on 16 Feb 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc