| 1 | | /*! |
| 2 | | * connect-rt - lib/connect-rt.js |
| 3 | | * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com> |
| 4 | | * MIT Licensed |
| 5 | | */ |
| 6 | | |
| 7 | 1 | "use strict"; |
| 8 | | |
| 9 | | /** |
| 10 | | * Module dependencies. |
| 11 | | */ |
| 12 | | |
| 13 | 1 | var microtime = require('microtime'); |
| 14 | | |
| 15 | | /** |
| 16 | | * Reponse time: |
| 17 | | * |
| 18 | | * Adds the `X-Response-Time` header displaying the response |
| 19 | | * duration in milliseconds. |
| 20 | | * |
| 21 | | * @see https://github.com/senchalabs/connect/blob/master/lib/middleware/responseTime.js |
| 22 | | * @return {Function(req, res, next)} |
| 23 | | * @api public |
| 24 | | */ |
| 25 | | |
| 26 | 1 | module.exports = function responseTime() { |
| 27 | 2 | return function (req, res, next) { |
| 28 | 2 | var start = microtime.now(); |
| 29 | | |
| 30 | 2 | if (res._responseTime) { |
| 31 | 1 | return next(); |
| 32 | | } |
| 33 | 1 | res._responseTime = true; |
| 34 | | |
| 35 | 1 | res.on('header', function () { |
| 36 | 1 | var duration = microtime.now() - start; |
| 37 | 1 | res.setHeader('X-Response-Time', (duration / 1000) + 'ms'); |
| 38 | | }); |
| 39 | | |
| 40 | 1 | next(); |
| 41 | | }; |
| 42 | | }; |