Git Product home page Git Product logo

response-time's Introduction

response-time

NPM Version NPM Downloads Build Status Test Coverage

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.

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install response-time

API

var responseTime = require('response-time')

responseTime([options])

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).

Options

The responseTime function accepts an optional options object that may contain any of the following keys:

digits

The fixed number of digits to include in the output, which is always in milliseconds, defaults to 3 (ex: 2.300ms).

header

The name of the header to set, defaults to X-Response-Time.

suffix

Boolean to indicate if units of measurement suffix should be added to the output, defaults to true (ex: 2.300ms vs 2.300).

responseTime(fn)

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.

Examples

express/connect

var express = require('express')
var responseTime = require('response-time')

var app = express()

app.use(responseTime())

app.get('/', function (req, res) {
  res.send('hello, world!')
})

vanilla http server

var finalhandler = require('finalhandler')
var http = require('http')
var responseTime = require('response-time')

// create "middleware"
var _responseTime = responseTime()

http.createServer(function (req, res) {
  var done = finalhandler(req, res)
  _responseTime(req, res, function (err) {
    if (err) return done(err)

    // respond to request
    res.setHeader('content-type', 'text/plain')
    res.end('hello, world!')
  })
})

response time metrics

var express = require('express')
var responseTime = require('response-time')
var StatsD = require('node-statsd')

var app = express()
var stats = new StatsD()

stats.socket.on('error', function (error) {
  console.error(error.stack)
})

app.use(responseTime(function (req, res, time) {
  var stat = (req.method + req.url).toLowerCase()
    .replace(/[:.]/g, '')
    .replace(/\//g, '_')
  stats.timing(stat, time)
}))

app.get('/', function (req, res) {
  res.send('hello, world!')
})

License

MIT

response-time's People

Contributors

dougwilson avatar erlichmen avatar jonathanong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

response-time's Issues

Warning on build for use of eval

Version: 2.3.2

(!) Use of eval is strongly discouraged
https://rollupjs.org/guide/en/#avoiding-eval
../../../node_modules/response-time/node_modules/depd/index.js
408: 
409:    // eslint-disable-next-line no-eval
410:   var deprecatedfn = eval('(function (' + args + ') {\n' +
                          ^
411:     '"use strict"\n' +
412:     'log.call(deprecate, message, site)\n' +

Add support for request-received middleware

The request-received middleware at https://github.com/cabinjs/request-received exposes a Symbol.for property, which this package could conditionally consume if interested (as opposed to just generating process.hrtime().

e.g. https://github.com/expressjs/response-time/blob/master/index.js#L53 would change from var startAt = process.hrtime() to var startAt = req[Symbol.for('request-received.startAt')] ? req[Symbol.for('request-received.startAt')] : process.hrtime()

Different Response Times

Hi,

Why is the response time of the attached header X-Response-Time different from the response time of the responseTime(req, res, time) which are both different to the response time in postman for a get request.

Expose X-Response-Time header by default

This is definitely debatable, but in my opinion, X-Response-Time header should be added to the Access-Control-Expose-Headers header. My rationale for this is that if somebody is using this package, they are explicitly asking you to expose the X-Response-Time header, therefore, you need to explicitly tell the browser that this header is safe. This can be done with one line of code:

res.header('Access-Control-Expose-Headers', 'X-Response-Time');

response-time does not contain data about url of request.

Hey,
I was using response-time as a methodology to obtain metric on all the node module accesses and the server time used by each call. In order for ease of access, the handler that processes response-time is embedded into load event handler for all XMLHTTPRequests.
However, the XMLHTTPRequests implementation lacks information about which URL the response came from, which was strange.
I'm suggesting that response-time return a JSON object like thus:
this.setHeader('X-Response-Time', '{"url":"'+req.url+'","time":"'+ ms.toFixed(digits) + '"}');
Would this be more preferable or am I approaching this the wrong way?

[RFC] timing only mode

I want to use this module to calculate the response times, but don't want to send back a header. Instead, I want to provide a callback that will be called when the response time is calculated so I can then store this in a stats db/tracker.

Thoughts?

Typescript Support

Hi,

When using the responseTime(fn) it throws some errors.

app.use(responseTime((req: Request, res: Response, time: any)

Argument of type '(req: Request, res: Response, time: any) => void' is not assignable to parameter of type 'ResponseTimeFunction'.
Types of parameters 'req' and 'request' are incompatible.
Type 'IncomingMessage' is not assignable to type 'Request'.
Property 'get' is missing in type 'IncomingMessage'.

And you cannot add a nextFunction.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.