connect-middleware-response-time

1.0.0 • Public • Published

Response Time

NPM version Build Status Coverage Status Dependencies

Connect middleware to calculate response times.

Similar to response-time, except this implementation does not set a response header for the calculated time and does not override the default response.writeHead method.

The calculated response time is output to a callback provided on initialization. Accordingly, the module is better suited for logging response times and monitoring application performance.

Installation

$ npm install connect-middleware-response-time

Usage

To use the module,

var responseTime = require( 'connect-middleware-response-time' );

responseTime( clbk )

The middleware generator accepts a callback which should accept a numeric value as its only input.

var mw = responseTime( onTime );
 
function onTime( value ) {
    console.log( 'Response time: %s ms', value );
}

Notes

In the event that an HTTP response terminates prematurely, the returned response time is NaN.

Examples

var express = require( 'express' ),
    request = require( 'request' ),
    responseTime = require( 'connect-middleware-response-time' );
 
// Define a port:
var PORT = 7331;
 
// Create a new application:
var app = express();
 
// Use the response time middleware for all requests:
app.use( responseTime( logger ) );
 
// Bind a route:
app.get( '/', [ compute, send ] );
 
// Spin up a new server:
var server = app.listen( PORT, onListen );
 
 
function onListen() {
    request({
        'method': 'GET',
        'uri': 'http://127.0.0.1:' + PORT
    }, onResponse );
}
 
function onResponse( error, response, body ) {
    if ( error ) {
        throw new Error( error );
    }
    console.log( body );
}
 
function compute( request, response, next ) {
    var rand = Math.floor( Math.random()*2e6 ),
        idx = 0,
        x;
    for ( var i = 0; i < rand; i++ ) {
        x = Math.sqrt( ++idx );
    }
    next();
}
 
function logger( value ) {
    console.log( 'Response time: %s ms', value );
}
 
function send( request, response, next ) {
    response
        .status( 200 )
        .send( '...computation finished...' );
}

To run an example from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.


Copyright

Copyright © 2014. Athan Reines.

Package Sidebar

Install

npm i connect-middleware-response-time

Weekly Downloads

6

Version

1.0.0

License

none

Last publish

Collaborators

  • kgryte