http-proxy-middleware icon indicating copy to clipboard operation
http-proxy-middleware copied to clipboard

Wrong origin, with CORS error in browser but response is 200 with right body

Open mburger81 opened this issue 7 years ago • 4 comments

Expected behavior

We do a request from browser localhost:8100 to http://localhost:3000/proxy/tun0/blabla which should be redirected to https://tun0.bla.bla.com/bla

The strange thing I use several routes and any other is working expecting this one, and it is totally configured equals as the others.

The other strange thing is, the request is the only one request which has not 'localhost:8100' as Access-Control-Allow-Origin, but it has https://tun0.bla.bla.com, but in the response is OK 200 and has the response in it. But at the end I get a CORS error on the browser!!! This is very very stranged, some ideas

Actual behavior

Setup

  • http-proxy-middleware: 0.17.4
  • server: express 4.15.4
  • cors 2.8.4

proxy middleware configuration

/* STUN */
var proxyMiddlewareStun0 = proxy({
    target: 'https://tun0.bla.bla.com/bla',
    changeOrigin: true,
    headers: {
        Origin: 'https://tun0.bla.bla.com'
    },
    cookieDomainRewrite: '',
    pathRewrite: {
        '/proxy/tun0': ''
    },
    logLevel: 'debug'
});```
#### server mounting
```javascript
var app = express();
    app.use(cors(
                    {
                        credentials: true,
                        origin: function (origin, callback) {
                                    callback(null, true)
                                }
                    }
                )
           );
    app.use('/proxy/tun0', proxyMiddlewareStun0);

console.log("listening on port 3000");
var server = app.listen(3000);

mburger81 avatar Aug 18 '17 15:08 mburger81

hello, do you fix this issure?

rayunstop avatar Nov 21 '17 13:11 rayunstop

hello,What did you do about it?

erdouzhang avatar Feb 13 '19 03:02 erdouzhang

Not sure about the impact but this works for me.

  onProxyRes: async (proxyRes: http.IncomingMessage) => {
    proxyRes.headers['access-control-allow-origin'] = '[ORIGIN HERE]';
  }

bratanon avatar May 02 '21 12:05 bratanon

Not sure about the impact but this works for me.

  onProxyRes: async (proxyRes: http.IncomingMessage) => {
    proxyRes.headers['access-control-allow-origin'] = '[ORIGIN HERE]';
  }

This work for me. Thanks.

MinimalistYing avatar Jan 25 '24 16:01 MinimalistYing