🌐 Nodejs.cn

超时中间件

在 Connect/Express 应用框架中使请求超时。

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

安装

🌐 Install

这是一个可以通过npm注册表获取的Node.js模块。安装可以使用npm install命令完成:

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

Terminal window
npm install connect-timeout

Note

connect-timeout 不包含其自身的 TypeScript 类型定义。如果你使用 TypeScript,还需要作为开发依赖从 DefinitelyTyped 安装社区维护的类型:

Terminal window
npm install --save-dev @types/connect-timeout

API

注意 除非你采取措施停止你自己的中间件处理,否则不建议将此模块作为“顶层”中间件(即 app.use(timeout('5s')))使用。有关如何作为顶层中间件使用,请参见 作为顶层中间件

虽然当请求超过给定超时时,库会触发 ‘timeout’ 事件,但 Node 会继续处理这个慢请求直到它结束。即使你在超时回调中返回了 HTTP 响应,慢请求仍会继续占用 CPU 和内存。为了更好地控制 CPU/内存,你可能需要找到耗时较长的事件(第三方 HTTP 请求、磁盘 I/O、数据库调用),并找到取消它们的方法,和/或关闭附加的套接字。

🌐 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])

返回在 time 毫秒后超时的中间件。time 也可以是 ms 模块接受的字符串。超时后,req 将触发 "timeout"

🌐 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

timeout 函数接受一个可选的 options 对象,该对象可能包含以下任意键:

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

respond

控制此模块是否以转发错误的形式“响应”。 如果为 true,超时错误将传递给 next(),以便你可以自定义响应行为。该错误具有 .timeout 属性以及 .status == 503。默认值为 true

🌐 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 如果超时触发;否则 false

例子

🌐 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