I am trying to modify HTML body response of proxied target url, but my res.send
method is not executing because onProxyRes
function not waiting the proxyRes.on('end', () => {
event.
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
const onProxyRes = async function (proxyRes, req, res) {
let body = '';
proxyRes.on('data', (chunk) => {
body += chunk;
});
proxyRes.on('end', () => {
res.send(body + ' modified part'); // Send the modified response
});
};
;(async() => {
const exampleProxy = createProxyMiddleware({
target: 'https://example.com',
changeOrigin: true,
on: { proxyRes: onProxyRes }
});
app.use('/', exampleProxy);
app.listen(3000)
})()
output is the source code of https://example.com
.
But if I update the function as,
const onProxyRes = async function (proxyRes, req, res) {
res.send('test'); // Send the modified response
};
I can set the response as test
, but I need to set the response from actual target url’s response body + my modifications.. any suggestions? thanks.