Skip to main content

Command Palette

Search for a command to run...

Getting Started with Express.js

Updated
•4 min read•View as Markdown

Express.js is one of the most popular frameworks for building backend applications with Node.js. It simplifies server creation, routing, and request handling, allowing developers to build APIs and web applications faster.

In this guide, you will learn what Express.js is, why developers use it, and how to create your first Express server.


What Express.js Is

Express.js is a lightweight web framework built on top of Node.js.

It provides tools and features for:

  • Creating servers

  • Handling routes

  • Managing requests and responses

  • Building APIs

Key Idea:

Express simplifies backend development by reducing boilerplate code.


Why Express Simplifies Node.js Development

Creating servers using raw Node.js can become repetitive and difficult for large applications.

Raw Node.js Example:

const http = require("http");

const server = http.createServer((req, res) => {
  if (req.url === "/") {
    res.end("Home Page");
  }
});

server.listen(3000);

This works, but handling multiple routes and methods becomes messy.


Express Server Example

Express Version:

const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Home Page");
});

app.listen(3000);

Benefits:

  • Cleaner syntax

  • Easier routing

  • Better readability

  • Faster development


Installing Express.js

First, initialize a Node.js project:

npm init -y

Install Express:

npm install express

Creating Your First Express Server

Create a file named:

server.js

Add the following code:

const express = require("express");

const app = express();

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

Run the server:

node server.js

Request → Route Handler → Response Flow

Client Request
      ↓
Express Route
      ↓
Route Handler Executes
      ↓
Response Sent Back

Understanding Routing in Express

Routing means deciding how the server responds to different URLs and HTTP methods.

Example:

app.get("/", (req, res) => {
  res.send("Welcome");
});

Here:

  • / = route

  • GET = request method

  • Callback function = route handler


Handling GET Requests

GET requests are used to retrieve data.

Example:

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

Access:

http://localhost:3000/about

Handling POST Requests

POST requests are used to send data to the server.

Example:

app.post("/user", (req, res) => {
  res.send("User Created");
});

Common Usage:

  • Form submission

  • Creating new records

  • Sending API data


Sending Responses

Express provides several response methods.

res.send()

Sends text or data.

res.send("Hello");

res.json()

Sends JSON data.

res.json({
  name: "Akhtar",
  role: "Developer"
});

Express Routing Structure Visualization

GET /           → Home Route
GET /about      → About Route
POST /user      → Create User Route

Minimal Express Server Example

const express = require("express");

const app = express();

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

app.listen(3000);

Why Developers Prefer Express

Express helps developers:

  • Build APIs quickly

  • Organize routes easily

  • Handle requests cleanly

  • Reduce repetitive code

  • Scale applications better


Suggestions (Best Practices)

  • Keep routes simple and readable

  • Understand HTTP methods clearly

  • Compare Express with raw Node.js to understand benefits

  • Learn routing before middleware

  • Practice building small APIs

  • Use meaningful route names


Conclusion

  • Express.js is a lightweight framework for Node.js

  • It simplifies routing and server creation

  • GET and POST requests are easy to handle

  • Express improves code readability and development speed

Learning Express.js is an important step toward building modern backend applications and REST APIs.