Aron Suarez
Aron Suarez

Reputation: 33

path based proxy with http-proxy nodejs

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

Upvotes: 2

Views: 5919

Answers (2)

framp
framp

Reputation: 873

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;
}

Upvotes: 2

macross2005
macross2005

Reputation: 26

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)

Upvotes: 1

Related Questions