How to Modify the Request Path in a Node Express Proxy Setup?

16 Replies, 1346 Views

Dude, for Node Express proxy change request path, you can also try `http-proxy` directly if `http-proxy-middleware` feels limiting. It’s a bit more low-level but gives you full control. Here’s a quick example:

```javascript
const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer();

app.use('/api/v1', (req, res) => {
req.url = req.url.replace('/api/v1', '/v2');
proxy.web(req, res, { target: 'http://backend-server' });
});
```

This lets you tweak the path or even the entire request object before forwarding. It’s a bit more work, but worth it if you need fine-grained control.

Messages In This Thread



Users browsing this thread: 1 Guest(s)