$ show expressjs/response-time
response-time is a minimal, high-quality Node.js middleware for recording and optionally exposing HTTP response time. It uses `process.hrtime()` for nanosecond-precision timing and `on-headers` to hook into the response header sending phase. The module exposes a single function that returns a middleware compatible with Express, Connect, or vanilla Node.js HTTP servers. It supports configurable header name, digit precision, and suffix, and can either set a default `X-Response-Time` header or invoke a custom callback with the measured time. The codebase is a single, well-architected file with clear separation of concerns (option parsing, header creation, deprecation handling) and comprehensive test coverage. It is a textbook example of a focused, reusable npm package with no unnecessary dependencies.
The LLM identified 1 files worth highlighting for reuse:
The entire module implementation. It exports a function `responseTime` that accepts options (object or number) or a custom callback. It returns a middleware that records the start time via `process.hrtime`, registers an `on-headers` callback, and on header send calculates the elapsed time and either sets the `X-Response-Time` header or passes the time to a user-provided function. The internal `createSetHeader` function encapsulates header formatting (digits, suffix). The code demonstrates clean separation, deprecation management, and use of high-resolution timers. It is fully self-contained and can be extracted as-is.