Registry / http-networking / on-http-end

on-http-end

JSON →
library1.0.3jsnpmunverified

on-http-end is a lightweight Node.js utility designed to intercept and capture the full HTTP response payload, including headers, status code, and the final data, just before the response stream concludes. Inspired by `apicache`, it offers a simple, callback-based API to register a function that executes when `res.end()` is called. This allows developers to inspect or process the complete response content and metadata, making it suitable for logging, debugging, or custom caching mechanisms. The current stable version is 1.0.3, which includes a fix for proper scope handling, allowing multiple `onEnd` registrations on a single response object. Its release cadence appears infrequent, reflecting its focused scope and stable API. A key differentiator is its direct, low-level integration with Node.js's native `http.ServerResponse` object, providing granular control without the overhead of a full middleware framework.

npm install on-http-end
INSTALL
IMPORT
SIG · ON-HTTP-END
O
on-http-end
http-networkingjavascriptv1.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

onEnd
const onEnd = require('on-http-end')
import onEnd from 'on-http-end'
The package primarily uses CommonJS `require()` syntax as shown in its documentation. For modern Node.js environments configured for ESM, use the `import` statement.

Demonstrates how to integrate `on-http-end` with a basic Node.js HTTP server to capture response details.

const onEnd = require('on-http-end') const http = require('http') const server = http.createServer((req, res) => { onEnd(res, (payload) => { // The payload object contains status, headers, data, and encoding console.log('Captured Response Payload:', payload) // Example: Log only the data // console.log('Response data:', payload.data) }) res.setHeader('x-custom-header', 'captured-value') res.statusCode = 201; res.end('Hello, on-http-end user!', 'utf-8') }) server.listen(3000, () => { console.log('Server listening on http://localhost:3000') console.log('Try visiting http://localhost:3000 in your browser.') })
Debug
Known issues
gotchaPrior to `v1.0.3`, registering `on-http-end` multiple times on the same `http.ServerResponse` object could lead to previous registrations being overwritten or unexpected behavior. This was fixed to properly allow multiple callbacks.
fix
Upgrade to `on-http-end@1.0.3` or newer to ensure all registered `onEnd` callbacks execute reliably when `res.end()` is called.
affects: <1.0.3
gotchaThe `onEnd` callback is executed *after* `res.end()` has been called. Attempting to modify headers or the status code within this callback will have no effect on the client response, as headers would have already been sent.
fix
Ensure all modifications to `res.statusCode` and `res.setHeader()` occur *before* `res.end()` is invoked. The `onEnd` callback is for *observing* the final response, not modifying it.
affects: >=1.0.0
gotchaCapturing the entire response body in memory, especially for very large responses, can lead to significant memory consumption. This can be a concern for high-traffic servers or services handling multi-megabyte payloads.
fix
For applications dealing with potentially very large responses, consider alternative streaming approaches or ensure proper resource management and scaling strategies if using `on-http-end`.
affects: >=1.0.0
gotchaWhile `on-http-end` works directly with Node.js's native `http` module, its integration with higher-level web frameworks (e.g., Express, Koa, Fastify) might require careful attention. These frameworks often wrap or modify the `res.end()` method, which could impact when or how `on-http-end` intercepts the response.
fix
Test `on-http-end` thoroughly within your chosen framework. Ensure it's registered early in the middleware chain to properly capture the response before framework-specific `res.end()` modifications take effect.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: onEnd is not a function
Attempting to import `on-http-end` using an incorrect syntax for the current module context (e.g., ESM `import` in a CommonJS file or vice-versa), or a typo in the symbol name.
fix
For CommonJS (most Node.js projects without 'type: module' in package.json): `const onEnd = require('on-http-end')`. For ES Modules: `import onEnd from 'on-http-end'`.
Error: Can't set headers after they are sent.
This error occurs when you attempt to modify HTTP headers or the status code within the `onEnd` callback or after `res.end()` has already been called elsewhere.
fix
All header and status code modifications must occur *before* `res.end()` is called. The `onEnd` callback is purely for observing the final response data and metadata, not for altering it.
onEnd callback not firing or payload.data is empty/missing.
The `onEnd` callback was registered *after* `res.end()` was called, or `res.end()` was never called on the response object.
fix
Ensure that `onEnd(res, callback)` is invoked *before* `res.end()` is called in your request handler. Verify that `res.end()` is indeed being called to terminate the response.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
on-http-end — npm install on-http-end · libregistry