Response time for Node.js servers.
This module creates a middleware that records the response time for requests in HTTP servers. The "response time" is defined here as the elapsed time from when a request enters this middleware to when the headers are written out to the client.
This is a Node.js module available through the
npm registry. Installation is done using the
npm install command:
$ npm install response-timevarresponseTime=require('response-time')Create a middleware that adds a X-Response-Time header to responses. If
you don't want to use this module to automatically set a header, please
see the section about responseTime(fn).
The responseTime function accepts an optional options object that may
contain any of the following keys:
The fixed number of digits to include in the output, which is always in
milliseconds, defaults to 3 (ex: 2.300ms).
The name of the header to set, defaults to X-Response-Time.
Boolean to indicate if units of measurement suffix should be added to
the output, defaults to true (ex: 2.300ms vs 2.300).
Create a new middleware that records the response time of a request and
makes this available to your own function fn. The fn argument will be
invoked as fn(req, res, time), where time is a number in milliseconds.
varexpress=require('express')varresponseTime=require('response-time')varapp=express()app.use(responseTime())app.get('/',function(req,res){res.send('hello, world!')})varfinalhandler=require('finalhandler')varhttp=require('http')varresponseTime=require('response-time')// create "middleware"var_responseTime=responseTime()http.createServer(function(req,res){vardone=finalhandler(req,res)_responseTime(req,res,function(err){if(err)returndone(err)// respond to requestres.setHeader('content-type','text/plain')res.end('hello, world!')})})varexpress=require('express')varresponseTime=require('response-time')varStatsD=require('node-statsd')varapp=express()varstats=newStatsD()stats.socket.on('error',function(error){console.error(error.stack)})app.use(responseTime(function(req,res,time){varstat=(req.method+req.url).toLowerCase().replace(/[:.]/g,'').replace(/\//g,'_')stats.timing(stat,time)}))app.get('/',function(req,res){res.send('hello, world!')})