Question

I want a proxy with path based routing. But my code not working

var httpProxy = require('http-proxy')

var proxy = httpProxy.createProxy();

var options = {
'example.com/app1': 'http://localhost:4444',
'example.com/app2': 'http://localhost:3333'
}

require('http').createServer(function(req, res) {
proxy.web(req, res, {
    target: options[req.headers.host]
},function(error) {

});
}).listen(80);

How is the problem?

Thanks for Help

Was it helpful?

Solution 2

the latest version of http-proxy dropped the proxytable feature. see https://github.com/nodejitsu/node-http-proxy/blob/master/UPGRADING.md the version 0.8.x can do path based routing. And for current http-proxy, a middleware can do proxytable for it (https://github.com/dominictarr/proxy-by-url)

OTHER TIPS

You might want to try express-http-proxy which let you mount the proxy on a express route:

app.use('/app1/', proxy('http://localhost:4444', {
    forwardPath: function(req, res){
      return url.parse(req.url).path.replace(/\/app1/,'/');
    }
  })
);

Even though you asked for a node.js solution, I can't really suggest to go with this route (no pun intended). You're probably better off with something which have been tested more with this specific use case - like nginx:

location /app1/ {
  proxy_pass http://localhost:4444;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top