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-rewriteVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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.
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.
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.