Times out a request in the Connect/Express application framework.在Connect/Express应用程序框架中超时请求。
This is a Node.js module available through the npm registry. 这是一个Node.js模块,可通过npm注册表获得。Installation is done using the 使用npm install
command:npm install
命令完成安装:
$ npm install connect-timeout
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. app.use(timeout('5s'))
)。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. 慢速请求将继续使用CPU和内存,即使您在超时回调中返回HTTP响应。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.为了更好地控制CPU/内存,您可能需要查找花费很长时间的事件(第三方HTTP请求、磁盘I/O、数据库调用),并找到取消这些事件和/或关闭连接的套接字的方法。
Returns middleware that times out in 返回以毫秒为单位超时的中间件。time
milliseconds. time
can also be a string accepted by the ms module. time
也可以是ms模块接受的字符串。On timeout, 超时时,req
will emit "timeout"
.req
将发出"timeout"
事件。
The timeout
function takes an optional options
object that may contain any of the following keys:timeout
函数采用可选options
对象,该对象可能包含以下任意键:
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. true
,则将超时错误传递给next()
,以便您可以自定义响应行为。This error has a 此错误具有.timeout
property as well as .status == 503
. .timeout
属性以及.status == 503
。This defaults to 这默认为true
.true
。
Clears the timeout on the request. The timeout is completely removed and will not fire for this request in the future.清除请求的超时。超时已完全删除,将来不会为此请求触发。
如果触发超时,则为true
if timeout fired; false
otherwise.true
;否则就是false
。
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.由于中间件处理的工作方式,一旦此模块将请求传递给下一个中间件(为了让您完成工作,它必须这样做),它就不能再停止流,因此在继续处理请求之前,您必须注意检查请求是否有timedout。
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)
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)
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)