node-http-proxy-json
node-http-proxy-json copied to clipboard
Response not returned
I am just trying to keep it super simple for, now but it is not working for me. I just get an empty return. What am I doing incorrectly?
Here is my code:
onProxyRes: (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], function(body) {
if(body) {
// modify some information
}
return body;
});
};
modifyResponse is a callback of type void, so it doesn't return a value. You have two options:
- use the
bodyvalue within the callback itself, apply modifications in there. - convert the callback to a promise so you can retrieve the value asynchronously:
function toJsonResponse(res, proxyRes) {
return new Promise((resolve, reject) => modifyResponse(res, proxyRes, (body) => resolve(body)));
}
// ...
proxy.on('proxyRes', async (proxyRes, req, res) => {
const body = await toJsonResponse(res, proxyRes);
// do something with body here
console.log(body);
};
@marcusds You can compare the test cases, such as gzip.js, which has Promise usage