{"id":16141,"library":"node-http-proxy-json","title":"JSON Response Transformation for node-http-proxy","description":"node-http-proxy-json is a specialized library, currently at version 0.1.9, designed to intercept and transform JSON responses from a proxied server when used in conjunction with node-http-proxy. Its primary function is to allow developers to modify JSON payloads on the fly, making it suitable for scenarios like API mocking, data manipulation, or stripping sensitive information before the response reaches the client. The library handles responses compressed with `gzip` or `deflate`, as well as uncompressed data. Unlike general-purpose stream manipulation tools such as Harmon, which targets HTML/XML, node-http-proxy-json focuses exclusively on JSON. Given its last commit in 2016, the project appears to be abandoned, with no active development or maintenance, and no clear release cadence, making it potentially unsuitable for new projects requiring ongoing support or modern features.","status":"abandoned","version":"0.1.9","language":"javascript","source_language":"en","source_url":"https://github.com/langjt/node-http-proxy-json","tags":["javascript","http-proxy","streaming","json"],"install":[{"cmd":"npm install node-http-proxy-json","lang":"bash","label":"npm"},{"cmd":"yarn add node-http-proxy-json","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-http-proxy-json","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This library is an extension for node-http-proxy, requiring its `proxyRes` event for operation.","package":"http-proxy","optional":false}],"imports":[{"note":"The library is CommonJS-only and does not provide ESM exports. Direct default import is not supported.","wrong":"import modifyResponse from 'node-http-proxy-json';","symbol":"modifyResponse","correct":"const modifyResponse = require('node-http-proxy-json');"},{"note":"The correct order of arguments is `res` (response from proxy server to client) then `proxyRes` (response from target server to proxy).","wrong":"modifyResponse(proxyRes, res, function (body) { ... });","symbol":"modifyResponse function signature","correct":"modifyResponse(res, proxyRes, function (body) { ... });"}],"quickstart":{"code":"const zlib = require('zlib');\nconst http = require('http');\nconst httpProxy = require('http-proxy');\nconst modifyResponse = require('node-http-proxy-json');\n\n// Create a proxy server\nconst proxy = httpProxy.createProxyServer({\n    target: 'http://localhost:5001'\n});\n\n// Listen for the `proxyRes` event on `proxy` to modify JSON responses\nproxy.on('proxyRes', function (proxyRes, req, res) {\n    // Only modify if Content-Type is application/json\n    const contentType = proxyRes.headers['content-type'];\n    if (contentType && contentType.includes('application/json')) {\n        modifyResponse(res, proxyRes, function (body) {\n            if (body) {\n                // Example modification: change age, remove version field\n                body.age = 2;\n                delete body.version;\n                console.log('Modified response body:', body);\n            }\n            return body; // Return the modified body (can be a Promise)\n        });\n    } else {\n        console.log('Skipping modification for non-JSON response.');\n    }\n});\n\n// Create your main server that proxies requests\nconst server = http.createServer(function (req, res) {\n    proxy.web(req, res);\n}).listen(5000, () => {\n    console.log('Proxy server listening on port 5000');\n});\n\n// Create your target server that provides JSON responses (e.g., gzipped)\nconst targetServer = http.createServer(function (req, res) {\n    const gzip = zlib.Gzip();\n    const _write = res.write;\n    const _end = res.end;\n\n    gzip.on('data', function (buf) { _write.call(res, buf); });\n    gzip.on('end', function () { _end.call(res); });\n\n    res.write = function (data) { gzip.write(data); };\n    res.end = function () { gzip.end(); };\n\n    res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'gzip'});\n    res.write(JSON.stringify({name: 'original-name', age: 1, version: '1.0.0', status: 'active'}));\n    res.end();\n}).listen(5001, () => {\n    console.log('Target server listening on port 5001');\n    console.log('Test with: curl -H \"Accept-Encoding: gzip\" http://localhost:5000');\n});","lang":"javascript","description":"This quickstart demonstrates setting up a proxy server using `node-http-proxy` and `node-http-proxy-json` to intercept and modify gzipped JSON responses from a target server. It modifies the 'age' field and removes the 'version' field before sending the response to the client."},"warnings":[{"fix":"Evaluate modern alternatives for proxying and response modification (e.g., 'http-proxy-middleware' with custom body parsers, or writing custom stream handlers) if long-term maintenance and security are critical.","message":"The project is abandoned, with the last commit in 2016. It is not maintained, may have unpatched vulnerabilities, and is unlikely to receive updates for newer Node.js versions or dependencies. Use with caution or consider alternatives.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"Ensure your target server only uses supported compression formats or implement custom handling for unsupported formats by extending `http-proxy`'s `proxyRes` event handler manually.","message":"This library only supports 'gzip', 'deflate', and uncompressed content encodings. If the target server uses 'br' (Brotli) or other compression types, the response body will not be parsed correctly, and modifications will fail.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Always check `body` for null/undefined inside your callback function. Consider verifying `Content-Type` header before calling `modifyResponse` if unexpected data types are possible from the target.","message":"The library assumes `Content-Type: application/json`. If a proxied response has a different content type but contains JSON, or if the content type is `application/json` but the body is not valid JSON, parsing will fail, and `body` passed to the callback might be `null` or cause errors.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Add a check `if (body) { ... }` before attempting to access properties of the `body` object. Verify the `Content-Type` header and `Content-Encoding` from the `proxyRes` object to ensure it's JSON and uses a supported compression.","cause":"The `body` passed to the modification callback was `null` or undefined, likely because the response could not be parsed as valid JSON or had an unsupported encoding.","error":"TypeError: Cannot read properties of null (reading 'age')"},{"fix":"Configure the target server to use 'gzip', 'deflate', or no compression. Alternatively, consider using a different proxy library or implementing a custom `proxyRes` handler to decompress Brotli manually before processing the JSON.","cause":"The proxied server's response is compressed using Brotli ('br'), which is not supported by `node-http-proxy-json`.","error":"Error: Unsupported content-encoding: br"}],"ecosystem":"npm"}