I'm trying to write a proxy using http-proxy-middleware
that modifies the content of the response, and set some headers.
I'm intercepting the reponse using responseInterceptor
as the documentation says.
Here is the proxy implementation
const proxy = createProxyMiddleware({
target: "http://www.google.com" ,
changeOrigin: true,
/**
* IMPORTANT: avoid res.end being called automatically
**/
selfHandleResponse: true, // res.end() will be called internally by responseInterceptor()
/**
* Intercept response and replace 'Hello' with 'Goodbye'
**/
onProxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
res.setHeader("custom-header", "header-value");
const response = responseBuffer.toString('utf8'); // convert buffer to string
return response.replaceAll('Hello', 'GoodBye'); // manipulate response and return the result
}),
});
The issue is related with the target, for example for the target google.com
it works perfectly, but using notion.so
I'm getting an empty response buffer (so I can't modify it, neither the headers), but finnaly the proxy returns the response without any changes. Is onProxyRes calling before receiving the response of the server? Or I'm missing some configuration?