Created
April 18, 2013 21:37
-
-
Save BenHall/5416437 to your computer and use it in GitHub Desktop.
Return the response time as a HTTP header in NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var time = require('./response-time'); | |
app.configure(function(){ | |
app.use(time()); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* Connect - responseTime | |
* Copyright(c) 2011 TJ Holowaychuk | |
* MIT Licensed | |
*/ | |
module.exports = function responseTime(){ | |
return function(req, res, next){ | |
var start = new Date(); | |
if (res._responseTime) return next(); | |
res._responseTime = true; | |
res.on('header', function(){ | |
var duration = new Date() - start; | |
res.setHeader('X-Response-Time', duration + 'ms'); | |
}); | |
next(); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment