7

I'm planning to do three sites using node.js. I have got some common templates among the sites. Should I run all three sites on single node.js instance?

I'm aware of 'vhost' middleware that allows you to run multiple domains on single http server. Is there any better option to do this?

I've also got some static html templates and not sure how to deal with these in node.js?

Finally I would like to know hosting options for this kind of setup?

Gurpreet Singh
  • 20,907
  • 5
  • 44
  • 60
  • You should use nginx. Check out this post: http://stackoverflow.com/questions/5009324/node-js-nginx-and-now – Pardoner Oct 28 '12 at 14:52

1 Answers1

15

I myself just had to do this exact same thing. What you want to do is use some sort of reverse proxy.

The one I use is here: https://github.com/nodejitsu/node-http-proxy

Simply install the proxy package: npm install http-proxy

What I do is have the proxy running on the server on port 80. I set the DNS up on each domain to point to this server.

Each application is running on the same server (im using screens).

For example:

MySiteApplication1 - 3001
MySiteApplication2 - 3002
MySiteApplication3 - 3003

Then your proxy server file would look like this

var httpProxy = require('http-proxy');

var server = httpProxy.createServer({
   router: {
     'mysite1.com': 'localhost:3001',
     'mysite2.com': 'localhost:3002',
     'mysite3.com': 'localhost:3003'
   }
});

server.listen 80
Menztrual
  • 40,867
  • 12
  • 57
  • 70
  • Thanks Brendan, Where are you hosting your node apps? How did httpProxy work in terms of performance? Have you got any static data, if yes then how are you dealing with that? Cheers – Gurpreet Singh Oct 28 '12 at 12:51
  • 1
    Im hosting it on a VPS that I purchased. So something like linode is fine. For static data, you would probably want to use something like nginx but I haven't ran into any issues. You could use `ab` to test it though :) – Menztrual Oct 28 '12 at 12:54
  • 2
    This answer is outdated. `node-http-proxy` [removed](https://github.com/nodejitsu/node-http-proxy/issues/652) the `router` option because it "belongs in a separate module." [`http-master`](https://github.com/encharm/http-master) seems to be the answer to this, and uses `node-http-proxy` internally. – Nateowami Jun 21 '15 at 10:05
  • 1
    @Nateowami they've removed `router` option but there is a way to do same thing with `node-http-proxy`. See [Pradino's answer to similar question](http://stackoverflow.com/a/31644584/1539073). – Bogac Oct 29 '15 at 16:13