响应时间中间件
Node.js 服务器的响应时间。
🌐 Response time for Node.js servers.
此模块创建了一个中间件,用于记录 HTTP 服务器请求的响应时间。这里的“响应时间”被定义为从请求进入此中间件到向客户端写出头信息所经过的时间。
🌐 This module creates a middleware that records the response time for requests in HTTP servers. The “response time” is defined here as the elapsed time from when a request enters this middleware to when the headers are written out to the client.
安装
🌐 Installation
这是一个可以通过npm注册表获取的Node.js模块。安装可以使用npm install命令完成:
🌐 This is a Node.js module available through the
npm registry. Installation is done using the
npm install command:
npm install response-timeyarn add response-timepnpm add response-timebun add response-timeNote
response-time 不包含其自身的 TypeScript 类型定义。如果你使用 TypeScript,还需要作为开发依赖从 DefinitelyTyped 安装社区维护的类型:
npm install --save-dev @types/response-timeyarn add --dev @types/response-timepnpm add --save-dev @types/response-timebun add --dev @types/response-timeAPI
var responseTime = require('response-time');responseTime([options])
创建一个中间件,为响应添加 X-Response-Time 头。如果你不想使用此模块自动设置头,请参阅关于 responseTime(fn) 的部分。
🌐 Create a middleware that adds a X-Response-Time header to responses. If
you don’t want to use this module to automatically set a header, please
see the section about responseTime(fn).
选项
🌐 Options
responseTime 函数接受一个可选的 options 对象,该对象可能包含以下任意键:
🌐 The responseTime function accepts an optional options object that may
contain any of the following keys:
digits
输出中要包含的固定数字位数,始终以毫秒为单位,默认为 3(例如:2.300ms)。
🌐 The fixed number of digits to include in the output, which is always in
milliseconds, defaults to 3 (ex: 2.300ms).
header
要设置的头名称,默认为 X-Response-Time。
🌐 The name of the header to set, defaults to X-Response-Time.
suffix
布尔值,用于指示是否应在输出中添加计量单位后缀,默认值为 true(例如:2.300ms 与 2.300)。
🌐 Boolean to indicate if units of measurement suffix should be added to
the output, defaults to true (ex: 2.300ms vs 2.300).
responseTime(fn)
创建一个新的中间件,用于记录请求的响应时间,并将其提供给你自己的函数 fn。fn 参数将以 fn(req, res, time) 的形式被调用,其中 time 是以毫秒为单位的数字。
🌐 Create a new middleware that records the response time of a request and
makes this available to your own function fn. The fn argument will be
invoked as fn(req, res, time), where time is a number in milliseconds.
例子
🌐 Examples
express/connect
var express = require('express');var responseTime = require('response-time');
var app = express();
app.use(responseTime());
app.get('/', function (req, res) { res.send('hello, world!');});原生 HTTP 服务器
🌐 vanilla http server
var finalhandler = require('finalhandler');var http = require('http');var responseTime = require('response-time');
// create "middleware"var _responseTime = responseTime();
http.createServer(function (req, res) { var done = finalhandler(req, res); _responseTime(req, res, function (err) { if (err) return done(err);
// respond to request res.setHeader('content-type', 'text/plain'); res.end('hello, world!'); });});响应时间指标
🌐 response time metrics
var express = require('express');var responseTime = require('response-time');var StatsD = require('node-statsd');
var app = express();var stats = new StatsD();
stats.socket.on('error', function (error) { console.error(error.stack);});
app.use( responseTime(function (req, res, time) { var stat = (req.method + req.url).toLowerCase().replace(/[:.]/g, '').replace(/\//g, '_'); stats.timing(stat, time); }));
app.get('/', function (req, res) { res.send('hello, world!');});许可证
🌐 License