Registry / http-networking / http-proxy-response-rewrite

http-proxy-response-rewrite

JSON →
library0.0.1jsnpmunverified

http-proxy-response-rewrite is a Node.js library designed to simplify the modification of response bodies when using `http-proxy`. Its core functionality involves transparently handling common compression formats like `gzip` and `deflate` to decompress the response body, allowing developers to manipulate the plain text or JSON content, and then re-compress it before sending it to the client. The library is currently at version 0.0.1 and has not been updated in approximately 8 years, indicating it is no longer actively maintained. Its primary differentiator was providing a streamlined way to intercept and modify compressed `http-proxy` responses, a task that would otherwise require manual zlib handling. Due to its abandoned status, developers should consider more modern and actively maintained alternatives for proxy response manipulation.

npm install http-proxy-response-rewrite
INSTALL
IMPORT
SIG · HTTP-PROXY-RESPONS
H
http-proxy-response-rewrite
http-networkingjavascriptv0.0.1
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.

modifyResponse
import modifyResponse from 'http-proxy-response-rewrite';
const modifyResponse = require('http-proxy-response-rewrite/index.js');
While the examples use `require('../')`, for an installed package, the correct CommonJS import is `require('http-proxy-response-rewrite')`. For modern Node.js environments supporting ESM, use the `import` statement.

This quickstart demonstrates setting up an HTTP proxy to intercept and modify a gzipped JSON response from a target server. It parses the body, changes an attribute, deletes another, and then re-serializes and re-compresses the response.

const zlib = require('zlib'); const http = require('http'); const httpProxy = require('http-proxy'); const modifyResponse = require('http-proxy-response-rewrite'); // Create a proxy server const proxy = httpProxy.createProxyServer({ target: 'http://localhost:5001' }); // Listen for the `proxyRes` event on `proxy` to modify responses proxy.on('proxyRes', function (proxyRes, req, res) { modifyResponse(res, proxyRes.headers['content-encoding'], function (body) { if (body) { // Modify the response body (e.g., JSON manipulation) let modifiedBody = JSON.parse(body); modifiedBody.age = 2; delete modifiedBody.version; return JSON.stringify(modifiedBody); } return body; }); }); // Create your server and then proxies the request const server = http.createServer(function (req, res) { proxy.web(req, res); }).listen(5000, () => console.log('Proxy server listening on port 5000')); // Create a target server with gzipped content const targetServer = http.createServer(function (req, res) { const gzip = zlib.createGzip(); const _write = res.write; const _end = res.end; gzip.on('data', function (buf) { _write.call(res, buf); }); gzip.on('end', function () { _end.call(res); }); res.write = function (data) { gzip.write(data); }; res.end = function () { gzip.end(); }; res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'gzip'}); res.write(JSON.stringify({name: 'http-proxy-json', age: 1, version: '1.0.0'})); res.end(); }).listen(5001, () => console.log('Target server listening on port 5001'));
Debug
Known issues
deprecatedThe package `http-proxy-response-rewrite` is effectively abandoned. It has not received updates in approximately 8 years and is stuck at version 0.0.1. It is not recommended for new projects and existing projects should seek alternatives for security and maintenance reasons.
fix
Consider using newer and actively maintained proxy middleware solutions like `http-proxy-middleware` which offer response interception and manipulation capabilities, often with better support for various compression types and modern JavaScript features.
affects: 0.0.1
gotchaThis library only supports `gzip`, `deflate`, and uncompressed response bodies for modification. If the upstream server uses other compression formats (e.g., Brotli), this library will not be able to decompress and modify the response, potentially leading to corrupt or unhandled data.
fix
Ensure your upstream server's `Content-Encoding` header matches one of the supported formats. For unsupported formats, you would need to implement custom decompression logic or use an alternative library with broader support. Modern alternatives like `http-proxy-middleware` often include Brotli support.
affects: 0.0.1
gotchaThe `modifyResponse` callback expects a string or null return. If the callback function returns an `undefined` value, the `http-proxy` might send an empty or incomplete response to the client, leading to unexpected behavior in client-side applications.
fix
Always ensure the callback function passed to `modifyResponse` explicitly returns the modified body (as a string) or the original `body` if no modification is needed. Return `null` only if you intend to send an empty response body.
affects: 0.0.1
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'on') or similar errors related to `gzip` or `deflate`.
This error often occurs when the `Content-Encoding` header from the proxied response is not 'gzip' or 'deflate', and the `modifyResponse` function tries to initialize a zlib stream for an unsupported or missing encoding.
fix
Verify that the `proxyRes.headers['content-encoding']` is correctly set by the target server and is either 'gzip' or 'deflate' or completely absent (for uncompressed). If the target sends an unsupported encoding, the library will fail.
Error: premature close or similar stream-related errors.
These errors can arise if the response stream is closed prematurely or handled incorrectly after modification, especially if the `modifyResponse` callback takes too long or encounters an error before the stream operations are complete.
fix
Ensure that your modification logic within the `modifyResponse` callback is robust and handles all potential data states (e.g., empty body, invalid JSON). Also, confirm that `http-proxy` and Node.js `http` stream handling are correctly configured and not being interfered with by other middleware.
Upgrade
Version history
0.0.1latest on npm
Audit
Dependencies
http-proxyrequiredThis library is an extension for http-proxy, specifically designed to modify its proxied responses.
Agent activity
6 hits · last 30 days
node
6
Resources
http-proxy-response-rewrite — npm install http-proxy-response-rewrite · libregistry