{"id":17315,"library":"on-http-end","title":"Capture HTTP Response Content","description":"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.","status":"maintenance","version":"1.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/jkyberneees/on-http-end","tags":["javascript","http","response","content","headers"],"install":[{"cmd":"npm install on-http-end","lang":"bash","label":"npm"},{"cmd":"yarn add on-http-end","lang":"bash","label":"yarn"},{"cmd":"pnpm add on-http-end","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package primarily uses CommonJS `require()` syntax as shown in its documentation. For modern Node.js environments configured for ESM, use the `import` statement.","wrong":"import onEnd from 'on-http-end'","symbol":"onEnd","correct":"const onEnd = require('on-http-end')"}],"quickstart":{"code":"const onEnd = require('on-http-end')\nconst http = require('http')\n\nconst server = http.createServer((req, res) => {\n  onEnd(res, (payload) => {\n    // The payload object contains status, headers, data, and encoding\n    console.log('Captured Response Payload:', payload)\n    // Example: Log only the data\n    // console.log('Response data:', payload.data)\n  })\n\n  res.setHeader('x-custom-header', 'captured-value')\n  res.statusCode = 201;\n  res.end('Hello, on-http-end user!', 'utf-8')\n})\n\nserver.listen(3000, () => {\n  console.log('Server listening on http://localhost:3000')\n  console.log('Try visiting http://localhost:3000 in your browser.')\n})","lang":"javascript","description":"Demonstrates how to integrate `on-http-end` with a basic Node.js HTTP server to capture response details."},"warnings":[{"fix":"Upgrade to `on-http-end@1.0.3` or newer to ensure all registered `onEnd` callbacks execute reliably when `res.end()` is called.","message":"Prior 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.","severity":"gotcha","affected_versions":"<1.0.3"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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`.","message":"Capturing 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"While `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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"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'`.","cause":"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.","error":"TypeError: onEnd is not a function"},{"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.","cause":"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.","error":"Error: Can't set headers after they are sent."},{"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.","cause":"The `onEnd` callback was registered *after* `res.end()` was called, or `res.end()` was never called on the response object.","error":"onEnd callback not firing or payload.data is empty/missing."}],"ecosystem":"npm","meta_description":null}