Introduction
Express.JS is a minimal and flexible, Node.JS web application framework which provides a lot of features for developing web and mobile applications. using thousands of HTTP utility methods and middleware at your disposal creating an API is very quick and easy. In this tutorial, we are going to install a basic Express.JS web server, using Node.JS, a Javascript runtime based on Chrome’s V8 engine, on an Ubuntu 16.04 system.
Installing Node.JS
In this section, we are going to install the latest stable version of Node.JS which is 8.9.3 LTS at the time of writing. First of all update your repository list with the following command:
apt-get update
Now you have to add the Node.JS official repository with the command below:
curl -sL https://deb.nodesource.com/setup_8.x | -E bash -
Execute the following command to install Node.JS:
apt-get install nodejs
Before installing Express.JS, move to a proper directory that you want to store your project (e.g. “/home/myproject/”):
cd /home/myproject/
Then we use Node.JS init feature to create a “package.json” where all of our modules will be store:
npm init
Follow the prompts then you will see something like below when you are finished:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Installing Express.JS
Next, you can install the Express.JS module with the following command:
npm install express
To make this even easier, we will use a neat little module called “express-generator”. It will generate a skeleton website for us, It takes care of the basic stuff, like routes and such. To install Express Generator, simply execute:
npm install -g express-generator
We are going to install “EJS” rendering engine for our project, you can set it up using the following command:
express -v ejs test
It will create a new directory (test) in our project. Now you have to switch into it and execute the following command to make sure that you have all of the required modules:
cd test/
npm install
Now you can start your Express.JS project using the following command:
npm start
You can check if everything works by seeing your Public IP Address or your Domain in your browser. you will see a page like below:
http://YOUR_IP_OR_DOMAIN:3000
Check out Express.JS official website to access the documentation and more information!