{"id":25725,"library":"hijackresponse","title":"hijackresponse","description":"Module for hijacking and rewriting HTTP response bodies in middleware stacks. Version 5.0.0 requires Node >=8.0.0. It is a spiritual successor to express-hijackresponse, with no direct coupling to Express and support for streams2. Main use cases are content filtering, inline script injection, and transpiler/preprocessor middleware (e.g., express-processimage, express-autoprefixer). It provides fine-grained control over response interception via a promise-based API.","status":"active","version":"5.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/gustavnikolaj/hijackresponse","tags":["javascript"],"install":[{"cmd":"npm install hijackresponse","lang":"bash","label":"npm"},{"cmd":"yarn add hijackresponse","lang":"bash","label":"yarn"},{"cmd":"pnpm add hijackresponse","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default export as a function. CommonJS require works, but ESM import is preferred for modern code.","wrong":"const hijackResponse = require('hijackresponse')","symbol":"hijackResponse","correct":"import hijackResponse from 'hijackresponse'"}],"quickstart":{"code":"import express from 'express';\nimport hijackResponse from 'hijackresponse';\n\nconst app = express();\n\napp.use((req, res, next) => {\n  hijackResponse(res, next).then(({ readable, writable, destroyAndRestore }) => {\n    // Don't hijack HTML responses:\n    if (/^text\\/html/.test(res.getHeader('Content-Type'))) {\n      return readable.pipe(writable);\n    }\n\n    res.setHeader('X-Hijacked', 'yes!');\n    res.removeHeader('Content-Length');\n\n    // Example: transform to uppercase (replace with your transform stream)\n    const transformStream = new (require('stream').Transform)({\n      transform(chunk, encoding, callback) {\n        callback(null, chunk.toString().toUpperCase());\n      }\n    });\n\n    readable.pipe(transformStream).pipe(writable);\n  });\n});\n\napp.get('/', (req, res) => {\n  res.send('hello world');\n});\n\napp.listen(3000);","lang":"javascript","description":"Demonstrates hijacking a response, conditionally skipping HTML, and piping through a transform stream."},"warnings":[{"fix":"Always call readable.pipe(writable) (possibly through a transform) in the hijack callback, or use destroyAndRestore() to restore original behavior.","message":"If you forget to pipe the readable to writable, the response will hang and never send.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Call res.removeHeader('Content-Length') before piping through a transform stream.","message":"The hijacked response's Content-Length header must be removed explicitly when modifying the body, otherwise the browser may wait for expected bytes.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Pass next as the second argument to hijackResponse, or await the promise before calling next.","message":"Do not call next() before awaiting hijackResponse resolution; otherwise the next middleware may write to the original response before hijack takes effect.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Ensure you set headers (via setHeader) only after the hijack promise resolves, and avoid direct res.write/end – always use the writable stream.","cause":"Node's HTTP response methods being called after hijack but before piping.","error":"TypeError: res._implicitHeader is not a function"},{"fix":"Do not write to res directly after hijacking; write to the writable stream only.","cause":"Writing to res after hijack has ended the writable stream or after pipe completed.","error":"Error [ERR_STREAM_WRITE_AFTER_END]: write after end"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}