Node-http-proxy 动态路由表?

发布于 2024-12-08 17:38:35 字数 296 浏览 0 评论 0原文

我对 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);

我的问题是,我可以动态更新路由表吗?不关闭代理服务器?

感谢您的回答

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏璃陌 2024-12-15 17:38:35

对于每个面临这个问题的人,我终于找到了开箱即用的解决方案。如果您传递一个指向文件的字符串,而不是传递一个对象作为参数,那么这一切都是可能的。我举个例子应该就很清楚了。

proxy.js:

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

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

正如你在这里看到的,我将 table.json 作为路由器选项传递。所以查看该文件的内部。

table.json:

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

这就是全部的魔力。 node-http-proxy 将监视该文件,如果您对其进行任何更改,它将自动更新其路由表。

问候

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

执着的年纪 2024-12-15 17:38:35

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文