Sep 28, 2024 7 min read

How to Install Express.js on Ubuntu 22.04

Install Express.js on Ubuntu 22.04 with our step-by-step tutorial. It is a popular and lightweight web application framework for Node.js.

Install Express.js on Ubuntu 22.04
Table of Contents

Introduction

Before we discuss how to install Express.js on Ubuntu 22.04, let's briefly understand-What is Express.js?

Express.js is a popular and lightweight web application framework for Node.js. It simplifies the process of building web applications and APIs by providing a robust set of features and tools.

This tutorial will walk you through the installation process of Express.js on Ubuntu 22.04, highlighting its advantages, addressing common FAQs, and providing a conclusion.

Advantages

  1. Simplicity: Express.js is designed to be lightweight and straightforward, providing a simple and intuitive syntax for creating web applications.
  2. Flexibility: Express.js offers a minimal core framework that allows developers to choose and integrate additional modules or libraries as needed.
  3. Middleware Support: Express.js provides a middleware architecture, enabling developers to add powerful functionality to their applications by utilizing middleware components.
  4. Routing: Express.js simplifies the process of defining and managing routes, making it easy to handle different HTTP requests and URL patterns.
  5. Active Community: Express.js has a large and active community of developers, ensuring an extensive range of resources, documentation, and third-party libraries are available.

Section 1: Setup Node.js Environment for Express.js on Ubuntu 22.04

The backbone of any Express.js project is Node.js, as it allows the execution of JavaScript code on the server side. This section aims to provide a step-by-step tutorial to setting up Node.js on your Ubuntu system.

Step 1: Refreshing Your Ubuntu System

Initiating any new installation process on an Ubuntu system calls for an updated software environment. By updating your system’s existing packages, you’re ensuring the most recent versions of all installed software are up-to-date, mitigating the potential for software conflicts.

Execute the following command to update your system:

sudo apt update && sudo apt upgrade

The above command has two components: sudo apt update updates the package lists for upgrades, and sudo apt upgrade upgrades all packages that are updatable.

Step 2: Install Initial Packages

As part of the Node.js setup, we’ll need to install several additional packages that facilitate a smooth installation process. These include curl for data transfer via URLs, git for version control, and wget for network downlinking.

Run the command below to install these packages:

sudo apt install curl git wget -y

The -y flag is added at the end to automatically answer ‘yes’ to any prompts, thereby allowing the installation to proceed without requiring manual intervention.

Step 3: Import the NodeSource Repository on Ubuntu

Our choice for installing Node.js is the NodeSource repository. Known for providing the latest versions of Node.js, it’s an ideal choice for our setup.

We first need to import the repository by executing a script provided by NodeSource. The following command fetches the script and runs it:

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo bash -

In this command, curl -fsSL https://deb.nodesource.com/setup_lts.x fetches the script from the provided URL, and | sudo bash - pipes it to bash, which executes the script with root permissions.

Next, let’s update the system’s package lists to include the newly added packages from the NodeSource repository:

sudo apt update

Step 4: Install Node.js for Express.js on Ubuntu 22.04

With the preliminary setup complete, it’s time to install Node.js. Use the following command to initiate the installation from the NodeSource repository:

sudo apt install nodejs

Step 5: Verifying the Node.js Installation on Ubuntu

After the installation, it’s prudent to validate the Node.js installation to ensure it’s ready for use. This validation confirms that the installation process was successful.

To verify your Node.js installation, use the following command:

node --version

The command will output the installed version of Node.js, thereby confirming a successful installation.

Section 2: Install Express.js on Ubuntu 22.04

Following the installation of Node.js, the next step is deploying Express.js. Express.js is a potent element of our Node.js landscape, being a minimalist yet versatile web application framework that supplies a comprehensive range of features for web and mobile applications.

Step 1: Update NPM to the Most Recent Version on Ubuntu

As a preliminary action, before the Express.js setup, we must guarantee that npm (Node Package Manager) is current. Npm acts as the default package manager for Node.js and is indispensable for administering Node.js packages. Regularly updating npm ensures we can access the latest functionalities and security updates.

To bring npm up to the most recent version, we execute the following command:

sudo npm install npm@latest -g

