All Projects → expressjs → Timeout

expressjs / Timeout

Licence: mit
Request timeout middleware for Connect/Express

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Timeout

laminas-mvc-middleware
Dispatch middleware pipelines in place of controllers in laminas-mvc.
Stars: ✭ 18 (-93.59%)
Mutual labels:  middleware
Zen
zen is a elegant and lightweight web framework for Go
Stars: ✭ 257 (-8.54%)
Mutual labels:  middleware
Awesome Psr15 Middlewares
A curated list of awesome PSR-15 HTTP Middleware resources
Stars: ✭ 271 (-3.56%)
Mutual labels:  middleware
redux-simple-auth
A library for implementing authentication and authorization for redux applications
Stars: ✭ 20 (-92.88%)
Mutual labels:  middleware
phalcon-authmiddleware
Auth Middleware component for Phalcon.
Stars: ✭ 27 (-90.39%)
Mutual labels:  middleware
Pdfkit
A Ruby gem to transform HTML + CSS into PDFs using the command-line utility wkhtmltopdf
Stars: ✭ 2,799 (+896.09%)
Mutual labels:  middleware
phpdebugbar
PSR-15 middleware for PHP Debug bar
Stars: ✭ 64 (-77.22%)
Mutual labels:  middleware
Product Ei
An open source, a high-performance hybrid integration platform that allows developers quick integration with any application, data, or system.
Stars: ✭ 277 (-1.42%)
Mutual labels:  middleware
stats
📊 Request statistics middleware that stores response times, status code counts, etc
Stars: ✭ 15 (-94.66%)
Mutual labels:  middleware
Cors
🔮Supported(Laravel/Lumen/PSR-15/Swoft/Slim/ThinkPHP) - PHP CORS (Cross-origin resource sharing) middleware.
Stars: ✭ 266 (-5.34%)
Mutual labels:  middleware
koa-waterline
Deprecated: A middleware for your hose
Stars: ✭ 14 (-95.02%)
Mutual labels:  middleware
wam.js
wam is a koa and next.js inspired middleware framework for node
Stars: ✭ 14 (-95.02%)
Mutual labels:  middleware
Home
Project Glimpse: Node Edition - Spend less time debugging and more time developing.
Stars: ✭ 260 (-7.47%)
Mutual labels:  middleware
ngx-security-starter
A full implementation of the heloufir/security-starter with an Angular 7+ front-end implementation, with a laravel 5.8.* server
Stars: ✭ 37 (-86.83%)
Mutual labels:  middleware
Secure headers
Manages application of security headers with many safe defaults
Stars: ✭ 2,942 (+946.98%)
Mutual labels:  middleware
rack-fluentd-logger
Rack middleware to send traffic logs to Fluentd
Stars: ✭ 21 (-92.53%)
Mutual labels:  middleware
Laravel Demo Mode
A package to protect your work in progress from prying eyes
Stars: ✭ 259 (-7.83%)
Mutual labels:  middleware
Simple Php Router
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
Stars: ✭ 279 (-0.71%)
Mutual labels:  middleware
Faraday Http Cache
a faraday middleware that respects HTTP cache
Stars: ✭ 276 (-1.78%)
Mutual labels:  middleware
Frontexpress
An Express.js-Style router for the front-end
Stars: ✭ 263 (-6.41%)
Mutual labels:  middleware

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].