Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It is designed to build single-page, multi-page, and hybrid web applications efficiently. Express.js simplifies the process of handling HTTP requests, routing, middleware, and more.

Key Features of Express.js

  • Minimal and Flexible: Express.js is unopinionated, meaning it provides the basic tools to build web applications without enforcing a specific structure.
  • Middleware Support: Middleware functions can be used to handle requests, responses, and errors.
  • Routing: Express.js provides a powerful routing mechanism to handle different HTTP methods and URL paths.
  • Template Engines: It supports various template engines like Pug, EJS, and Handlebars for rendering dynamic content.
  • Extensible: Easily extendable with numerous third-party packages available via NPM.

Setting Up Express.js

Step 1: Install Node.js and NPM

Ensure you have Node.js and NPM installed on your machine. You can download and install them from Node.js official website.

Step 2: Create a New Project Directory

Create a new directory for your Express.js project and navigate into it:

mkdir my-express-app
cd my-express-app

Step 3: Initialize a New Node.js Project

Initialize a new Node.js project using NPM:

npm init -y

This command creates a package.json file with default settings.

Step 4: Install Express.js

Install Express.js as a dependency:

npm install express

Your First Express.js Application

Let's create a simple Express.js application that responds with "Hello, World!" when accessed via a web browser.

Step 1: Create the Application File

Create a new file named app.js in your project directory and add the following code:

// Import the Express module
const express = require('express');

// Create an instance of an Express application
const app = express();

// Define a route for the root URL ("/")
app.get('/', (req, res) => {
  res.send('Hello, World!');
});

// Start the server and listen on port 3000
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

Step 2: Run the Application

Run your Express.js application using Node.js:

node app.js

You should see the message Server is running on http://localhost:3000 in your terminal.

Step 3: Access the Application

Open your web browser and navigate to http://localhost:3000. You should see the message "Hello, World!" displayed on the page.

Explanation of the Code

  • Import the Express module: The require('express') statement imports the Express module.
  • Create an instance of an Express application: The express() function creates an instance of an Express application.
  • Define a route: The app.get('/', (req, res) => { ... }) method defines a route for the root URL ("/"). When a GET request is made to the root URL, the callback function is executed, sending the response "Hello, World!".
  • Start the server: The app.listen(PORT, () => { ... }) method starts the server and listens on the specified port (3000 in this case). The callback function logs a message indicating that the server is running.

Practical Exercise

Exercise: Create a Simple Express.js Application

  1. Create a new directory named exercise-express-app.
  2. Initialize a new Node.js project in the directory.
  3. Install Express.js.
  4. Create an app.js file with the following requirements:
    • Define a route for the root URL ("/") that responds with "Welcome to Express.js!".
    • Define a route for "/about" that responds with "About Page".
    • Start the server on port 4000.

Solution

mkdir exercise-express-app
cd exercise-express-app
npm init -y
npm install express

Create an app.js file with the following content:

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

app.get('/', (req, res) => {
  res.send('Welcome to Express.js!');
});

app.get('/about', (req, res) => {
  res.send('About Page');
});

const PORT = 4000;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

Run the application:

node app.js

Access the application in your web browser:

  • http://localhost:4000 should display "Welcome to Express.js!".
  • http://localhost:4000/about should display "About Page".

Summary

In this section, you learned about the basics of Express.js, including its key features and how to set up a simple Express.js application. You also created a basic application that responds to different routes. In the next module, we will dive deeper into setting up an Express application and explore middleware, routing, and error handling.

Node.js Course

Module 1: Introduction to Node.js

Module 2: Core Concepts

Module 3: File System and I/O

Module 4: HTTP and Web Servers

Module 5: NPM and Package Management

Module 6: Express.js Framework

Module 7: Databases and ORMs

Module 8: Authentication and Authorization

Module 9: Testing and Debugging

Module 10: Advanced Topics

Module 11: Deployment and DevOps

Module 12: Real-World Projects

© Copyright 2024. All rights reserved