forked from expressjs/response-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (36 loc) · 875 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*!
* response-time
* Copyright(c) 2011 TJ Holowaychuk
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2014 Douglas Christopher Wilson
* MIT Licensed
*/
/**
* Module dependencies
*/
var onHeaders = require('on-headers')
/**
* Reponse time:
*
* Adds the `X-Response-Time` header displaying the response
* duration in milliseconds.
*
* @param {number} [digits=3]
* @return {function}
* @api public
*/
module.exports = function responseTime(digits) {
digits = digits === undefined
? 3
: digits
return function responseTime(req, res, next) {
var startAt = process.hrtime()
onHeaders(res, function () {
if (this.getHeader('X-Response-Time')) return;
var diff = process.hrtime(startAt)
var ms = diff[0] * 1e3 + diff[1] * 1e-6
this.setHeader('X-Response-Time', ms.toFixed(digits) + 'ms')
})
next()
}
}