2

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

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.