node.js - Express 服务器代理多个应用程序
问题描述
我正在尝试为多个节点应用程序编写反向代理。什么时候做这样的事情:
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);
});
我对http://localhost:8000/config的请求被路由到 localhost:5010
如果我只做代理,/config
它将正确路由。它是关于我何时做多个代理的。
我为此使用 express 和 express-http-proxy。
关于如何做到这一点的任何想法?
解决方案
Express 中间件按顺序执行。所以你的第一行
app.use('/', proxy('http://localhost:5010/'));
对于/
and/config
和所有的路线都是如此。因此,如果您想为某些路线做一些不同的事情,请将其放在顶部
app.use('/config', proxy('http://localhost:5020/config'));
app.use('/', proxy('http://localhost:5010/'));
因此,现在您的第一行将仅对/config
路径有效,所有其他路由器将前进到 secod 处理程序。
推荐阅读
- amazon-web-services - 诊断 vpc 之间 aws 上的路由问题
- javascript - PHP 限制可用游戏卡的数量
- php - PHP检查特征在使用前是否存在
- javascript - 使用滑块作为用户输入提交 HTML 表单并在 JavaScript 中使用其数据
- angular - 即使必填字段为空白,单击内部表单也会以角度提交
- node.js - 反应客户端尝试登录时如何解决express.js中的CORS请求不成功?
- php - 使用ajax从mysql DB检索和打印数据到文本框
- android - 如何从 EditText 禁用仅剪切选项?
- php - 如何像 PHP 中的框架一样传递参数?
- vhdl - 使用 VHDL 的 RTL 硬件设计,示例 7.1