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

16 Replies, 1297 Views

Hey! I had a similar issue with Node Express proxy change request path a while back. What worked for me was using `http-proxy-middleware`'s `pathRewrite` option. You can set it up like this:

```javascript
const { createProxyMiddleware } = require('http-proxy-middleware');

app.use('/api/v1', createProxyMiddleware({
target: 'http://backend-server',
pathRewrite: { '^/api/v1': '/v2' },
}));
```

This will rewrite `/api/v1/something` to `/v2/something`. You can tweak the regex to match your needs. For dynamic paths, you can use a function inside `pathRewrite` to handle it. Hope this helps!

Messages In This Thread



Users browsing this thread: 1 Guest(s)