• Stars
    star
    312
  • Rank 128,856 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Request timeout middleware for Connect/Express

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

More Repositories

1

express

Fast, unopinionated, minimalist web framework for node.
JavaScript
63,539
star
2

multer

Node.js middleware for handling `multipart/form-data`.
JavaScript
11,285
star
3

morgan

HTTP request logger middleware for node.js
JavaScript
7,790
star
4

session

Simple session middleware for Express
JavaScript
6,163
star
5

cors

Node.js CORS middleware
JavaScript
5,961
star
6

body-parser

Node.js body parsing middleware
JavaScript
5,376
star
7

expressjs.com

HTML
5,138
star
8

compression

Node.js compression middleware
JavaScript
2,722
star
9

csurf

CSRF token middleware
2,299
star
10

cookie-parser

Parse HTTP request cookies
JavaScript
1,907
star
11

generator

Express' application generator
JavaScript
1,803
star
12

serve-static

Serve static files
JavaScript
1,368
star
13

cookie-session

Simple cookie-based session middleware
JavaScript
1,104
star
14

vhost

virtual domain hosting
JavaScript
758
star
15

serve-favicon

favicon serving middleware
JavaScript
620
star
16

method-override

Override HTTP verbs.
JavaScript
614
star
17

response-time

Response time header for node.js
JavaScript
458
star
18

serve-index

Serve directory listings
JavaScript
435
star
19

errorhandler

Development-only error handler middleware
JavaScript
423
star
20

express-paginate

Paginate middleware
JavaScript
417
star
21

connect-multiparty

connect middleware for multiparty
JavaScript
347
star
22

express-namespace

Adds namespaced routing capabilities to Express
JavaScript
345
star
23

express-expose

Expose raw js, objects, and functions to the client-side (awesome for sharing utils, settings, current user data etc)
JavaScript
299
star
24

basic-auth-connect

Basic auth middleware for node and connect
JavaScript
129
star
25

domain-middleware

`uncaughtException` middleware for connect, base on `domain` module.
JavaScript
101
star
26

api-error-handler

Express error handlers for JSON APIs
JavaScript
100
star
27

flash

JavaScript
92
star
28

restful-router

Simple RESTful url router.
JavaScript
86
star
29

urlrouter

http url router, `connect` missing router middleware
JavaScript
59
star
30

discussions

Public discussions for the Express.js organization
55
star
31

vhostess

virtual host sub-domain mapping
JavaScript
24
star
32

connect-markdown

Auto convert markdown to html for connect.
JavaScript
20
star
33

expressjs.github.io

16
star
34

connect-rid

connect request id middleware
JavaScript
10
star
35

routification

DEPRECATED
JavaScript
10
star
36

mime-extended

DEPRECATED - Please use mime-types instead.
JavaScript
7
star
37

statusboard

A project status board for the Express community
6
star
38

.github

5
star
39

set-type

DEPRECATED - Please use mime-types instead.
JavaScript
5
star
40

security-wg

Express.js Security Working Group
4
star
41

Admin

Admin repository for the Express Organization, including pillarjs and jshttp
2
star