Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

connect-timeout

expressjs1mMIT1.9.0TypeScript support: definitely-typed

Request timeout middleware for Connect/Express

readme

connect-timeout

NPM Version NPM Downloads Build Status Test Coverage Gratipay

Times out a request in the Connect/Express application framework.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ 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.

While the library will emit a 'timeout' event when requests exceed the given timeout, node will continue processing the slow request until it terminates. Slow requests will continue to use CPU and memory, even if you are returning a HTTP response in the timeout callback. For better control over CPU/memory, you may need to find the events that are taking a long time (3rd party HTTP requests, disk I/O, database calls) and find a way to cancel them, and/or close the attached sockets.

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

The timeout function takes an optional options object that may contain any of the following keys:

respond

Controls if this module will "respond" in the form of forwarding an error. 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. The timeout is completely removed and will not fire for this request in the future.

req.timedout

true if timeout fired; false otherwise.

Examples

as top-level middleware

Because of the way middleware processing works, 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 bodyParser = require('body-parser')
var cookieParser = require('cookie-parser')
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 = 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

changelog

1.9.0 / 2017-05-16

1.8.0 / 2016-11-21

  • Remove un-used debug dependency
  • deps: http-errors@~1.5.1
    • Add HttpError export, for err instanceof createError.HttpError
    • Use setprototypeof module to replace __proto__ setting
    • deps: inherits@2.0.3
    • deps: statuses@'>= 1.3.1 < 2'
    • perf: enable strict mode
  • deps: ms@0.7.2
  • deps: on-headers@~1.0.1
    • perf: enable strict mode

1.7.0 / 2015-08-23

  • Use on-finished instead of override socket destroy
    • Addresses memory leaking on keep alive connections
    • Ensures timers always get cleaned up
  • perf: enable strict mode
  • perf: remove argument reassignment
  • perf: use standard option existence check

1.6.2 / 2015-05-11

  • deps: debug@~2.2.0
  • deps: ms@0.7.1
    • Prevent extraordinarily long inputs

1.6.1 / 2015-03-14

  • deps: debug@~2.1.3
    • Fix high intensity foreground color for bold
    • deps: ms@0.7.0

1.6.0 / 2015-02-15

  • deps: http-errors@~1.3.1
    • Construct errors using defined constructors from createError
    • Fix error names that are not identifiers
    • Set a meaningful name property on constructed errors

1.5.0 / 2014-12-30

  • deps: debug@~2.1.1
  • deps: http-errors@~1.2.8
    • Fix stack trace from exported function
  • deps: ms@0.7.0
    • Add milliseconds
    • Add msecs
    • Add secs
    • Add mins
    • Add hrs
    • Add yrs

1.4.0 / 2014-10-16

  • Create errors with http-errors
  • deps: debug@~2.1.0
    • Implement DEBUG_FD env variable support

1.3.0 / 2014-09-03

  • deps: debug@~2.0.0

1.2.2 / 2014-08-10

  • deps: on-headers@~1.0.0

1.2.1 / 2014-07-22

1.2.0 / 2014-07-11

  • Accept string for time (converted by ms)
  • deps: debug@1.0.3
    • Add support for multiple wildcards in namespaces

1.1.1 / 2014-06-16

1.1.0 / 2014-04-29

  • Add req.timedout property
  • Add respond option to constructor

1.0.1 / 2014-04-28

  • Clear timer on socket destroy
  • Compatible with node.js 0.8
  • deps: debug@0.8.1

1.0.0 / 2014-03-05

  • Genesis from connect