You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
582 B

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