{"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install request-stats"],"cli":null},"imports":["const requestStats = require('request-stats')","import requestStats from 'request-stats'","const statsEmitter = requestStats(server)"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}