{"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.","language":"javascript","status":"abandoned","last_verified":"Tue Apr 21","install":{"commands":["npm install node-http-proxy-json"],"cli":null},"imports":["const modifyResponse = require('node-http-proxy-json');","modifyResponse(res, proxyRes, function (body) { ... });"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}