Skip to main content

Command Palette

Search for a command to run...

Creating Routes and Handling Requests with Express

Published
5 min read
Creating Routes and Handling Requests with Express

Once developers start building backend applications with Node.js, one of the first things they realize is that creating servers using Node’s built-in HTTP module can become repetitive very quickly.

Even simple tasks like:

  • handling routes

  • managing requests

  • sending responses

  • parsing data

require a lot of manual code.

That’s exactly why:

Express.js

became so popular.

Express is a lightweight web framework built on top of Node.js that makes backend development much simpler, cleaner, and faster.

In this article, we’ll understand what Express.js is, why developers use it, and how to create routes and handle requests using Express.


What Express.js Is ?

Express.js is a minimal and flexible web framework for Node.js.

It helps developers build:

  • APIs

  • web servers

  • backend applications

  • REST services

without writing repetitive low-level server code again and again.

Think of Node.js as the engine of a car.

Express.js is like the steering wheel, dashboard, and controls that make driving easier.

Node.js provides the core functionality, while Express provides a cleaner and more developer-friendly way to work with servers and routes.


Why Express Simplifies Node.js Development

To understand why Express became popular, let’s compare it with a raw Node.js HTTP server.

Raw Node.js HTTP Server

Here’s a simple server using Node.js only :

import http from 'http';

const server = http.createServer((req, res) => {

  if (req.url === "/") {
    res.send("Home Page");
  }

  else if (req.url === "/about") {
    res.send("About Page");
  }

});

server.listen(3000);

This works, but as the application grows:

  • routing becomes messy

  • request handling becomes repetitive

  • middleware becomes difficult

  • code organization suffers

Now compare that with Express.

Express Server Example


import express from 'express';

const app = express();

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

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

app.listen(3000);

The difference is immediately noticeable.

The code is:

  • shorter

  • cleaner

  • easier to read

  • easier to scale

That simplicity is one of the biggest reasons developers love Express.


Creating First Express Server

Now let’s create a basic Express server step-by-step.

Create a file called:

app.js

Then add this code:


import express from 'express';

const app = express();

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

Here’s what’s happening:

  • express() creates an Express application

  • app.listen() starts the server

  • port 3000 becomes active

Run the file using:


node app.js

Understanding Routes in Express

A route is simply a path that the server responds to.

For example:

  • /

  • /about

  • /contact

Different routes usually return different data or pages.

Express makes routing extremely simple using methods like:

  • app.get()

  • app.post()

  • app.put()

  • app.delete()

Each route contains:

  1. a URL path

  2. a request handler function


Handling GET Requests

GET requests are mainly used to retrieve data from the server.

Here’s a simple example:

import express from 'express';

const app = express();

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

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

app.listen(3000);

Now:

  • visiting / returns "Welcome to Express"

  • visiting /about returns "About Page"

Express automatically checks the route and runs the correct handler.

Request → Route Handler → Response Flow


  Browser Request
         ↓
  Express Route Matches URL
         ↓
  Route Handler Executes
         ↓
  Response Sent Back

This request-response cycle is the foundation of almost every backend application.


Handling POST Requests

POST requests are used when the client wants to send data to the server.

Examples include:

  • login forms

  • registration forms

  • creating new records

  • submitting data

Here’s a basic POST route:


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

When a POST request hits /login, Express executes the route handler and sends a response back.

Even though this example is simple, the same concept is used in real-world authentication systems and APIs.


Sending Responses in Express

Express provides several ways to send responses.

The most common one is:

res.send()

It can send:

  • text

  • HTML

  • JSON

  • objects

Example:

res.send("Hello World");

You can also send JSON responses:

res.json({
  success: true,
  message: "Data fetched successfully"
});

This is especially useful when building APIs.

Express Routing Structure Visualization

  
  Incoming Request
         ↓
  Express Server
         ↓
  Route Matching
         ↓
  Correct Route Handler
         ↓
  Response Returned

As applications grow larger, Express allows routes to be organized into separate files and modules, making backend code much easier to maintain.


Why Developers Prefer Express

Express became the standard framework for Node.js because it removes a lot of complexity from backend development.

Instead of manually handling low-level server logic, developers can focus more on building application features.

It offers:

  • clean routing

  • middleware support

  • easier request handling

  • faster API development

  • scalable project structure

At the same time, Express stays lightweight and flexible, which is why it remains one of the most widely used Node.js frameworks today.


Conclusion

Express.js simplified backend development for Node.js developers in a huge way.

Instead of writing repetitive server code manually, developers can define routes and handle requests using clean, readable syntax.

The most important thing to understand is this:

A route listens for a request and sends back a response.

That simple request-response cycle powers almost every web application and API on the internet.

And Express makes working with that cycle incredibly easy.