npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lum_request-stats

v3.2.0

Published

Get stats on your Node.js HTTP server requests

Downloads

37,403

Readme

request-stats

Build status js-standard-style

Get stats on your Node.js HTTP server requests.

Emits two events:

  • request when ever a request starts: Passes a Request object that can later be used to query for the progress of a long running request
  • complete when ever a request completes: Passes a stats object containing the overall stats for the entire HTTP request

Installation

npm install request-stats --save

Example usage

Get stats for each completed HTTP request:

var requestStats = require('request-stats')
var server = http.createServer(...)

requestStats(server, function (stats) {
  // this function will be called every time a request to the server completes
  console.log(stats)
})

Get periodic stats for long running requests:

var server = http.createServer(...)

var stats = requestStats(server)

stats.on('request', function (req) {
  // evey second, print stats
  var interval = setInterval(function () {
    var progress = req.progress()
    console.log(progress)
    if (progress.completed) clearInterval(interval)
  }, 1000)
})

API

Constructor

requestStats(server[, callback])

Attach request-stats to a HTTP server.

Initialize request-stats with an instance a HTTP server. Returns a StatsEmitter object. Optionally provide a callback which will be called for each completed HTTP request with a stats object (see stats object details below).

If no callback is provided, you can later attach a listener on the "complete" event.

requestStats(req, res[, callback])

Attach request-stats to a single HTTP request.

Initialize request-stats with an instance a HTTP request and response. Returns a StatsEmitter object. Optionally provide a callback which will be called with a stats object when the HTTP request completes (see stats object details below).

If no callback is provided, you can later attach a listener on the "complete" event.

StatsEmitter object

.on('complete', callback)

Calls the callback function with a stats object when a HTTP request completes:

{
  ok: true,           // `true` if the connection was closed correctly and `false` otherwise
  time: 0,            // The milliseconds it took to serve the request
  req: {
    bytes: 0,         // Number of bytes sent by the client
    headers: { ... }, // The headers sent by the client
    method: 'POST',   // The HTTP method used by the client
    path: '...',      // The path part of the request URL
    ip: '...',        // The remote ip
    raw: [Object]     // The original `http.IncomingMessage` object
  },
  res: {
    bytes: 0,         // Number of bytes sent back to the client
    headers: { ... }, // The headers sent back to the client
    status: 200,      // The HTTP status code returned to the client
    raw: [Object]     // The original `http.ServerResponse` object
  }
}

.on('request', callback)

Calls the callback function with a special Request object when a HTTP request is made to the server.

Request object

The Request object should not be confused with the Node.js http.IncomingMessage object. The request-stats Request object provides only a single but powerfull function:

.progress()

Returns a progress object if called while a HTTP request is in progress. If called multiple times, the returned progress object will contain the delta of the previous call to .progress().

{
  completed: false, // `false` if the request is still in progress
  time: 0,          // The total time the reuqest have been in progress
  timeDelta: 0,     // The time since previous call to .progress()
  req: {
    bytes: 0,       // Total bytes received
    bytesDelta: 0,  // Bytes received since previous call to .progress()
    speed: 0,       // Bytes per second calculated since previous call to .progress()
    bytesLeft: 0,   // If the request contains a Content-Size header
    timeLeft: 0     // If the request contains a Content-Size header
  },
  res: {
    bytes: 0,       // Total bytes send back to the client
    bytesDelta: 0,  // Bytes sent back to the client since previous call to .progress()
    speed: 0        // Bytes per second calculated since previous call to .progress()
  }
}

Acknowledgement

Thanks to mafintosh for coming up with the initial concept and pointing me in the right direction.

License

MIT