Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiple sites on node.js

Tags:

node.js

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?

like image 722
Gurpreet Singh Avatar asked Oct 28 '12 11:10

Gurpreet Singh


People also ask

How do I run multiple node servers?

Yes you can run multiple node instances, all you have to do is to change the port number for which the server is listening. If you are using express framework then you can simply do it like this in app. js file. If you are not using express framework then you have to change in the main file which is generally server.

How many connections can a node js handle?

JS uses a single thread with an event-loop. In this way, Node can handle 1000s of concurrent connections without any of the traditional detriments associated with threads.

How many sites use Nodejs?

Node JS is most popular in the US—over 6.3 million websites use it.


1 Answers

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
like image 184
Menztrual Avatar answered Nov 14 '22 22:11

Menztrual