You could earn up to $300 by adding new articles!

Get Started Now

Setting up an Express.js Web Server on Ubuntu 16.04 LTS

Published on: Mon, Aug 14, 2017 at 5:15 pm EST

In this tutorial, we will be installing a basic Express.js web server, using Node.js, a Javascript runtime based on Chrome's V8 engine, on our Vultr VPS running Ubuntu 16.04. If you don't know, Express.js is a minimal and flexible Node.js framework that gives you the power to write web applications. It really showcases what Node.js can do, in terms of web development. So without further ado, let's get started!

Installing Node.js

To begin, you'll first need to install Node.js, which will be the backend for our Express site. To install it on Ubuntu 16.04, we'll first need to add the repository for the latest version. To do that, type the following:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

What this does, is it grabs the latest 8.x packages from nodesource.com, and sets it up as an APT repository. When that's finished, run the following to install Node:

sudo apt-get install -y nodejs build-essential

What this is doing is installing Node.js itself, and it grabs build-essential, which are some tools you may need when compiling modules. When that is done, we will create a new Node project. First, make sure you are in your /home directory.

/home/yourname

To check, just type pwd in your terminal, and if you see /home/yourname, then you're all set! Then, we will be using npm's init feature to create a package.json where all of our modules will be kept track of. To do that, type npm init in your terminal. Follow all of the prompts, and you will see this when you are finished:

{
  "name": "example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "rich",
  "license": "MIT"
}

Then, click enter to confirm.

Installing Express

Next, we will install our Express module. To do that, just type:

npm install express

Wait until it finishes installing.

To make life easier, we will use a neat little module called express-generator. What this will do is it will generate a skeleton website for us, making out life a lot easier. It takes care of the basic stuff, like routes and such. To install Express Generator, simply type:

npm install express-generator -g

We specify the -g flag, which means that it will be global, and we can use it throughout our system. Once we finish installing it, we have to make a decision. We have to choose what rendering engine we want to use for our project. For brevity, we will use ejs in this tutorial, which is an awesome rendering engine that I highly recommend you look more into. To setup our bare project, just type:

express -v ejs mysite

This will make a new folder inside of our existing project, with the name mysite. Now, to make sure we have all of the required modules for the project, run the following command.

npm install

Now that we have the basic website setup, we can start it! To start the website, with a basic port, simply type:

DEBUG=mysite:* npm start

The website will now start. The only issue is, you won't be able to see the website, since you are using a terminal. In order to view the example page, we will have to specify an IP address to the HTTP server, which will be the IP address of our VPS. To obtain your IP address, simply go to your Vultr VPS panel, and click on the servers tab. It should be right below your server name, on the bottom. For example: "1024 MB Server - yourIP". Copy that IP address, and keep it close, because we will need it. Then, go back to your server, and change your directory to the bin folder.

cd mysite/bin

This is assuming you created the folder in your home directory. Edit the file inside: the www file. To do that, we will use the built-in editor, Nano.

nano www

Once inside, navigate to this line:

server.listen(port);

Add your IP address into that line. Edit it so it looks like this:

server.listen(port, 'your ip here');

Then, press "control-o" on your keyboard, and "enter" to save. Press "control-x" to exit. Now, we are all set to start our server. Simply type the same command we typed earlier to test it:

DEBUG=mysite:* npm start

The console will now output the port it is listening on. Keep track of this port, as you will need it. To view your site, open your web browser and navigate to the following URL.

http://yourIP:port

For example, it might look like this:

http://192.0.2.0:3000

If all went well, you will see "Welcome to Express" on your screen!

And you're done! You have created your first Express site with Node.js, running off a Vultr VPS. To learn more about Express, and what you can do with it, I recommend going over to their site at http://expressjs.org, and the EJS website at http://ejs.co, where you can read up on the documentation, and create your own killer site!

Want to contribute ?

You could earn up to $300 by adding new articles!

Get started in the SSD Cloud!