admin管理员组文章数量:1026213
I am trying to modify HTML body response of proxied target url, but my res.send
method is not executing because onProxyRes
function is 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: '',
changeOrigin: true,
on: { proxyRes: onProxyRes }
});
app.use('/', exampleProxy);
app.listen(3000)
})()
output is the source code of .
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.
I am trying to modify HTML body response of proxied target url, but my res.send
method is not executing because onProxyRes
function is 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',
changeOrigin: true,
on: { proxyRes: onProxyRes }
});
app.use('/', exampleProxy);
app.listen(3000)
})()
output is the source code of https://example
.
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.
1 Answer
Reset to default 0To modify response, you need to use responseInterceptor
method:
Intercept responses from upstream with
responseInterceptor
. (Make sure to setselfHandleResponse: true
)
(...)
NOTE:responseInterceptor
disables streaming of target's response.https://www.npmjs/package/http-proxy-middleware#intercept-and-manipulate-responses
See example from the recipes:
Replace text and change http status code
const { createProxyMiddleware, responseInterceptor } = require('http-proxy-middleware');
const proxy = createProxyMiddleware({
target: 'http://www.example',
changeOrigin: true, // for vhosted sites
/**
* IMPORTANT: avoid res.end being called automatically
**/
selfHandleResponse: true, // res.end() will be called internally by responseInterceptor()
/**
* Intercept response and replace 'Hello' with 'Teapot' with 418 http response status code
**/
on: {
proxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
res.statusCode = 418; // set different response status code
const response = responseBuffer.toString('utf8');
return response.replaceAll('Example', 'Teapot');
}),
},
});
I am trying to modify HTML body response of proxied target url, but my res.send
method is not executing because onProxyRes
function is 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: '',
changeOrigin: true,
on: { proxyRes: onProxyRes }
});
app.use('/', exampleProxy);
app.listen(3000)
})()
output is the source code of .
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.
I am trying to modify HTML body response of proxied target url, but my res.send
method is not executing because onProxyRes
function is 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',
changeOrigin: true,
on: { proxyRes: onProxyRes }
});
app.use('/', exampleProxy);
app.listen(3000)
})()
output is the source code of https://example
.
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.
1 Answer
Reset to default 0To modify response, you need to use responseInterceptor
method:
Intercept responses from upstream with
responseInterceptor
. (Make sure to setselfHandleResponse: true
)
(...)
NOTE:responseInterceptor
disables streaming of target's response.https://www.npmjs/package/http-proxy-middleware#intercept-and-manipulate-responses
See example from the recipes:
Replace text and change http status code
const { createProxyMiddleware, responseInterceptor } = require('http-proxy-middleware');
const proxy = createProxyMiddleware({
target: 'http://www.example',
changeOrigin: true, // for vhosted sites
/**
* IMPORTANT: avoid res.end being called automatically
**/
selfHandleResponse: true, // res.end() will be called internally by responseInterceptor()
/**
* Intercept response and replace 'Hello' with 'Teapot' with 418 http response status code
**/
on: {
proxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
res.statusCode = 418; // set different response status code
const response = responseBuffer.toString('utf8');
return response.replaceAll('Example', 'Teapot');
}),
},
});
本文标签: javascriptModifying response body from target proxy through httpproxymiddlewareStack Overflow
版权声明:本文标题:javascript - Modifying response body from target proxy through http-proxy-middleware - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745625692a2159844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论