node-http-proxy-json icon indicating copy to clipboard operation
node-http-proxy-json copied to clipboard

Response not returned

Open marcusds opened this issue 7 years ago • 2 comments

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;
	});
};

marcusds avatar Nov 08 '19 20:11 marcusds

modifyResponse is a callback of type void, so it doesn't return a value. You have two options:

  1. use the body value within the callback itself, apply modifications in there.
  2. 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);
};

sleeyax avatar Nov 27 '19 00:11 sleeyax

@marcusds You can compare the test cases, such as gzip.js, which has Promise usage

langjt avatar Nov 27 '19 01:11 langjt