Nodejs App With Multiple Domains

Problem

I have one Express.js node application but I want to manage different content or behaviour from 2 different domain names. For example; ingles.myapp.com and spanish.myapp.com point to the same nodejs web app but have to serve different content.

Solution

For small applications use the Express.js Extention vhost. For large web applications use a reverse proxy to redirect to different servers.

Find the completed code at Github

How to Use VHost with Express 4.0

This post is based on Hack Sparrow‘s but with small changes to adapt it for Express 4.0 (vhost is no longer included with Express and has to be installed separately).

Installed pre-requisites are:

  1. Node.js
  2. Express.js
  3. If you don’t have Express app generator install it by typing npm install express-generator -g
  4. Linux, Unix or Mac

First

Modify the local host file to point to desired domains. In a production environment this would be done in a DNS rather than the host file (out of scope of the current article). So open a command line in your Mac or linux and go to the home directory and type nano /private/etc/hosts (for a linux type nano /etc/hosts) to open the hosts file with the nano text editor. Then add the following entries:

127.0.0.1 english.localhost

127.0.0.1 spanish.localhost

Your file should have the two entries like this:

Hosts file modified

Save and exit by pressing ctrl-x, then press the y key and enter. Note that I used nano text editor but you can work with EMACS or VI if you prefer (or any other text editor).

By making those entries in the hosts file, we are telling our PC “when you see apple.localhost or book.localhost, connect to yourself (localhost / 127.0.0.1), don’t bother doing any DNS lookups”. As mentioned by Hack Sparrow.

Second

We’ll begin by creating a folder structure below your home directory:

mkdir -p test_vhost/websites

Third

Generate Express.js application

The previous command mkdir with a -p creates the both parent and child directories even if the parent didn’t previously exist.

Then change directory to websites by typing cd test_vhost/websites/ and then create an express app with express english.localhost then you’ll see an output like:

Express app generator output

Now cd into english.localhost and then type npm install to download all the dependencies.

Fourth

odify the application title

In your english.localhost folder go to the routes directory and edit the index.js file by replacing the the following:

res.render('index', { title: 'Express' });

with

res.render('index', { title: 'English' });

Fifth

Repeat the third and fourth step for the spanish.localhost application

Basically follow the same steps but replace english.localhost with spanish.localhost in every case and set the application title to Spanish.

Sixth

Create an express app in the ‘test_vhost’ directory

You will be told the directory is not empty, and asked you if you want to continue. Go to the test_vhost directory and type the following command and confirme by typing y when prompt:

express npm install

See the following image

Create Express app in existing directory and confirm by pressing ‘y’

Seventh

Modify the vhost_test app.js file to include the english and spanish express applications

Open the vhost_test/app.js file with your favorite text editor (I use sublime for medium to large files) and add another require statement for the vhost module:

var vhost = require('vhost');

Then after the var app = express(); statement add the following variables

var english_localhost = require('./websites/english.localhost/app.js'); var spanish_localhost = require('./websites/spanish.localhost/app.js');

We just included the Node modules we created earlier. Now after theapp.use(cookieParser()); statement add the following lines:

app.use(vhost('english.localhost', english_localhost)); app.use(vhost('*.localhost', spanish_localhost));

That calls the vhost middleware and prepares it to handle our domain names.

Save the app.js file

Eight

Execute the application

You will need the vhost extension installed in the current application (vhost_example folder):

npm install vhost

Use the following command to run the application

DEBUG=vhost_example bin/www

Ninth

Open the applications

While the express application is running open a web browser and go to the following locations:

http://english.localhost:3000/

http://spanish.localhost:3000/

You should see the page running with their respective titles:

Tenth

Enjoy!

Remember for 2 applications this is a quick way to get started but if you are considering managing several Node.js applications then you should implement a reverse proxy.

Best regards!