In this tutorial, we will show you how to set up your Ubuntu with Node.js and Express.js and write your first Hello World Express.js app on Ubuntu.

Express is a minimal and flexible Node.js web application framework that provides a robust set of applications for web and mobile applications.

Before starting

For this tutorial, you will only need access to the Ubuntu system. We wrote this tutorial for a Ubuntu 20.04 Server, but the directive will also work on the desktop version or any other Ubuntu-based distro. You can get it on a cheap Ubuntu server Jmsolution.

You will need a root user for this tutorial, or run sudo where necessary.

Step 1: update ubuntu and install nano, curl

Your first step should always be to update your Ubuntu system. Run the following command:

apt-get update && apt-get upgrade -y

For this tutorial, we will use nano and curl, so if you have not already installed them, run the following command:

apt-get install nano curl -y

Step 2: Install Node.js

Since Express is a Node.js framework, you must first install Node.js.

To install the current version of Node.js (v15 as of writing), run the following nodes commands:

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
apt-get install nodejs -y

Or if you want to install the latest LTS release (v14 as of writing) of Node.js, run the following command:

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
apt-get install nodejs -y

To check which version of Node.js is installed on your system (if any), run

node -v

Which should return the version of Node.js you are currently using.

Step 3: Create a directory and necessary JS files for your Express app

The next step is to first create and navigate to a directory:

mkdir linuxstanjs && cd linuxstanjs

Next, use the following command to create the package.json file:

npm init

The command will give you some hints that are all self-explanatory. You can leave everything for the default values ​​and just enter a hit for everything.

Step 4: Install Express

Now for the main part of our tutorial. While you’re still in the directory you created earlier, run the following command to install Express.js on Ubuntu:

npm install express --save

And all! Express is installed. Now we will write our first Hello World app.

Step 5: Create a Hello World Express app

To create the Sim Express Hello World app, create a file in your new directory (linuxstanjs in our case):

nano expressapp.js

You can name it whatever you want.

And paste the following code:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello Linux Stans!')
})

app.listen(port, () => {
  console.log(`Your first Express app is successfully running! You can view the output of this app at http://localhost:${port}`)
})

This application is a simple “Hello Linux stance!” message. To run the application, use the following command:

node expressapp.js

Which should start the app. Now you can see the output in your browser at http: // localhost: 3000 or http: //.

If you are using a firewall, you can allow the port:

ufw allow 3000

And all! This is your first Express Hello World app. Subscribe for more express. Beginner friendly tutorials like this.