【问题标题】:How to host multiple node.js apps on the same subdomain with Heroku?如何使用 Heroku 在同一个子域上托管多个 node.js 应用程序?
【发布时间】:2014-04-19 04:37:18
【问题描述】:

我们的基本域为http://api.mysite.com。这应该作为我们所有 API 的前门。假设我们有两个使用以下 url 结构访问的不同 API:

http://api.mysite.com/launchrockets
http://api.mysite.com/planttrees

这些是完全不同的。关于在 Heroku 上运行它,我们似乎有两个选择。

1) 将所有内容放在 Heroku 上的一个应用程序中。这感觉是错误的(非常错误),并且可能导致一个 API 的更改无意中破坏另一个 API。

2) 拥有 3 个不同的 Heroku 应用。第一个作为代理 (http://mysite-api-proxy.herokuapp.com) 将查看传入请求并使用 bouncyhttp-proxy 之类的模块重定向到 http://planttrees.herokuapp.comhttp://launchrockets.herokuapp.com

我倾向于选项 2,但我担心管理代理应用程序的负载。对于具有同步架构的 Web 框架,这种方法将是灾难性的。然而,对于使用 cluster module 的 node.js 并且是异步的,我认为这可以扩展。

我之前看到过类似的问题,但大多数与同步框架有关,其中选项 2 肯定是一个糟糕的选择。这个问题特定于节点以及它的执行方式。

关于构建这个的最佳方式的想法?

【问题讨论】:

    标签: node.js heroku


    【解决方案1】:

    类似于@TehNrd,我正在使用代理。然而,这种方法不需要多个 Heroku 应用,只需要一个:

    在您的网络应用上:

    var express = require('express')
      , url = require('url')
      , api_app = require('../api/server') //this is your other apps index.js or server.js 
      , app = express()
      , httpProxy = require('http-proxy')
      , apiport = parseInt(process.env.PORT)+100 || 5100 //this works!
      ;
    
    // passes all api requests through the proxy
    app.all('/api*', function (req, res, next) {
      api_proxy.web(req, res, {
          target: 'http://localhost:' + apiport
      });
    });
    

    在您的 API 服务器上:

    var express  = require('express');
    var app      = express();
    var port     = parseInt(process.env.PORT)+100 || 5100;
    
    ...
    ...
    app.listen(port);
    

    【讨论】:

      【解决方案2】:

      我实现了一个简单的演示项目来实现多应用结构。

      https://github.com/hitokun-s/node-express-multiapp-demo

      通过这种结构,您可以轻松地独立设置和维护每个应用程序。
      我希望这对你有帮助。

      【讨论】:

      • 如果我们运行单独的 repo。表示 heroku 中同一域上的两个应用程序
      【解决方案3】:

      这是我写的一篇博客文章,试图回答这个问题。有很多选择,但您已决定适合您的应用和架构的选项。

      http://www.tehnrd.com/host-multiple-node-js-apps-on-the-same-subdomain-with-heroku/

      【讨论】:

      • 这篇博文有可能加载吗?我什么也没得到。
      • 这太不可思议了,我已经为我的业余项目研究了两天多。如果它可能对其他人有所帮助,我做出了以下决定:鉴于我有时间,我选择了一个代理其他应用程序(或 API)的应用程序。我记住,一旦我有更多的流量,并且有足够的时间来管理每个应用程序,我就会恢复到独立的应用程序。谢谢@TehNrd
      猜你喜欢
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 2012-12-10
      • 1970-01-01
      • 2018-12-21
      • 2016-04-05
      相关资源
      最近更新 更多