Socket
Socket
Sign inDemoInstall

connect-timeout

Package Overview
Dependencies
6
Maintainers
8
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    connect-timeout

timeout middleware


Version published
Weekly downloads
236K
decreased by-19.69%
Maintainers
8
Install size
74.3 kB
Created
Weekly downloads
 

Readme

Source

connect-timeout

NPM Version NPM Downloads Build Status Test Coverage Gratipay

Times out the request in ms, defaulting to 5000.

Install

$ npm install connect-timeout

API

NOTE This module is not recommend as a "top-level" middleware (i.e. app.use(timeout('5s'))) unless you take precautions to halt your own middleware processing. See as top-level middleware for how to use as a top-level middleware.

timeout(time, options)

Returns middleware that times out in time milliseconds. time can also be a string accepted by the ms module. On timeout, req will emit "timeout".

options
  • respond - If true, the timeout error is passed to next() so that you may customize the response behavior. This error has a .timeout property as well as .status == 503. This defaults to true.

req.clearTimeout()

Clears the timeout on the request.

req.timedout

true if timeout fired; false otherwise.

Examples

as top-level middleware

Because of the way middleware processing works, this once this module passes the request to the next middleware (which it has to do in order for you to do work), it can no longer stop the flow, so you must take care to check if the request has timedout before you continue to act on the request.

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

// example of using this top-level; note the use of haltOnTimedout
// after every middleware; it will stop the request flow on a timeout
var app = express();
app.use(timeout('5s'));
app.use(bodyParser());
app.use(haltOnTimedout);
app.use(cookieParser());
app.use(haltOnTimedout);

// Add your routes here, etc.

function haltOnTimedout(req, res, next){
  if (!req.timedout) next();
}

app.listen(3000);

express 3.x

var express = require('express');
var bodyParser = require('body-parser');
var timeout = require('connect-timeout');

var app = express();
app.post('/save', timeout('5s'), bodyParser.json(), haltOnTimedout, function(req, res, next){
  savePost(req.body, function(err, id){
    if (err) return next(err);
    if (req.timedout) return;
    res.send('saved as id ' + id);
  });
});

function haltOnTimedout(req, res, next){
  if (!req.timedout) next();
}

function savePost(post, cb){
  setTimeout(function(){
    cb(null, ((Math.random()* 40000) >>> 0));
  }, (Math.random()* 7000) >>> 0));
}

app.listen(3000);

connect

var bodyParser = require('body-parser');
var connect = require('connect');
var timeout = require('connect-timeout');

var app = require('connect');
app.use('/save', timeout('5s'), bodyParser.json(), haltOnTimedout, function(req, res, next){
  savePost(req.body, function(err, id){
    if (err) return next(err);
    if (req.timedout) return;
    res.send('saved as id ' + id);
  });
});

function haltOnTimedout(req, res, next){
  if (!req.timedout) next();
}

function savePost(post, cb){
  setTimeout(function(){
    cb(null, ((Math.random()* 40000) >>> 0));
  }, (Math.random()* 7000) >>> 0));
}

app.listen(3000);

License

MIT

FAQs

Last updated on 31 Dec 2014

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc