Linux, Web Hosting, and Everything Else in Between
Linux, Web Hosting, and Everything Else in Between

Getting Started with Express.js: Server Setup

getting started with express.js

This tutorial demonstrates how to use install Node.js on an Ubuntu server and how to run your first Express app.

Prerequisites

To follow this tutorial, you’ll need:

  • An Ubuntu server. You can also run Node and Express on your local computer, but this tutorial has specific instructions for an Ubuntu server, which may also work on other setups. You can get an Ubuntu 16.04 server from Vultr for $2.5 per month (it’s free if you use credits), or you can compare other cloud hosting providers.
  • An SSH client. You can try MobaXterm or PuTTy
  • Root (sudo) access to the server. All commands below are executed by the root user. If you’re using a non-root user, then append ‘sudo’ to the appropriate commands.

Updating the server

Whatever you do on your server, you should start by updating the server with the following command:

apt-get update && apt-get upgrade

Installing Node.js

First thing you need to do is install Node.js so you can run and use Express.

You can simply install Node.js by running ‘apt-get install nodejs’, but if you want to install a more recent version, you should do it by running the following commands:

curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh

The command above will download an install script for the (currently latest) LTS version of Nodejs.

To run the script, use this command:

bash nodesource_setup.sh

Wait for the script to finish, and finally install Nodejs by running:

apt-get install nodejs build-essential

npm is already installed, but to use some of npm’s features, you’ll need to install build-essential too.

And you’re done. Nodejs is installed. Now you’ll mostly work with node’s package manager (npm)

Installing Express.js

Easiest way of installing Express is by using the express-generator:

npm install express-generator -g

The appended -g means that it will be installed globally for the whole system.

We also need a Nodejs engine so we can run and view our app. We’ll use dot.js, as it’s considered the fastest engine:

npm install express-dot-engine

Creating your first Express.js app

And now, to the fun part. We’ll create our first website/app.

First, create the actual website/folder:

express myFirstExpressApp

Now navigate to the new folder and make sure you have all dependencies installed:

cd myFirstExpressApp
npm install

You can already run your new app and view your website on the default port and localhost, but we’ll configure the server first.

Configuring the server

First, navigate to the bin folder:

cd bin

(the folder is ~/myFirstExpressApp/bin).

We can configure the server by editing the ‘www’ file:

nano www

Find the ‘server.listen’ line and update it:

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

Of course, add your server’s IP. Save the file.

Now, to run the website/app, run the following command:

DEBUG=myFirstExpressApp:* npm start

You can now test your app and see if everything’s working by visiting:

http://your.server.ip.address:3000

You should see the Welcome to Express page.

And that’s it. You’re done.

If you’re interested in a series of Express.js tutorials, follow us, we’ll (most probably) be publishing more tutorials soon.

Leave a comment

Your email address will not be published. Required fields are marked *