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

[BUG] Path rewrite failed

Open zhangbobell opened this issue 7 years ago • 1 comments
trafficstars

Expected behavior

pathRewrite success

Actual behavior

pathRewrite failed

Setup

  • http-proxy-middleware: 0.18.0
  • server: express 4.15.4

proxy middleware configuration

var apiProxy = proxy({
	target: 'http://example.com/',
	pathRewrite: {'^/tetris/api/sentry/api/314/store/' : '/api/314/store/'},
	changeOrigin: true
});

server mounting

var app = express();


app.use('/tetris/api/sentry/api/314/store/', apiProxy);
app.listen(3000);

Reason

I found in here, using req.url not req.path, then req.url = 'http://host.com/path/to/be' and the regex in pathRewriter will not match this, since regex=^\/tetris\/api\/sentry\/api\/314\/store\/.

If we could change req.url -> req.path, it will be matched.

probably a bug ?

zhangbobell avatar May 21 '18 05:05 zhangbobell

I finally find that because I use SwitchyOmega to proxy a domain(eg. example.com) to 127.0.0.1:3000 , neither req.url nor req.originalUrl, they were http://example.com/path/xxxx, not desired /path/xxx. So maybe it's the problem about my usage.

But, if could you take this scenery into consideration ? use req.path or other variables firmly stand for the path, not depends on the usage?

zhangbobell avatar May 21 '18 08:05 zhangbobell