xonecas.io
142
St

startsInvasion / files

startsInvasion / node_modules / connect / lib / middleware / responseTime.js

725 bytes
1
2/*!
3 * Connect - responseTime
4 * Copyright(c) 2011 TJ Holowaychuk
5 * MIT Licensed
6 */
7
8/**
9 * Adds the `X-Response-Time` header displaying the response
10 * duration in milliseconds.
11 *
12 * @return {Function}
13 * @api public
14 */
15
16module.exports = function responseTime(){
17  return function(req, res, next){
18    var writeHead = res.writeHead
19      , start = new Date;
20
21    if (res._responseTime) return next();
22    res._responseTime = true;
23
24    // proxy writeHead to calculate duration
25    res.writeHead = function(status, headers){
26      var duration = new Date - start;
27      res.setHeader('X-Response-Time', duration + 'ms');
28      res.writeHead = writeHead;
29      res.writeHead(status, headers);
30    };
31
32    next();
33  };
34};