Host multiple websites using Node.js Express

13,306

Solution 1

I am not sure how you are using the vhost. First of all with vhost approach, you need to run only one express app. Not two. Here is an example.

var express = require('express');
var vhost = require('vhost');

/*
edit /etc/hosts:

127.0.0.1       api.mydomain.local
127.0.0.1       admin.mydomain.local
*/

// require your first app here

var app1 = require("./app1");

// require your second app here

var app2 = require("./app2");

// redirect.use(function(req, res){
//   if (!module.parent) console.log(req.vhost);
//   res.redirect('http://example.com:3000/' + req.vhost[0]);
// });

// Vhost app

var appWithVhost = module.exports = express();

appWithVhost.use(vhost('api.mydomain.local', app1)); // Serves first app

appWithVhost.use(vhost('admin.mydomain.local', app2)); // Serves second app

/* istanbul ignore next */
if (!module.parent) {
  appWithVhost.listen(8000);
  console.log('Express started on port 8000');
}

You just need to run the main express app with vhost enabled using forever.

Solution 2

You're hosting the applications on the same port, using the same network interface. So when the second app starts, it will always find the port in use. If you want to use multiple applications on the same port, they each need to have their own network interface. When using vhost, you would still need to listen on a different port for each app. See this example for details. If you would like your apps to be completely independent, you're better off using node-http-proxy. This allows you to host a proxy on port 80 which forwards requests to express apps listening on different ports. If one of these apps crashes, it will not crash the other app, unlike the vhosts approach. This post gives an example of the implementation using node-http-proxy.

Solution 3

Thanks @veggiesaurus for pointing up to node-http-proxy. Apologies for posting late.

Here is how I solved my problem using node-http-proxy

Folder Structure:

  • www/
    • server.js
    • abc/ [express setup]
      • app.js
    • xyz/ [express-admin setup]
      • node_modules/express-admin/app.js

"abc" and "xyz" have there own setup and running on port x.x.x.x:3001 and x.x.x.y:3002

I installed node-http-proxy and added server.js file with following codes. Referred this link

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

var proxy = httpProxy.createProxy();
var options = {  
  'abc.com': 'http://x.x.x.x:3001',
  'xyz.com': 'http://x.x.x.y:3002'
}

http.createServer(function(req, res) {
  proxy.web(req, res, {
    target: options[req.headers.host]
  });
}).listen(80);

Finally, used forever to run all 3 apps setup to run forever in port 3001, 3002 and 80.

Share:
13,306
Pradino
Author by

Pradino

SOreadytohelp

Updated on June 04, 2022

Comments

  • Pradino
    Pradino almost 2 years

    I am having problems configuring two different Node.js applications with different domains. Have two directories

    "/abc/" -> express-admin setup (backend) -> admin.abc.com
    

    and

    "/xyz/" -> express setup (frontend) -> abc.com
    

    I need admin.abc.com to point to express-admin setup and abc.com to express setup. I have vhost installed and both the site listens to port 80.

    Have added

    app.use(vhost('abc.com', app)); // xyz/app.js file
    app.use(vhost('admin.abc.com', app)); // abc/app.js file
    

    My problems:

    • forever is installed, whenever i start both the apps, the second one is always stopped. I tried using different port for both apps but still having the same error. Individually they run without problems.

    • I think my setup is too complicated for domain forwarding. Any better suggestions? May be I have a master app.js file which I can use to route the domains to their respective apps without using the app.js of each applications.

  • Good Idea
    Good Idea over 7 years
    would there be performance issues or other problems if this same setup were used to run a larger number (say 20) of express apps? And, let's assume that 19 of these are very lightweight (express rendering simple Jade templates) - but the 20th is a little more complex and requires some more CPU. If I have 10 active users on each of the 20 sites, would the 10 that are viewing the more complicated one cause slowdowns for the viewers of the other 19?
  • cmroanirgo
    cmroanirgo over 7 years
    @StandardQuality. The problem with vhost is that all these separate apps are running in the same user space. That is there is probably a need to run them in separate spaces (like suexec in apache), so that if one site get's hacked/crashes, it can't infect the others. It might not matter much in this example though. Have a look at pm2: pm2.keymetrics.io/docs/usage/startup/#user-permissions