2.3.3 • Published 9 months ago

response-time v2.3.3

Weekly downloads
256,605
License
MIT
Repository
github
Last release
9 months ago

response-time

NPM Version NPM Downloads Node.js Version 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

anything-send-to-chatmaxwin_est_timemaxwin_realtime_webhikeon-market-utils-2docker-layers-cacherectified-iocapi-gateway-coreapi-gateway-packsupply-chain-webapps-k8s-npm-packageclickster-commoninternational-portal-commonregulondb-apimaxwin_est_serveracr-coreac-api-frameworkekko-express-prometheusekko-middleware-promekko-middleware-prometheus@minta/metrics@firstblood/nestjs-monitoring@adamelliotfields/logapfm-rules-engine@verikhuyha/node-commonsappchoose-nextjsfission-serverfission-server-jspolka-prometheus-middlewarenodewebx-serveroase-facadehulionjsapi-cataloguesthing-it-serverintervu-shared@everything-registry/sub-chunk-2658@kodermax/orange-corenamaste-betcg-loggerwolves-utilwettywetty-bunkaiwildspiderrest-on-sqlsuper-servertelen-apistaart-managerspine-old-deprecatedsuper-bootstrapthink-response-timev2-runtime-apiverdandi@composer-js/service-core@denali-js/core@double-mangoes/dlob-server@pct-org/pop-apijinbeijimpick-dathttpdjagtesterjotunheimriot-edgejsongle-serverlaas-admin-api@tamanh/common-nestjs@aarondewes/anonlytics-expressstructured-loggingstructured-logging-nextstellar-sandboxteservertesseraunleash-serverunleash-server-mongounleash-server-openapiusey-http@apifie/node-microservice@asapzacy/uxscoreboard@aqsi/scripts@arzooo/express-quick-server@atlantis-of-code/aoc-server@aux4/api@akiroz/server@akjs/core@beaker/homebase@afsalk/nestjs-prom@bloomreach/graphql-commerce-connector-service@bzznbyd/lib@ccondry/hydra-express@cheevr/server@ciscospark/sparkd@clearmetrics/server@chengchengw/node-service@clocklimited/cf-api2ex@abgov/adsp-service-sdk@aantonov/redis-omapper@abacollection/web@ddn/server@digikare/nestjs-prom@digifi/periodicjsyj-npmxx-serveryog2-kernel
2.3.3

9 months ago

2.3.2

9 years ago

2.3.1

10 years ago

2.3.0

10 years ago

2.2.0

11 years ago

2.1.0

11 years ago

2.0.1

11 years ago

2.0.0

11 years ago

1.0.0

11 years ago