Introduction to Express.js

🌐 What is Express.js?

Express.js is a fast, minimalist, and flexible web framework for Node.js. It simplifies building web applications and RESTful APIs by providing a clean routing system, middleware support, and utilities for handling HTTP requests and responses.

Information

Express.js is one of the most widely used frameworks in the Node.js ecosystem because it provides just enough structure while remaining highly flexible.

🚀 Why Use Express.js?

  • Lightweight and easy to learn.
  • Powerful routing capabilities.
  • Supports middleware for request processing.
  • Ideal for building REST APIs and web applications.
  • Large ecosystem and community support.

🏗 How Express.js Fits into a Web Application

Client (Browser)
HTTP Request
HTTP Response
Express Server
Middleware
Route Handler
Authentication
Logging
Validation

đŸ“Ļ Installing Express.js

Before installing Express, ensure that Node.js and npm are installed on your system.

Create a New Project

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

Install Express

npm install express

📝 Creating Your First Express Server

index.js

const express = require('express');

const app = express();

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

const PORT = 3000;

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

Run the Server

node index.js

Success

Open http://localhost:3000 in your browser to see your first Express application running.

âš™ī¸ Core Concepts

An Express application is created using express(). It acts as the central object responsible for handling incoming requests.

Routes determine how your application responds to different URLs and HTTP methods such as GET, POST, PUT, and DELETE.

Middleware functions execute before the final route handler. They can modify requests, validate data, authenticate users, or log information.

Responses are sent using methods such as res.send(), res.json(), and res.status().

đŸ›Ŗ Defining Routes

Basic Routes

app.get('/users', (req, res) => {
  res.send('Get all users');
});

app.post('/users', (req, res) => {
  res.send('Create a new user');
});

🧩 Middleware Example

Custom Middleware

app.use((req, res, next) => {
  console.log(req.method, req.url);
  next();
});

Middleware executes in sequence. Calling next() passes control to the next middleware or route handler.

📋 Common HTTP Methods

MethodPurposeExample
GETRetrieve data/users
POSTCreate data/users
PUTUpdate data/users/1
DELETEDelete data/users/1

📚 Request Lifecycle

💡 Best Practices

  • Organize routes into separate files.
  • Use middleware for reusable functionality.
  • Handle errors consistently.
  • Use environment variables for configuration.
  • Validate incoming request data.

Best Practice

As your application grows, separate concerns by organizing routes, controllers, middleware, and configuration into dedicated folders.

📖 Learn More

Explore the official documentation at Express.js Documentation for advanced topics such as routing, template engines, security, and deployment.

>>"Simplicity is the ultimate sophistication." — A philosophy that perfectly matches the design of Express.js.

đŸŽ¯ Summary

Summary

Express.js provides a simple yet powerful foundation for building web applications and REST APIs with Node.js. By understanding routing, middleware, request handling, and application structure, you can build scalable backend applications efficiently.