3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Build Status][travis-image]][travis-url]
6 [![Test Coverage][coveralls-image]][coveralls-url]
7 [![Gratipay][gratipay-image]][gratipay-url]
9 Times out the request in `ms`, defaulting to `5000`.
14 $ npm install connect-timeout
19 **NOTE** This module is not recommend as a "top-level" middleware (i.e.
20 `app.use(timeout('5s'))`) unless you take precautions to halt your own
21 middleware processing. See [as top-level middleware](#as-top-level-middleware)
22 for how to use as a top-level middleware.
24 ### timeout(time, [options])
26 Returns middleware that times out in `time` milliseconds. `time` can also
27 be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme)
28 module. On timeout, `req` will emit `"timeout"`.
32 The `timeout` function takes an optional `options` object that may contain
33 any of the following keys:
37 Controls if this module will "respond" in the form of forwarding an error.
38 If `true`, the timeout error is passed to `next()` so that you may customize
39 the response behavior. This error has a `.timeout` property as well as
40 `.status == 503`. This defaults to `true`.
42 ### req.clearTimeout()
44 Clears the timeout on the request. The timeout is completely removed and
45 will not fire for this request in the future.
49 `true` if timeout fired; `false` otherwise.
53 ### as top-level middleware
55 Because of the way middleware processing works, this once this module
56 passes the request to the next middleware (which it has to do in order
57 for you to do work), it can no longer stop the flow, so you must take
58 care to check if the request has timedout before you continue to act
62 var express = require('express');
63 var timeout = require('connect-timeout');
65 // example of using this top-level; note the use of haltOnTimedout
66 // after every middleware; it will stop the request flow on a timeout
68 app.use(timeout('5s'));
69 app.use(bodyParser());
70 app.use(haltOnTimedout);
71 app.use(cookieParser());
72 app.use(haltOnTimedout);
74 // Add your routes here, etc.
76 function haltOnTimedout(req, res, next){
77 if (!req.timedout) next();
86 var express = require('express');
87 var bodyParser = require('body-parser');
88 var timeout = require('connect-timeout');
91 app.post('/save', timeout('5s'), bodyParser.json(), haltOnTimedout, function(req, res, next){
92 savePost(req.body, function(err, id){
93 if (err) return next(err);
94 if (req.timedout) return;
95 res.send('saved as id ' + id);
99 function haltOnTimedout(req, res, next){
100 if (!req.timedout) next();
103 function savePost(post, cb){
104 setTimeout(function(){
105 cb(null, ((Math.random()* 40000) >>> 0));
106 }, (Math.random()* 7000) >>> 0));
115 var bodyParser = require('body-parser');
116 var connect = require('connect');
117 var timeout = require('connect-timeout');
119 var app = require('connect');
120 app.use('/save', timeout('5s'), bodyParser.json(), haltOnTimedout, function(req, res, next){
121 savePost(req.body, function(err, id){
122 if (err) return next(err);
123 if (req.timedout) return;
124 res.send('saved as id ' + id);
128 function haltOnTimedout(req, res, next){
129 if (!req.timedout) next();
132 function savePost(post, cb){
133 setTimeout(function(){
134 cb(null, ((Math.random()* 40000) >>> 0));
135 }, (Math.random()* 7000) >>> 0));
145 [npm-image]: https://img.shields.io/npm/v/connect-timeout.svg
146 [npm-url]: https://npmjs.org/package/connect-timeout
147 [travis-image]: https://img.shields.io/travis/expressjs/timeout/master.svg
148 [travis-url]: https://travis-ci.org/expressjs/timeout
149 [coveralls-image]: https://img.shields.io/coveralls/expressjs/timeout/master.svg
150 [coveralls-url]: https://coveralls.io/r/expressjs/timeout?branch=master
151 [downloads-image]: https://img.shields.io/npm/dm/connect-timeout.svg
152 [downloads-url]: https://npmjs.org/package/connect-timeout
153 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
154 [gratipay-url]: https://www.gratipay.com/dougwilson/