Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to host multiple node.js apps on the same subdomain with Heroku?

Tags:

node.js

heroku

We have a base domain of http://api.mysite.com. This should serve as the front door for all our APIs. Let's say we have two different APIs accessed with the following url structure:

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

These are totally different. With regards running this on Heroku it seems we have two options.

1) Put everything inside one application on Heroku. This feels wrong (very wrong) and could lead to a higher chance of changes in one API inadvertently breaking the other.

2) Have 3 different Heroku apps. The first as a proxy (http://mysite-api-proxy.herokuapp.com) that will look at the incoming request and redirect to http://planttrees.herokuapp.com or http://launchrockets.herokuapp.com using a module like bouncy or http-proxy.

I'm leaning towards option 2 but I am concerned about managing the load on the proxy app. For web frameworks that have a synchronous architecture this approach would be disastrous. Yet with node.js using the cluster module and being asynchronous I think this may scale okay.

I've seen similar questions asked before but most related to synchronous frameworks where option 2 would definitely be a poor choice. This question is specific to node and how it would perform.

Thoughts on best way to architect this?

like image 207
TehNrd Avatar asked Mar 14 '14 03:03

TehNrd


3 Answers

I implemented simple demo project to achieve multi-app structure.

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

With this structure, you can easily set up and maintain each app independently.
I hope this would be a help for you.

like image 125
toshi Avatar answered Oct 05 '22 05:10

toshi


Here is a blog post I wrote trying to answer this question. There are many options but you have decide what is right for your app and architecture.

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

like image 20
TehNrd Avatar answered Oct 05 '22 05:10

TehNrd


Similar to @TehNrd, I'm using a proxy. However this approach doesn't require multiple heroku apps, just one:

On your web app:

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

On your API server:

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

...
...
app.listen(port);
like image 28
Ricky Sahu Avatar answered Oct 05 '22 04:10

Ricky Sahu