3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Build Status][travis-image]][travis-url]
6 [![Test Coverage][coveralls-image]][coveralls-url]
7 [![Gratipay][gratipay-image]][gratipay-url]
9 Response time for Node.js servers.
11 This module creates a middleware that records the response time for
12 requests in HTTP servers. The "response time" is defined here as the
13 elapsed time from when a request enters this middleware to when the
14 headers are written out to the client.
19 $ npm install response-time
25 var responseTime = require('response-time')
28 ### responseTime([options])
30 Create a middleware that adds a `X-Response-Time` header to responses. If
31 you don't want to use this module to automatically set a header, please
32 see the section about [`responseTime(fn)`](#responsetimeoptions).
36 The `responseTime` function accepts an optional `options` object that may
37 contain any of the following keys:
41 The fixed number of digits to include in the output, which is always in
42 milliseconds, defaults to `3` (ex: `2.300ms`).
46 The name of the header to set, defaults to `X-Response-Time`.
50 Boolean to indicate if units of measurement suffix should be added to
51 the output, defaults to `true` (ex: `2.300ms` vs `2.300`).
55 Create a new middleware that records the response time of a request and
56 makes this available to your own function `fn`. The `fn` argument will be
57 invoked as `fn(req, res, time)`, where `time` is a number in milliseconds.
64 var express = require('express')
65 var responseTime = require('response-time')
69 app.use(responseTime())
71 app.get('/', function (req, res) {
72 res.send('hello, world!')
76 ### vanilla http server
79 var finalhandler = require('finalhandler')
80 var http = require('http')
81 var responseTime = require('response-time')
83 // create "middleware"
84 var _responseTime = responseTime()
86 http.createServer(function (req, res) {
87 var done = finalhandler(req, res)
88 _responseTime(req, res, function (err) {
89 if (err) return done(err)
92 res.setHeader('content-type', 'text/plain')
93 res.end('hello, world!')
98 ### response time metrics
101 var express = require('express')
102 var responseTime = require('response-time')
103 var StatsD = require('node-statsd')
106 var stats = new StatsD()
108 stats.socket.on('error', function (error) {
109 console.error(error.stack)
112 app.use(responseTime(function (req, res, time) {
113 var stat = (req.method + req.url).toLowerCase()
114 .replace(/[:\.]/g, '')
116 stats.timing(stat, time)
119 app.get('/', function (req, res) {
120 res.send('hello, world!')
128 [npm-image]: https://img.shields.io/npm/v/response-time.svg
129 [npm-url]: https://npmjs.org/package/response-time
130 [travis-image]: https://img.shields.io/travis/expressjs/response-time/master.svg
131 [travis-url]: https://travis-ci.org/expressjs/response-time
132 [coveralls-image]: https://img.shields.io/coveralls/expressjs/response-time/master.svg
133 [coveralls-url]: https://coveralls.io/r/expressjs/response-time?branch=master
134 [downloads-image]: https://img.shields.io/npm/dm/response-time.svg
135 [downloads-url]: https://npmjs.org/package/response-time
136 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
137 [gratipay-url]: https://www.gratipay.com/dougwilson/