For example, my request is http://localhost/this/that/path and on the target, I want http-proxy to call the target like this http://target.server/final/path. If you noticed the this/that got replaced by final
switch(hostname)
{
case 'foo.org':
if (pathname == 'this/that/path') {
proxy.web(req, res, { target: 'http://target.server' });
}
break;
has the same question
Same here. #887 refers to an issue on http-proxy-middleware, which points to its docs and implies that there is a pathRewrite option. But I've tried that and it does not work.
did you try to modify your request?
req.url = pathname;
if (hostNameChanged) {
req.headers.host = hostname;
}
proxy.web(req, res, { target: 'http://target.server' });
I'm so sorry @jf990. I forgot about this ticket I raised. The issue isn't in the hostname. My question was related to the pathing, this/that/path.
In Nginx, we can have a rule like
location /this/that/path {
proxy_pass http://target.server/final/path;
}
This is what I want to accomplish. What node-http-proxy is doing is it copies the whole path as is and then appends to the target server. I'll give pathRewrite option that @mrdanimal mentioned.
Thank you so much @mrdanimal! I got pathRewrite working on http-proxy-middleware. It is neat!
I have a NodeJS server api running on ip address 192.168.1.76 on TCP port 8080. Express is configured to listen on /api and not /apigarage. However, on my ReactJS app, I'm calling http://localhost/apigarage. My http-proxy-middleware listens on TCP port 80. That's the reason why I didn't put any TCP port after localhost. It worked great!
apiGarage2: {
target: 'http://192.168.1.76:8080',
changeOrigin: true,
pathRewrite: {
'^/apigarage' : '/api'
},
},
This is old and closed issue.
but, I hope my experience can help someone who want to use node-http-proxy.
I managed to change target path using proxyReq.
global.proxy.on('proxyReq', function(proxyReq, req, res, options){
const rewritedPath = req.url.replace('/foo','/'); // can make any rule
proxyReq.path = rewritedPath;
});
Is the prependPath option valid?
Adding another possible example in context of an express server.
```
const apiProxy = httpProxy.createProxyServer();
app.use('/api/google', function (req, res) {
const proxyTarget = 'https://google.com';
console.log(PROXY: Replaced URL is - ${cff}${req.url});
apiProxy.web(req, res, {target: proxyTarget});
});
I'm configuring the proxy for Angular app that runs alongside backend Spring app. Both run in same Tomcat, but Angular app path is prefixed.
Our use case is to ease local development. Proxy consists of 2 contexts: one for dropping the prefix that leads to the Angular app ( goes to :4200), other for remote server requests (proxied).
Not sure I could subscribe to events in the config file and because native pathRewrite refused to work for me -- it just didn't rewrite the path, although was caught in the correct context I had to do this:
{
...
"context": ["/prefix"],
"bypass": function (req, res, opts) {
req.url = req.url.replace('/prefix', '') || '/';
return req.url;
}
},
Most helpful comment
This is old and closed issue.
but, I hope my experience can help someone who want to use node-http-proxy.
I managed to change target path using proxyReq.
global.proxy.on('proxyReq', function(proxyReq, req, res, options){ const rewritedPath = req.url.replace('/foo','/'); // can make any rule proxyReq.path = rewritedPath; });