In this command, npm install npm@latest retrieves the most recent version of npm, and -g installs it globally, making it available across the entire system.

Step 2: Initiation of Node.js Project (Express.js) on Ubuntu

As we begin installing Express.js, our initial task is to set up a new Node.js project. This can be achieved by employing npm, which is automatically installed along with Node.js.

We navigate to the desired project directory, and for the sake of demonstration, we will create a new one:

mkdir expressjs-test && cd expressjs-test

Next, execute the following command to initiate a new Node.js project:

npm init -y

In this command, the -y flag is used to automatically fill in the default information in the setup configuration. The npm init command generates a package.json file in our project directory, which supplies the necessary metadata about our project.

Step 3: Install Express.js on Ubuntu 22.04

With a newly established Node.js project in place, the subsequent step is implementing Express.js. Once again, we will employ npm to install the Express.js package.

In our project directory, we execute the command below to install Express.js:

npm install express --save

In this command, npm install express obtains the Express.js package from the npm registry and installs it in our project. The --save flag ensures that the Express.js package is appended to the list of dependencies in the package.json file, highlighting its importance for our project.

While npm install express --save is generally the preferred method, you may occasionally find the npm install express --save-dev command useful in certain situations where you want Express.js as a development dependency.

Step 4: Verifying the Express.js Installation on Ubuntu

Following the Express.js installation, it’s always recommended to validate the operation. The aim is to ensure that Express.js is correctly installed and ready for use in your Node.js project.

The package.json file should now list Express.js in the dependencies section. You can verify this by viewing the package.json file with the following command:

cat package.json

The successful installation of Express.js should be reflected in the dependencies section of the package.json file.

Successfully installed Express.js dependencies on a test project in Ubuntu 22.04 Linux.

Section 3: Create a Simple Express.js Application on Ubuntu 22.04

Finally, test our Node Express.js environment by creating a simple Express.js application. Create a new file called app.js in your project directory. You can use your preferred text editor for this purpose.

For instance, if you’re using nano, you’d use:

nano app.js

In this file, insert the following Express.js code:

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

app.get('/', (req, res) => {
  res.send('Hello World! from https://www.vegastack.com');
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

This code sets up a basic Express.js application that listens on port 3000 and responds with “Hello World!” when you access the root URL (http://localhost:3000).

To start the Express.js application, use the following command:

node app.js

The command node app.js launches the Express.js application. You will see the message “Example app listening at http://localhost:3000 in your console if everything is configured correctly.

Navigate to http://localhost:3000 in your web browser, you should be greeted with the “Hello World!”. This indicates a successful setup of the Node Express.js environment.

 Express.js browser app running on Ubuntu 22.04 Linux.

FAQs to Install Express.js on Ubuntu 22.04

How do I create a new Express.js application in Ubuntu 22.04? 

After installing Express, create a new directory, navigate to it in the terminal, and run npx express-generator to scaffold a new Express.js application.

How do I run an Express.js application in Ubuntu 22.04?

Navigate to the directory of your Express.js application in the terminal and run npm start or node app.js to start the application. It will listen on the specified port (default is 3000).

Can I use a different template engine with Express.js? 

Yes, Express.js supports various template engines. By default, it uses Jade (now known as Pug), but you can replace it with other template engines like EJS or Handlebars.

How do I handle static files in Express.js? 

Use the express.static middleware in your Express.js application to serve static files such as CSS, JavaScript, and images. Specify the directory where your static files are located.

Can I use Express.js to create RESTful APIs? 

Yes, Express.js is widely used for building RESTful APIs. It provides robust routing and middleware support, making it well-suited for this purpose.

How do I handle error handling in Express.js? 

Express.js provides a middleware function for error handling. You can define error-handling middleware using the app.use method, specifying an error-handling function with four parameters.

Can I use WebSocket functionality with Express.js?

Yes, Express.js can be integrated with WebSocket functionality using libraries like Socket.IO or the native ws module in Node.js.

Conclusion

In this tutorial, we have provided comprehensive steps to install and configure Express.js on Ubuntu 22.04.

If you have any queries, feel free to ask them in the comments section, and we would be happy to respond to them....

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.