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

16 Replies, 1348 Views

Yo! For Node Express proxy change request path, you can totally do it dynamically. Instead of hardcoding, use a function in `pathRewrite`. Here's a quick example:

```javascript
pathRewrite: function (path, req) {
if (req.headers['x-custom-header'] === 'v2') {
return path.replace('/api/v1', '/v2');
}
return path;
}
```

This way, you can check headers, query params, or whatever to decide how to rewrite the path. Super flexible! Also, check out the `http-proxy-middleware` docs—they’re pretty solid.

Messages In This Thread



Users browsing this thread: 1 Guest(s)