Node-http-proxy dynamic routetable?
Asked Answered
T

2

9

im using following code for http-proxy:

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

var options = {
    router: {
    'url1.com': '127.0.0.1:3000',
    'url2.com': '127.0.0.1:3001'
    }
};

httpProxy.createServer(options).listen(80);

My question is, can i update the routetable dynamically? Without shuting down proxy server?

Thx for answers

Tool answered 7/10, 2011 at 17:29 Comment(0)
T
15

For everyone facing that problem, finally i got the solution out of the box. Its all possible, if u pass a string pointing to a file, instead of passing an object as arg. I'll give an example and it should be clear.

proxy.js:

var httpProxy = require('http-proxy');
var options   = { router: 'table.json' };

httpProxy.createServer(options).listen(80);

As u see here, i pass table.json as router option. So look inside that file.

table.json:

{
   "router":
   {
      "domain1.com": "127.0.0.1:8080",
      "domain2.com": "127.0.0.1:8001"
   }
}

And thats the whole magic. node-http-proxy will monitor that file, and if you do any changes on it, it will update its routetable automatically.

Greetings

Tool answered 27/4, 2012 at 6:40 Comment(2)
And that's how you get a working proxy server up and running in 5 minutes. I find NodeJS awesome. Light and simple.Johnette
Do they have a documentation for this?Ulysses
B
0

Yes, but not using the ProxyTable. I've documented an alternative to http-proxy's ProxyTable called 'Switchboard' that does what you want it to do. You'll have to re-arrange some of the features to initialize the paths and backend targets properly, but it should be a straightforward operation, and the backendTable object is dynamically available at runtime.

Believe answered 5/1, 2012 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.