0

Can anyone please explain what the router in options does in this code. I got this code from a blog. I am trying to implement node http-proxy.

var http = require('http'),  
    httpProxy = require('http-proxy');

//
//Leave out the hostnameOnly field this time, or set it to false...
//
var options = {  
  router: {
    'domainone.com/appone': '127.0.0.1:9000',
    'domainone.com/apptwo': '127.0.0.1:9001',
    'domaintwo.net/differentapp': '127.0.0.1:9002'
  }
}


//
//...and then pass in your options like last time.
//
var proxyServer = httpProxy.createServer(options).listen(80);

//
// ...and a simple http server to show us our request back.
//
http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);
bharadwaj
  • 492
  • 1
  • 4
  • 13
  • Proxy tables have been deprecated in favor of [add-ons](https://github.com/nodejitsu/node-http-proxy#proxytable-api). – robertklep May 06 '16 at 13:00

1 Answers1

0

The proxy library will take all incoming requests and attempt to match it with a rule in your router table. Assuming it finds a match, it will forward that request to the IP address associated with the DNS name you provided.

For example, requests going to domainone.com/appone will be forwarded to 127.0.0.1:9000

One problem that I see here is that you are listening on port 9000, and your first rule re-routes to 127.0.0.1:9000.

tier1
  • 6,303
  • 6
  • 44
  • 75
  • But when I type domainone.com/appone. It is not showing the page of localhost:9000. – bharadwaj May 06 '16 at 13:06
  • Typing that into your browser will not automatically redirect you to localhost:9000. You would need to edit your local hosts file to send domainone.com to whatever ip address your proxy application is running – tier1 May 06 '16 at 13:30