Skip to main content

Command Palette

Search for a command to run...

Creating Routes and Handling Requests with Express

Updated
6 min read
Creating Routes and Handling Requests with Express
P

Software Engineer | Passionate about Web Development, DSA & Problem Solving. I write simple, practical tech blogs to help developers learn and grow. Exploring JavaScript, C++, Backend & Modern Web Technologies.

Introduction

Building modern web applications requires an efficient way to handle incoming client requests and send meaningful responses. Whether it is a frontend application requesting user data, a mobile app submitting form information, or a browser loading a webpage, backend servers constantly process HTTP requests. While Node.js provides the ability to create servers using its built-in HTTP module, managing routes and request handling manually can quickly become complex as applications grow.

This is where Express.js becomes extremely valuable. Express is a lightweight and flexible framework built on top of Node.js that simplifies backend development by providing clean routing systems, middleware support, and simplified request-response handling. Instead of writing repetitive low-level code, developers can focus on building application logic efficiently.

Express has become one of the most widely used backend frameworks because of its simplicity and scalability. From beginner projects to enterprise-level APIs, Express powers countless web applications worldwide. Understanding how to create routes and handle requests in Express is one of the first major steps toward becoming a backend developer.

In this blog, we will explore what Express.js is, why it simplifies Node.js development, how to create an Express server, and how to handle GET and POST requests with practical examples and clear explanations.

What Express.js Is?

Express.js is a minimal and flexible web application framework for Node.js. It provides tools and features that simplify server-side development.

Without Express, developers must manually handle:

  • Routing

  • Request parsing

  • Response handling

  • Middleware management

Express abstracts these complexities and provides a cleaner API.

Example

const express = require('express');

const app = express();

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

Explanation:

  • require('express') imports Express

  • express() creates an application instance

  • app.listen() starts the server on port 3000

This creates a functional Express server with minimal code.

Why Express Simplifies Node.js Development

Node.js can create servers using the built-in http module, but route management becomes difficult for large applications.

Raw Node.js Server Example

const http = require('http');

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

server.listen(3000);

Express Server Example

const express = require('express');

const app = express();

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

app.listen(3000);

Explanation :

  • Raw Node.js requires manual route checking

  • Express provides clean route methods like app.get()

  • Express improves readability and maintainability

Before vs After

Raw Node.js Express.js
Manual routing Clean routing methods
More boilerplate Minimal code
Harder scalability Easier scaling

Creating Your First Express Server

An Express server listens for incoming requests and responds accordingly.

Example

const express = require('express');

const app = express();

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

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

Explanation :

  • app.get() defines a route

  • '/' represents homepage route

  • req contains request information

  • res.send() sends response to client

  • listen() starts server

When visiting:

http://localhost:3000

Browser displays:

Welcome to Express

Request Lifecycle

  1. Browser sends request

  2. Express matches route

  3. Route handler executes

  4. Response sent back

Understanding Routing in Express

Routing determines how an application responds to different URLs and HTTP methods.

Each route contains:

  • HTTP method

  • Path

  • Callback function

Example

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

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

Explanation :

  • /about handles about page requests

  • /contact handles contact page requests

  • Different URLs trigger different handlers

Handling GET Requests

GET requests are used to retrieve data from the server.

Example

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

Explanation :

  • app.get() handles GET requests

  • /users is endpoint path

  • Server sends response back to client

Practical Use Cases

  • Fetch products

  • Load user profiles

  • Display blog posts

GET Request Characteristics

Feature Description
Purpose Retrieve data
Data Change No
Common Usage APIs and webpages

Handling POST Requests

POST requests are used to send data from client to server.

Before handling POST data, Express must parse incoming JSON.

Example

app.use(express.json());

app.post('/users', (req, res) => {
    console.log(req.body);

    res.send("User created");
});

Explanation :

  • express.json() parses JSON request body

  • app.post() handles POST requests

  • req.body contains incoming data

Example Request Body:

{
  "name": "Peeyush"
}

Practical Use Cases

  • User registration

  • Login forms

  • Creating database records

Sending Responses in Express

Express provides multiple response methods for sending data back to clients.

Common methods:

  • res.send()

  • res.json()

  • res.status()

Example

app.get('/api', (req, res) => {
    res.status(200).json({
        message: "Success"
    });
});

Explanation :

  • status(200) sets HTTP status code

  • json() sends JSON response

  • Common in REST APIs

Response Types Comparison

Method Purpose
send() Send text/HTML
json() Send JSON
status() Set status code

Request → Route Handler → Response Flow

Express follows a clean request-processing structure.

Execution Flow

  1. Client sends request

  2. Express checks matching route

  3. Route handler executes

  4. Response returned

Example

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

Explanation :

  • Client requests /hello

  • Express finds matching handler

  • Callback executes

  • Response sent immediately

Conclusion

Express.js simplifies backend development by providing a clean and organized way to create routes and handle HTTP requests. Instead of manually managing request handling using Node’s native HTTP module, developers can use Express’s intuitive routing system to build scalable applications efficiently.

In this blog, we explored how Express works, why it simplifies Node.js development, and how to create an Express server from scratch. We also learned how routing works, how GET and POST requests are handled, and how responses are sent back to clients.

The key takeaway is that Express abstracts repetitive backend logic while still giving developers flexibility and control. Its routing system, middleware support, and simplified request-response handling make it one of the most powerful tools for modern backend development.

For beginners, mastering Express routing is the foundation for building APIs, authentication systems, and full-stack applications in the future.