Registry / http-networking / request-stats

request-stats

JSON →
library3.0.0jsnpmunverified

request-stats is a Node.js library designed to collect detailed statistics on HTTP server requests and responses. It tracks metrics such as total request duration, bytes sent by the client, bytes sent back to the client, request headers, HTTP method, request path, remote IP address, and the HTTP status code of the response. The current stable version, as of the provided information, is 3.0.0. The package emits two primary events: `request`, which fires when a request begins and provides a special `Request` object for monitoring progress, and `complete`, which fires when a request concludes and delivers a comprehensive `stats` object. A key differentiator of `request-stats` is its unique `.progress()` method available on the `Request` object, allowing developers to periodically query for real-time statistics of long-running requests, including incremental deltas since the last query. This capability goes beyond simple request completion tracking, offering deeper insights into in-flight request performance. The library supports attaching to an entire Node.js `http.Server` instance or to individual `http.IncomingMessage` and `http.ServerResponse` pairs.

npm install request-stats
INSTALL
IMPORT
SIG · REQUEST-STATS
R
request-stats
http-networkingjavascriptv3.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

requestStats
✓ const requestStats = require('request-stats')
CommonJS (CJS) syntax, primary usage pattern demonstrated in examples.
requestStats
✓ import requestStats from 'request-stats'
✗ import { requestStats } from 'request-stats'
For ESM projects, this typically works due to CJS interop, as 'request-stats' exports a default function. Avoid named imports.
StatsEmitter
✓ const statsEmitter = requestStats(server)
The StatsEmitter object is returned by calling the `requestStats` function, not directly imported.

This example initializes `request-stats` on a basic Node.js HTTP server, logging detailed statistics for each completed request, including method, path, status, duration, byte counts, and connection status.

const requestStats = require('request-stats') const http = require('http') const server = http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }) res.end('Hello World\n') }) requestStats(server, function (stats) { console.log('Request complete:') console.log(` Method: ${stats.req.method}`) console.log(` Path: ${stats.req.path}`) console.log(` Status: ${stats.res.status}`) console.log(` Duration: ${stats.time}ms`) console.log(` Bytes In: ${stats.req.bytes}, Bytes Out: ${stats.res.bytes}`) console.log(` Connection OK: ${stats.ok}`) }) server.listen(3000, function () { console.log('Server listening on http://localhost:3000') console.log('Test with: curl http://localhost:3000') })
Debug
Known issues
gotchaThe `engines` field specifies Node.js `>=0.12`, which is a very old version. While the package might still function, it indicates it may not be actively tested against or optimized for modern Node.js features and breaking changes in recent Node.js major versions.
fix
Ensure compatibility by testing `request-stats` thoroughly with your specific Node.js runtime version, especially if using newer Node.js features or modules.
affects: >=3.0.0
gotchaWhen using ESM (`import`) in Node.js, directly importing named exports like `import { requestStats } from 'request-stats'` will likely fail. The package primarily exposes a default CommonJS function.
fix
Use the default import syntax for ESM: `import requestStats from 'request-stats'` or stick to CommonJS `require('request-stats')`.
affects: >=3.0.0
gotchaContinuously polling `req.progress()` for many concurrent, long-running requests, especially at very high frequencies (e.g., every few milliseconds), can introduce measurable overhead due to frequent object creation and calculations. This method is intended for periodic updates.
fix
Call `req.progress()` at reasonable intervals (e.g., every second or few seconds) to monitor long-running requests without excessive performance impact. Use it judiciously based on your application's profiling.
affects: >=3.0.0
gotchaThe `complete` event's `stats` object includes an `ok` property, which is `true` if the connection was closed correctly and `false` otherwise (e.g., due to client disconnect or server error before completion). Developers often overlook this property.
fix
Always check `stats.ok` in your `complete` event listener to properly categorize and handle requests that did not finish gracefully, distinguishing them from successfully completed ones.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: requestStats is not a function
Attempting to use `import { requestStats } from 'request-stats'` in an ESM module, when the package exports a default function via CommonJS.
fix
Change your import statement to `import requestStats from 'request-stats'` for ESM, or use `const requestStats = require('request-stats')` for CommonJS.
Error: Cannot find module 'request-stats'
The package was not installed, or there's a problem with module resolution paths (e.g., `node_modules` not found in the expected location).
fix
Run `npm install request-stats` or `yarn add request-stats` in your project directory. Verify your `NODE_PATH` or module resolution configuration if the issue persists after installation.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
20
OpenAI (training)
2
Resources