Search code examples
node.jshttp-proxy

Node-http-proxy dynamic routetable?


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


Solution

  • 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