Skip to main content

Command Palette

Search for a command to run...

Creating Routes and Handling Requests with Express

Updated
3 min read
Creating Routes and Handling Requests with Express

When we building web applications using Node.js, writing code using the built-in HTTP module can be complex and repetitive.

To make development easier, we use Express.js.

1. What is Express.js?

Express.js is a web framework for Node.js.

It helps to:

  • Create servers easily

  • Handle requests and responses

  • Manage routes (URLs)

  • Build web applications faster

Why Developers Prefer Express

  • Simple and clean syntax

  • Easy routing system

  • Saves time

  • Widely used in real projects


2. Why Express Simplifies Node.js Development

Without Express, we have to use the HTTP module, which requires more code.

Example: Raw Node.js Server

import http from 'node:http'

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

server.listen(3000);

Problems:

  • More manual work

  • Hard to manage routes

  • Less readable

Example: Express Server

import express from 'express';
const app = express();

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

app.listen(3000);

Benefits:

  • Less code

  • Easy routing

  • Clean and readable


3. Creating First Express Server

Step 1: Install Express

npm install express

Step 2: Create Server

import express from 'express';
const app = express();

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

4. Handling GET Requests

GET requests are used to fetch data.

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

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

Each URL is called a route

Routing Concept

Routing means: "What should happen when a user visits a specific URL?"

Example:

  • / → Home page

  • /about → About page

  • /contact → Contact page


5. Handling POST Requests

POST requests are used to send data to the server.

app.use(express.json());

app.post("/data", (req, res) => {
  console.log(req.body);
  res.send("Data received");
});

Used in:

  • Forms

  • APIs

  • Login systems


6. Sending Responses

Express provides simple methods to send responses:

res.send("Hello");        // send text
res.json({ name: "John" }); // send JSON
res.status(200).send("OK"); // status + message

Request → Response Flow

  Client Request
       ↓ 
Route(app.get/app.post) 
       ↓ 
Route Handler Function 
       ↓ 
Response(res.send) 
       ↓
Client Receives Data

Simple Example

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

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

app.get("/user", (req, res) => {
  res.json({ name: "Rahul", age: 20 });
});

app.post("/login", (req, res) => {
  res.send("Login successful");
});

app.listen(3000);

Conclusion

Express.js makes working with Node.js much easier. It simplifies server creation, routing, and handling requests.

If you want to build web applications quickly and efficiently, Express is a great choice.

Express turns complex server code into simple and readable code.