Deploying a Node.js/Express Application on Digital Ocean (Part 1)

Ethan Nwankwo
CloudBoost
Published in
4 min readJun 3, 2019

--

Droplet

Introduction

Digital ocean is a platform that allows us to run applications in the cloud, just as we would locally. In simple terms, we can view digital ocean as a platform that gives us access to a computer running our desired platform online where we can setup our applications and have people access them, these are called droplets.

Requirements

Audience

Anyone wishing to deploy a node/express application on digital ocean.

Sample Node.js/Express application

This is a very basic application that says “Hello World” on the browser and “Hello World API” on the API backend

Install the node packages by running the following command:

npm install --save express path

Create the following files ( index.html and index.js)in the same folder:

Then run:

node index.js

This should start the app on port 5000.

Verify the app is started by navigating to localhost:5000 and localhost:5000/api on the browser.

Once the app runs successfully, push it to Github, let’s get it deployed!

While still on the terminal, generate an SSH key if you don’t already own one, it generally implies running the following command:

ssh-keygen -t rsa -C "your_email@example.com"

Get your public key handy, we’ll need it soon. The public key is usually stored in a .pub file usually in the directory similar to:
/Users/<my name>/.ssh/id_rsa.pub

Deploying on digital ocean

To deploy the app, we take the following steps:

  1. Create a droplet

Log into your digital ocean account and select create new droplet

  • On the “Choose an Image” option, click on *One-click apps* and select the“NodeJS” option.
  • On the next option to choose a size, given the size of our application, we can choose the $10/mo option.
  • Leave all the other options on default and go to the “Add New SSH” option and paste your public key
  • Then under “Finalise and Create” option, specify the desired name for the droplet and proceed to create.

2. Install the project in the droplet

  • Open a new terminal on your local computer and ssh into the droplet using the public ip of the droplet shown on digital ocean by typing the command below:

Note: In certain cases, especially when there’ll be several users accessing the droplet, it is recommended to create different users with different access control levels who can access the droplet, this is done generally to restrict access to the droplet. For simplicity, we’ll assume it’s only the root user that’ll have access to this droplet.

ssh root@<droplet ip address>

This should log you into the droplet

  • Clone the github project by running the command
git clone <github project link> (i.e https://github.com/<username>/<project_name>.git)
  • Drop into the newly generated project directory, then pull the project from github and install the npm packages using the following command:
cd <project_name> && git pull origin master && npm install

Note: If the project installation freezes for a long while and/or eventually fails, it is very likely that the droplet size and memory may be insufficient to run the project, hence may need to be increased.

3. Start the project in the droplet

Before we start the application in the droplet, we’ll need to allow tcp connections to our application’s port. By default, connections are disabled to all ports except 22 and 80. To do this, we’ll run the following command on the terminal (since port 5000 is our application port):

sudo ufw allow 5000/tcp

We now proceed to start the project from within the droplet by running

node index.js

This should start the app on the specified port (i.e 5000 in our case).

The app can now be accessed on a browser by entering the droplet url followed by the port:

http://<droplet ip address>:<port>

If everything runs as expected, then we’ve been able to deploy our application to digital ocean.

Note: Notice that stopping the application on the droplet terminal automatically pulls down the application, we’ll handle this in the next part using pm2.

Summary

In this post we have been able to see the steps in creating and running a NodeJS application on digital ocean.

In the next post of this series, we’ll explore options of running the application in production using pm2 as a process manager, setting up reverse proxy using nginx and assigning an ssl certificate to the application domain in order to serve it over https.

Let’s keep the conversation going, feel free to drop a line in the comment section.

Interested in cryptocurrency investments? check out my post on buying and investing.

Visit my profile for other publications I’ve made.

--

--