【问题标题】:node.js: route request to different port on same hostnode.js:将请求路由到同一主机上的不同端口
【发布时间】:2015-07-24 14:01:56
【问题描述】:

我有一台主机,它服务于许多 web 应用程序(不是基于 node.js)。它使用不同的端口来做到这一点。这意味着例如以下应用程序是实时的:

接下来,我有一个基于 node.js 的 webapp(在端口 80 上运行),我想将其用作一种路由器。当有人导航到http://localhost/app/app1 时。我希望它导航到http://hostname:3000。这使用简单的重定向相对简单。但是,我想保留 url http://localhost/app/app1。有人可以指出我使用 node.js/express 完成这项工作的方法吗?

我的路由逻辑看起来有点像这样(伪代码)。

app.route('/app/:appName')
   .get(appEngine.gotoApp);

appEngine.gotoApp = function(req, res) {
    redirectToApp logic 
    }

【问题讨论】:

标签: node.js express


【解决方案1】:

如果使用 express,可以尝试使用 cli express 应用生成器创建应用。

它创建一个 express 应用程序并将其与模块导出一起返回。

在 server.js 文件中,它将 express 应用对象传递给服务器实例的监听函数。

您可以创建更多的服务器对象并使用不同的端口监听不同的应用程序。

var server = http.createServer(app);
server.listen(port);
var server2 = http.createServer(app2);
server2.listen(port2);

如果你想根据 url 指向不同的应用程序,你可以实例化一个 express 路由器而不是 express 对象。

var app1 = express.Router();

然后您可以使用经典的 get 或 post 或其他方法将所有路由设置到此对象中。

现在您可以将路由器作为主要 express 应用的中间件传递。

app.use( "app1/", app1 );

您还可以将 express 应用程序传递给中间件,而不是路由器对象,以获得执行应用程序的可能性,并使用不同的 url 和端口服务器侦听。

【讨论】:

    【解决方案2】:

    您最好使用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;
        } 
    }
    

    【讨论】:

      【解决方案3】:

      有一个很好的 http-proxy 库就是专门为此设计的!

      const httpProxy = require('http-proxy');
      const url = require('url');
      
      const proxy = httpProxy.createProxy();
      const options = {
          '/app/app1': 'http://localhost:3000',
          '/app/app2': 'http://localhost:3001',
          '/app/app3': 'http://localhost:3003',
      }
      
      require('http').createServer((req, res) => {
          const pathname = url.parse(req.url).pathname;
          for (const [pattern, target] of Object.entries(options)) {
              if (pathname === pattern || 
                  pathname.startsWith(pattern + '/')
              ) {
                  proxy.web(req, res, {target});
              }
          }
      }).listen(80);
      

      【讨论】:

        猜你喜欢
        • 2016-05-10
        • 1970-01-01
        • 2020-04-16
        • 2022-01-20
        • 2019-12-11
        • 2016-01-10
        • 1970-01-01
        • 2016-11-13
        • 1970-01-01
        相关资源
        最近更新 更多