response-time

NPM Version NPM Downloads Build Status Test Coverage

Response time for Node.js servers.Node.js服务器的响应时间。

This module creates a middleware that records the response time for requests in HTTP servers. 此模块创建一个中间件,用于记录HTTP服务器中请求的响应时间。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安装

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 response-time

API

var responseTime = require('response-time')

responseTime([options])

Create a middleware that adds a X-Response-Time header to responses. 创建一个向响应添加X-Response-Time标头的中间件。If you don't want to use this module to automatically set a header, please see the section about responseTime(fn).如果您不想使用此模块自动设置标题,请参阅关于responseTime(fn)的部分。

Options

The responseTime function accepts an optional options object that may contain any of the following keys:responseTime函数接受可选options对象,该对象可能包含以下任意键:

digits

The fixed number of digits to include in the output, which is always in milliseconds, defaults to 3 (ex: 2.300ms).输出中包含的固定位数(始终以毫秒为单位)默认为3(例如:2.300ms)。

header

The name of the header to set, defaults to X-Response-Time.要设置的标头的名称,默认为X-Response-Time

suffix

Boolean to indicate if units of measurement suffix should be added to the output, defaults to true (ex: 2.300ms vs 2.300).布尔值表示是否应将测量单位后缀添加到输出中,默认为true(例如:2.300ms vs 2.300)。

responseTime(fn)

Create a new middleware that records the response time of a request and makes this available to your own function fn. 创建一个记录请求响应时间的新中间件,并使其可用于您自己的函数fnThe fn argument will be invoked as fn(req, res, time), where time is a number in milliseconds.fn参数将以fn(req, res, time)的形式调用,其中time是以毫秒为单位的数字。

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!')
})

vanilla http server香草http服务器

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

MIT