Create a Node.js Express Application on a CodeAnywhere Ubuntu 14.04 Container

04 Aug 2017 19:11
Tags express nginx nodejs pm2 ubuntu

Back to list of posts


JavaScript has risen in popularity over the past several years and has expanded beyond client side scripting in browsers to server side development with Node.js. Express has become standard for Node.js applications out there today as a web framework that is flexible and easy to use. With the rise of container technology such as Docker as a means to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud it is useful to know and be able to develop and deploy Node.js web applications in containers.

An easy way to get started with developing applications in containers is with Codeanywhere. You can create a free account on Codeanywhere and Codeanywhere offers a variety of containers to choose from. However, for this article, I’ll walk through the steps of installing, configuring and running a production ready Express application on a blank Codeanywhere Ubuntu container.

Create a Container on Codeanywhere

Create a free account on Codeanywhere. Create a new project and create a container with a "Blank Development Stack" on Ubuntu 14.04.

Node.js and Express


The first step is to installl Node.js. Open an SSH Terminal and run the following commands:

# Download the package lists from the to get information on the newest versions of packages and their dependencies.
sudo apt-get update

# Add the NodeSource APT repository for Debian-based distributions repository AND the PGP key for verifying packages. Note: this is for Node 6.x
curl -sL | sudo -E bash -

# Install Node.js from the Debian-based distributions repository
sudo apt-get install -y nodejs

# Get the most up-to-date npm
sudo npm install npm --global


Epxress is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. The Express application generator can be used to quickly create an application skeleton.

Install the application generator with the following command:

sudo npm install express-generator -g

Creating an Express application

The Express application generator has several command line options that can be displayed with the -h option:

express -h

The option that will be used in this article is the view option to add view engine support. By default, the view engine that is used is jade. However, the next article that I am writing is Creating Node.js Web Applications for ASP.NET MVC Developers that uses the Vash template engine to offer Razor Syntax that is familiar to ASP.NET MVC Developers.

# Create a skeleton application with the express application generator using the Vash view engine with the following command:
express --view=vash myapp

Once the skeleton application has been created, the node packages will need to be installed.

# Change directory to the application directory
cd myapp

# Install the node packages
npm install

To run the application, use the following command:

DEBUG=myapp:* npm start

To test that the application is running within the container, open another SSH Terminal window and run the curl command:


Install and configure PM2

PM2 is a production process manager for Node.js apps with a built-in load balancer.

Install and run PM2 process manager

The PM2 process manager can be used to run Express applications as a service. The advantages of running your application this way are:

  1. PM2 will automatically restart your application if it crashes.
  2. PM2 will keep a log of your unhandled exceptions - in this case, in a file at /home/safeuser/.pm2/logs/app-err.log.
  3. With one command, PM2 can ensure that any applications it manages restart when the server reboots. Basically, your node application will start as a service.

To install execute following:

sudo npm install pm2 -g

Run the Express application as a service with the following command:

sudo pm2 start ./myapp/bin/www

Once you started all the applications you want to manage, you can save the list across expected/unexpected server restart by typing this command:

sudo pm2 save

It will save the process list with the corresponding environments into the dump file $PM2_HOME/.pm2/dump.pm2.

Configure PM2 to startup on reboot

Run this command to run your application as a service by typing the following:

sudo env PATH=$PATH:/usr/local/bin pm2 startup

To test that the application will startup when the container reboots, restart the container.

When the container has restarted, open an SSH Terminal window and check the status of PM2 with the following command:

sudo pm2 status

Install and configure nginx

The web application is running and listening on a private IP address. To make it available for users to access it from the browser, we will set up an nginx web server as a reverse proxy.

Install nginx

In an SSH Terminal window execute the following commands:

sudo apt-get update

# Install the nginx server
sudo apt-get install -y nginx

Configure nginx

In an SSH Terminal window execute the following command:

sudo vi /etc/nginx/sites-available/default

In the vi editor, replace the text in the file with the following:

server {
  listen 80 default_server;

  # Replace with the container url, such as:

  # serving static files
  location ~ ^/(assets/|images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
    root /home/cabox/workspace/myapp/public/;
    access_log off;
    expires 24h;

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_redirect off;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_cache_bypass $http_upgrade;

In the SSH Terminal window run the following command to start nginx:

sudo service nginx restart

To verify that the application is now running, you can execute the following commands:

# Check the status of the nginx service
sudo nginx status

# Check the status of the pm2 service
sudo pm2 status

# Check that the appliation is running properly on port 80

Run the Express Application in the browser

In the Codeanywhere container's toolbar, click the "Run Project" button to launch the Express application in the browser.


  1. Codeanywhere
  2. Express
  3. nginx
  4. PM2
  5. Installing Node.js Tutorial: Ubuntu
  6. How To Use PM2 to Setup a Node.js Production Environment On An Ubuntu VPS


Creating Node.js Web Applications for ASP.NET MVC Developers

Comments: 0

Add a New Comment

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License