我有一台主计算机,它提供许多webapplications(不是node.js).它使用不同的端口执行此操作.这意味着例如以下应用程序是实时的: app1:http://hostname:3000 app2:http://hostname:3001 app3:http://hostnam
> app1:http://hostname:3000
> app2:http://hostname:3001
> app3:http://hostname:3003
接下来我有一个基于node.js的webapp(在端口80上运行),我想用它作为一种路由器.当有人导航到http://localhost/app/app1.我希望它导航到http://hostname:3000.使用简单的重定向这是相对简单的.但是,我想保留网址http://localhost/app/app1.有人能指出我使用node.js / express进行此工作的方法吗?
我的路由逻辑看起来有点像(伪代码).
app.route('/app/:appName')
.get(appEngine.gotoApp);
appEngine.gotoApp = function(req, res) {
redirectToApp logic
}
您可能最好使用
Nginx设置每个应用程序具有不同位置的反向代理.
这不是你要求的,因为它不使用node.js,但如果它是唯一的目的,Nginx真的很适合你的需求.
例如,Nginx配置文件应该按您想要的方式工作:
server {
listen 80;
server_name myapp.com;
location /app1 {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /app2 {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /app3 {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3003;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
