Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

response-time

expressjs1.5mMIT2.3.3TypeScript support: definitely-typed

Response time for Node.js servers

http, res, response time, x-response-time

readme

response-time

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Response time for Node.js servers.

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

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

$ npm install response-time

API

var responseTime = require('response-time')

responseTime([options])

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

The responseTime function accepts an optional options object that may contain any of the following keys:

digits

The fixed number of digits to include in the output, which is always in milliseconds, defaults to 3 (ex: 2.300ms).

The name of the header to set, defaults to 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).

responseTime(fn)

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

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

MIT

changelog

2.3.3 / 2024-10-07

  • deps: depd@~2.0.0
    • Remove remove use of eval
  • deps: depd@~1.1.2
    • Remove unnecessary Buffer loading
    • perf: remove argument reassignment

2.3.2 / 2015-11-15

  • deps: depd@~1.1.0
    • Enable strict mode in more places
    • Support web browser loading
  • deps: on-headers@~1.0.1
    • perf: enable strict mode
  • perf: enable strict mode

2.3.1 / 2015-05-14

  • deps: depd@~1.0.1

2.3.0 / 2015-02-15

  • Add function argument to support recording of response time

2.2.0 / 2014-09-22

  • Add suffix option
  • deps: depd@~1.0.0

2.1.0 / 2014-09-16

  • Add header option for custom header name
  • Change digits argument to an options argument

2.0.1 / 2014-08-10

  • deps: on-headers@~1.0.0

2.0.0 / 2014-05-31

  • add digits argument
  • do not override existing X-Response-Time header
  • timer not subject to clock drift
  • timer resolution down to nanoseconds
  • use on-headers module

1.0.0 / 2014-02-08

  • Genesis from connect