chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.7k stars 834 forks source link

web page continuously load when selfHandleResponse: true, used #807

Closed ShanArosh closed 2 years ago

ShanArosh commented 2 years ago

Checks

Describe the bug (be clear and concise)

web page continuously load when selfHandleResponse: true, used

Step-by-step reproduction instructions

const express = require('express');
const morgan = require("morgan");
const { createProxyMiddleware , responseInterceptor } = require('http-proxy-middleware');

// Create Express Server
const app = express();
// Configuration
const PORT = 3000;
const HOST = "localhost";

const proxy = createProxyMiddleware({
  target: 'http://example.com',
  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.replace('Hello', 'Teapot');
    }),
  },
});

app.get('/', proxy)

// Start Proxy
app.listen(PORT, HOST, () => {
    console.log(`Starting Proxy at http://${HOST}:${PORT}`);

});

Expected behavior (be clear and concise)

normally it loads when selfHandleResponse: false, but to edit response i need it to be true

How is http-proxy-middleware used in your project?

PS C:\Users\In-Motion\Desktop\netninja> npm ls http-proxy-middleware
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
netninja@1.0.0 C:\Users\In-Motion\Desktop\netninja
└── http-proxy-middleware@2.0.6

What http-proxy-middleware configuration are you using?

{
  "dependencies": {
    "express": "^4.18.1",
    "http-proxy": "^1.18.1",
    "http-proxy-middleware": "^2.0.6",
    "morgan": "^1.10.0",
    "url": "^0.11.0"
  },

What OS/version and node/version are you seeing the problem?

Windows 10 64 bit

Additional context (optional)

No response

chimurai commented 2 years ago

You were looking at v3 documentation and examples.

// v3
on: {
    proxyRes: ...
}
// v2
onProxyRes: ...

You can find the older v2 documentation here: https://github.com/chimurai/http-proxy-middleware/tree/2.x#intercept-and-manipulate-responses

ShanArosh commented 2 years ago

Thanks for your response. Thanks for this repo

bangank36 commented 1 year ago

@chimurai I was setting up the middleware for firebase functions and got stuck with exactly same issue. Thanks for your response!

morzel85 commented 1 year ago

I stumbled on this issue just today, so be aware that v3 is still in beta and normal npm install http-proxy-middleware will give you v2. Do npm install http-proxy-middleware@beta instead.