{"id":13939,"library":"request-stats","title":"Node.js HTTP Request Statistics","description":"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.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/watson/request-stats","tags":["javascript","stats","statistics","http","https","middleware","connect","express","analytics"],"install":[{"cmd":"npm install request-stats","lang":"bash","label":"npm"},{"cmd":"yarn add request-stats","lang":"bash","label":"yarn"},{"cmd":"pnpm add request-stats","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"CommonJS (CJS) syntax, primary usage pattern demonstrated in examples.","symbol":"requestStats","correct":"const requestStats = require('request-stats')"},{"note":"For ESM projects, this typically works due to CJS interop, as 'request-stats' exports a default function. Avoid named imports.","wrong":"import { requestStats } from 'request-stats'","symbol":"requestStats","correct":"import requestStats from 'request-stats'"},{"note":"The StatsEmitter object is returned by calling the `requestStats` function, not directly imported.","symbol":"StatsEmitter","correct":"const statsEmitter = requestStats(server)"}],"quickstart":{"code":"const requestStats = require('request-stats')\nconst http = require('http')\n\nconst server = http.createServer(function (req, res) {\n  res.writeHead(200, { 'Content-Type': 'text/plain' })\n  res.end('Hello World\\n')\n})\n\nrequestStats(server, function (stats) {\n  console.log('Request complete:')\n  console.log(`  Method: ${stats.req.method}`)\n  console.log(`  Path: ${stats.req.path}`)\n  console.log(`  Status: ${stats.res.status}`)\n  console.log(`  Duration: ${stats.time}ms`)\n  console.log(`  Bytes In: ${stats.req.bytes}, Bytes Out: ${stats.res.bytes}`)\n  console.log(`  Connection OK: ${stats.ok}`)\n})\n\nserver.listen(3000, function () {\n  console.log('Server listening on http://localhost:3000')\n  console.log('Test with: curl http://localhost:3000')\n})","lang":"javascript","description":"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."},"warnings":[{"fix":"Ensure compatibility by testing `request-stats` thoroughly with your specific Node.js runtime version, especially if using newer Node.js features or modules.","message":"The `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.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Use the default import syntax for ESM: `import requestStats from 'request-stats'` or stick to CommonJS `require('request-stats')`.","message":"When 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.","severity":"gotcha","affected_versions":">=3.0.0"},{"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.","message":"Continuously 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.","severity":"gotcha","affected_versions":">=3.0.0"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":">=3.0.0"}],"env_vars":null,"search_vec":"'3.0.0':69 'address':50 'allow':123 'analyt':187 'attach':165 'avail':118 'back':38 'begin':82 'beyond':148 'byte':31,36 'capabl':146 'client':35,41 'code':55 'collect':14 'complet':93,151 'comprehens':103 'conclud':99 'connect':185 'current':60 'deeper':154 'deliv':101 'delta':140 'design':12 'detail':15 'develop':124 'differenti':108 'durat':30 'emit':72 'entir':168 'event':75 'express':186 'fire':78,95 'flight':159 'goe':147 'header':43 'http':2,18,44,53,182 'http.incomingmessage':175 'http.server':170 'http.serverresponse':177 'https':183 'in-flight':157 'includ':138 'increment':139 'individu':174 'inform':67 'insight':155 'instanc':171 'ip':49 'javascript':179 'key':107 'last':143 'librari':11,163 'long':135 'long-run':134 'method':45,117 'metric':25 'middlewar':184 'monitor':90 'node.js':1,10,169 'object':88,105,122 'offer':153 'packag':71 'pair':178 'path':47 'perform':161 'period':126 'primari':74 'progress':91,116 'provid':66,84 'queri':127,144 'real':130 'real-tim':129 'remot':48 'request':3,6,20,29,42,46,76,81,87,98,111,121,137,150,160 'request-stat':5,110 'respons':22,58 'run':136 'sent':32,37 'server':19 'simpl':149 'sinc':141 'special':86 'stabl':61 'stat':7,104,112,180 'statist':4,16,132,181 'status':54 'support':164 'time':131 'total':28 'track':24,152 'two':73 'uniqu':115 'version':62","created_at":"2026-04-20T01:57:06.085958+00:00","updated_at":"2026-04-20T01:57:06.085958+00:00","problems":[{"fix":"Change your import statement to `import requestStats from 'request-stats'` for ESM, or use `const requestStats = require('request-stats')` for CommonJS.","cause":"Attempting to use `import { requestStats } from 'request-stats'` in an ESM module, when the package exports a default function via CommonJS.","error":"TypeError: requestStats is not a function"},{"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.","cause":"The package was not installed, or there's a problem with module resolution paths (e.g., `node_modules` not found in the expected location).","error":"Error: Cannot find module 'request-stats'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/watson/request-stats","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/request-stats","openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","observability"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}