如何使用Heroku在同一个子域中托pipe多个node.js应用程序?

我们有一个http://api.mysite.com的基本域。 这应该成为我们所有API的前门。 比方说,我们有两个不同的API使用下面的url结构访问:

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

这些完全不同。 关于在Heroku上运行它似乎我们有两个select。

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

2)有3个不同的Heroku应用程序。 首先作为代理( http://mysite-api-proxy.herokuapp.com ),将看看传入的请求,并redirect到http://planttrees.herokuapp.comhttp://launchrockets.herokuapp.com使用像有弹性或HTTP代理模块。

我倾向于选项2,但我担心pipe理代理应用程序的负载。 对于具有同步架构的Web框架来说,这种方法将是灾难性的。 然而,使用群集模块和asynchronous的node.js,我认为这可能可以扩展。

我曾经见过类似的问题,但是大部分都与同步框架有关,其中选项2肯定是一个糟糕的select。 这个问题是针对节点以及它将如何执行的。

思考最好的方式来devise这个?

这里是我写的一篇博客文章,试图回答这个问题。 有很多select,但你已经决定什么是适合你的应用程序和体系结构。

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

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

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

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

与@TehNrd类似,我正在使用代理。 但是这种方法不需要多个heroku应用程序,只有一个:

在您的networking应用程序:

 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);