Capture HTTP Response Content

1.0.3 · maintenance · verified Wed Apr 22

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.

Common errors

Warnings

Install

Imports

Quickstart

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.')
})

view raw JSON →