Node-http-proxy 动态路由表?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于每个面临这个问题的人,我终于找到了开箱即用的解决方案。如果您传递一个指向文件的字符串,而不是传递一个对象作为参数,那么这一切都是可能的。我举个例子应该就很清楚了。
proxy.js:
正如你在这里看到的,我将 table.json 作为路由器选项传递。所以查看该文件的内部。
table.json:
这就是全部的魔力。 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:
As u see here, i pass table.json as router option. So look inside that file.
table.json:
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
是的,但不使用 ProxyTable。我已经记录了 http-proxy 的 ProxyTable 的替代方案,称为“Switchboard”< /a> 这就是你想要它做的事情。您必须重新安排一些功能才能正确初始化路径和后端目标,但这应该是一个简单的操作,并且
backendTable
对象在运行时动态可用。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.