Set up NodeJS / ExpressJS application with Nginx on Ubuntu 16.04

This is my second post of the series of posts I like to share about setting up a NodeJS / ExpressJS environment with Nginx on Ubuntu 16.04 LTS. In this post I will show you how to set up a basic application with NodeJS and ExpressJS on Ubuntu with Nginx as a web server. I will also show you how to connect your domain to your application so you don’t need to use a port number in the URL to access your NodeJS / ExpressJS application.

In the previous post I wrote about how to install NodeJS and NPM on Ubuntu 16.04. Also, I explained in details what NodeJS and NPM are as well as how to use them for your projects. In this one I will talk a little bit about ExpressJS. I will explain what is ExpressJS, why it is used today, how to install ExpressJS as well as how to set up a simple application.

What is ExpressJS?

ExpressJS is a flexible web application framework based on NodeJS and it allows you to build web and mobile applications much faster than using NodeJS only. Together with the MongoDB database and the AngularJS front-end framework it forms the MEAN stack.

Basically it is a layer on top of the NodeJS HTTP server module that allows you to set up routing in your applications efficiently as well as to organize the web applications into an MVC architecture on the server side. To get some insight I would recommend you to watch the video below:

 

Many compare ExpressJS to NodeJS with Sinarta or Ruby on Rails to Ruby. When it comes to template languages, ExpressJS supports variety of them including EJS, JADE etc.

Install ExpressJS on Ubuntu 16.04

Assuming that you followed my first tutorial and you have NodeJS installed on your machine, we can start with the installation of ExpressJS. Navigate to your home directory and create a new directory for your first ExpressJS project:

cd ~
mkdir expressApp
cd expressApp/

Now, create a package.json file for your application.

npm init

This file will serve you as a documentation for what packages your project depends on and will help you to deploy your application on a different machine much easier. Once you enter the command you will be asked a number of questions. Answer the questions or select the default value for all except this one:

entry point: (index.js)

Here you can enter app.js or whatever you want the name of the main application file to be. Here is how the output should looks like:

The next command will install the ExpressJS framework and will save it in the package.json dependencies list.

npm install express --save

At this point you have ExpressJS installed and ready to use.

Create a sample ExpressJS application

Create the app.js file and paste the following content in it:

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

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000, function () {
  console.log('App started on port 3000')
})

To create and edit files under Linux you can use a text editor of your choice. I prefer using nano as it is simple and very easy to use. So to create the file using this text editor you can use:

nano app.js

Save the file and run the app:

nodejs app.js

The application will be accessible via web browser on port 3000.

However, it is not convenient to access the application using a port number in the URL (in this example 3000). It is much convenient to access the application using a domain name like mariodimov.com. Lets see how to set up this.

Use Nginx as a reverse proxy for your ExpressJS application

Nginx is a web server that can be used as a reverse proxy too. In this example, it will be used as a reverse proxy so you can access the ExpressJS application without using a port number.

Create Nginx server block for your domain name:

sudo nano /etc/nginx/sites-available/mariodimov.com

Use your actual domain name instead of mariodimov.com.
Paste the following content into the file:

upstream expressjs {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name mariodimov.com www.mariodimov.com;

    access_log  /var/log/nginx/expressjs.access.log;
    error_log   /var/log/nginx/expressjs.error.log;

    proxy_buffers 16 64k;
    proxy_buffer_size 128k;

    location / {
        proxy_pass  http://expressjs;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;

        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto https;
    }
}

Again, replace mariodimov.com with your actual domain name then save the file and create a symbolic link.

sudo ln -s /etc/nginx/sites-available/mariodimov.com /etc/nginx/sites-enabled/mariodimov.com

Check if there are errors in the configuration:

sudo nginx -t

If everything is OK, restart Nginx:

sudo systemctl restart nginx.service

Now, open your web browser and enter your domain name into the search bar.

This is much better right? 🙂

In the next tutorial I will show you how enable Transport Layer Security (TLS) encryption by installing a Let’s Encrypt SSL certificate for your application.

If you have any questions or thoughts post them in the comments below and don’t forget to share the tutorial.

Related Posts