{"id":18516,"library":"lum_request-stats","title":"request-stats","description":"request-stats is a Node.js library that collects HTTP request statistics for Node.js HTTP servers. It emits events for request start and completion, providing metrics such as bytes transferred, duration, headers, method, path, IP, and status code. It also offers a progress() method for long-running requests. Version 3.2.0 is the latest stable release; the project appears to be in maintenance mode with no recent updates (last commit several years ago). Key differentiators: simple API, event-based, supports both per-request and per-server stats, and works with plain HTTP servers, Express, and other frameworks.","status":"maintenance","version":"3.2.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 lum_request-stats","lang":"bash","label":"npm"},{"cmd":"yarn add lum_request-stats","lang":"bash","label":"yarn"},{"cmd":"pnpm add lum_request-stats","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is CommonJS only, so require() is correct; the ESM import shown is a polyfill for environments that support it.","wrong":"const requestStats = require('request-stats')","symbol":"requestStats (default)","correct":"import requestStats from 'request-stats'"},{"note":"The package exports a single function as default. Named import will fail.","wrong":"import { requestStats } from 'request-stats'","symbol":"requestStats (as function)","correct":"const requestStats = require('request-stats')"},{"note":"The package does not ship TypeScript definitions; you may need to install @types/request-stats or define your own.","wrong":"import { StatsEmitter } from 'request-stats'","symbol":"StatsEmitter type (TypeScript)","correct":"import requestStats from 'request-stats'; const stats: requestStats.StatsEmitter = requestStats(server)"}],"quickstart":{"code":"const http = require('http');\nconst requestStats = require('request-stats');\n\nconst server = http.createServer((req, res) => {\n  res.statusCode = 200;\n  res.end('Hello World');\n});\n\nrequestStats(server, (stats) => {\n  console.log(`Request completed in ${stats.time}ms`);\n  console.log(`Status: ${stats.res.status}`);\n  console.log(`Bytes sent: ${stats.res.bytes}`);\n});\n\nserver.listen(3000);","lang":"javascript","description":"Attaches request-stats to an HTTP server and logs stats for each completed request."},"warnings":[{"fix":"If you need cumulative values, accumulate manually or call .progress() only once per interval.","message":"The .progress() method returns delta values on subsequent calls, not cumulative totals.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Access the raw request via the 'raw' property on the Request object.","message":"The 'request' event emits a Request object, not Node.js's http.IncomingMessage.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Upgrade to v3 or manually parse query/body from the raw request.","message":"Version 2.0 changed the stats object structure: removed 'req.query' and 'req.body'.","severity":"breaking","affected_versions":"=2.0.0"},{"fix":"Test with your Node version; consider using alternatives like 'express-http-context' or custom middleware.","message":"The package has not been updated for Node.js >=14. Some internal HTTP APIs may behave differently.","severity":"deprecated","affected_versions":">=3.0.0"},{"fix":"Use the server-wide attachment for multiple requests.","message":"When attaching to a single request with requestStats(req, res), the callback is invoked once; attempting to attach again will overwrite listeners.","severity":"gotcha","affected_versions":">=1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use const requestStats = require('request-stats');","cause":"Using named import instead of default import with CommonJS.","error":"TypeError: requestStats is not a function"},{"fix":"Provide either a server object or a request/response pair as the first argument.","cause":"Calling requestStats() without a server or req/res object.","error":"Cannot read properties of undefined (reading 'on')"},{"fix":"Access stats object only inside the callback or 'complete' event listener.","cause":"Attempting to access headers before the 'complete' event fires.","error":"stats.req.headers is undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}