javascript - Modifying response body from target proxy through http-proxy-middleware - Stack Overflow

I am trying to modify HTML body response of proxied target url, but my res.send method is not executing

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.

Share Improve this question edited Nov 19, 2024 at 8:48 traynor 8,9123 gold badges15 silver badges28 bronze badges asked Nov 18, 2024 at 10:36 RangRang 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

To modify response, you need to use responseInterceptor method:

Intercept responses from upstream with responseInterceptor. (Make sure to set selfHandleResponse: 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');
    }),
  },
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745625694a4636789.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信