5

I'm trying to figure out the best way to accomplish this; essentially I have about 6 websites I have to get online but at the moment they will have next to zero traffic so to save money they need to be deployed on the same server (ideally we will be using Elastic BeanStalk from AWS).

Is there a way to essentially write each web application like normal (so they can easily be taken and moved to a dedicated server in the future) but have one app.js entry point that appropriate loads the node application depending on the URL?

Obviously this isn't ideal but and I've thought of a couple of ways to do this but I want to be as non-hacky as possible so the sites can easily be moved later.

Kris
  • 1,789
  • 3
  • 18
  • 27
  • What's wrong with running them as separate processes? If you simply want to forward the request to a specific node, I think a lot of people are using nginx for that. – Josh C. Aug 29 '13 at 20:20
  • Honestly not much; really was looking for a temporary and quick solution as we already have stuff setup in Elastic BeanStalk so it would be great to reuse that without screwing us down the road. But yeah that's an option (and more appropriate one). – Kris Aug 29 '13 at 20:30

1 Answers1

4

Use connect vhost. http://www.senchalabs.org/connect/vhost.html

var express = require('express'),
    main = express();

main.use(express.vhost('*.site1.com', require('../site1')));
main.use(express.vhost('*.site2.com', require('../site2')));

main.listen(80);

And ../site1/index.js might look like this:

var express = require('express'),
    app = express();

app.get('/', function(req, res) { res.send('Home Page'); });

module.exports = app;
Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
  • Yup this was perfect; apparently I didn't realize vhost could work with an entire app but that makes sense. Thanks! Hopefully soon it can be moved to a better solution. – Kris Sep 02 '13 at 08:51
  • Hi guys, will this mean that the Elastic Beanstalk App would scale as needed if for example only one of the apps gets high traffic/usage?. I'm looking for a way to deploy several nodeJS apps that need to scale but without having one individual server for each app. Makes sense? – Davo May 24 '17 at 15:52