Step-by-Step Guide to Installing Node.js and Express on Ubuntu 24.04

install NodeJs

In this guide, we’ll walk you through installing Node.js and the Express framework on an Ubuntu 20.04 server. Node.js, an open-source JavaScript runtime, allows developers to build scalable applications, while Express provides a lightweight web application framework for Node.js. The installation will be consist of several steps :

  1. Update the Package List
  2. Install Node.js
  3. Install the Express Framework
  4. Create a Simple Express Server
  5. Start the Server
  6. Access the Application
  7. Running Express in the Background (Optional)

Prerequisites

Before starting, ensure you have:

  • A server running Ubuntu 20.04 with root or sudo access.
  • Basic familiarity with command-line usage.

Step 1: Update the Package List

First, update your package list to ensure you have access to the latest software versions:

sudo apt update && sudo apt upgrade -y

Step 2: Install Node.js

Option A: Using the NodeSource Repository

For the latest stable Node.js version, use the NodeSource repository:

Add the NodeSource repository:

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

Install Node.js:

sudo apt install -y nodejs

Verify the installation:

node -v
npm -v

Option B: Install from Ubuntu Repository (Older Version)

To use the version available in the Ubuntu repository:

sudo apt install -y nodejs npm

Note: This version might not be the latest.

Step 3: Install the Express Framework

Create a project directory for your Express application:

mkdir myapp01
cd myapp01

Initialize a new Node.js project by creating a package.json file:

npm init -y

Install Express as a dependency:

 npm install express

Step 4: Create a Simple Express Server

Create a new file named app.js:

nano app.js

Add the following code to app.js:

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

app.get('/', (req, res) => {
res.send('Hello, World This is my firs .js app!');
});

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

Save and exit the file.

Step 5: Start the Server

Run the following command to start your server:

node app.js

You should see a message indicating that the server is running:

Server running at http://localhost:3000

Step 6: Access the Application

In your browser, navigate to http://your_server_ip:3000 (replace your_server_ip with your server’s IP address). You should see “Hello, World!” displayed.

Step 7: Running Express in the Background (Optional)

For a production environment, consider using pm2, a process manager for Node.js, to keep the server running:

Install pm2:

sudo npm install -g pm2

Start the app with pm2:

pm2 start app.js

Check the process status:

pm2 status

Conclusion

You’ve successfully installed Node.js and Express on Ubuntu 20.04 and created a simple web server. Express is now ready to handle incoming requests, making it a versatile choice for building scalable web applications.

 

(Visited 59 times, 1 visits today)

You may also like