Node.js Express服务器代理多个应用程序
我正在尝试为多节点应用程序编写反向代理。当你这样做的时候:Node.js Express服务器代理多个应用程序,node.js,express,proxy,reverse-proxy,Node.js,Express,Proxy,Reverse Proxy,我正在尝试为多节点应用程序编写反向代理。当你这样做的时候: app.use('/', proxy('http://localhost:5010/')); app.listen(8000, (err) => { if (err) { return console.error('Application failed to start:', err); } console.log('Application listening on port', 8000); }); 它按预期工
app.use('/', proxy('http://localhost:5010/'));
app.listen(8000, (err) => {
if (err) {
return console.error('Application failed to start:', err);
}
console.log('Application listening on port', 8000);
});
它按预期工作。但当我做类似的事情时
app.use('/', proxy('http://localhost:5010/'));
app.use('/config', proxy('http://localhost:5020/config'));
app.listen(8000, (err) => {
if (err) {
return console.error('Application failed to start:', err);
}
console.log('Application listening on port', 8000);
});
我请求路由到本地主机:5010
如果我只对/config
执行代理,它将正确路由。这是关于我做多个代理的事情
我正在为此使用express和express http代理
你有什么想法吗 Express middleware按顺序执行。那么你的第一行呢
app.use('/', proxy('http://localhost:5010/'));
对于/
和/config
以及所有路由都是如此。因此,如果你想为某些路线做一些不同的事情,就把它放在最上面
app.use('/config', proxy('http://localhost:5020/config'));
app.use('/', proxy('http://localhost:5010/'));
因此,现在您的第一行将仅对/config
路径有效,所有其他路由器将前进到secod处理程序