Skip to main content

Command Palette

Search for a command to run...

Creating Routes and Handling Requests with Express

Learn how Express simplifies Node.js backend development through cleaner routing and request handling.

Updated
5 min read
Creating Routes and Handling Requests with Express
P
IT graduate 2024. Learning software and web development in public. Writing about bugs, fixes, small projects, useful tools, and lessons from building things step by step.

Introduction

Once you start building with Node.js, one thing becomes obvious very quickly:

creating servers with raw Node HTTP is educational, but it can become repetitive.

You have to think about:

  • routing manually

  • method checks

  • request handling structure

  • response writing details

That is where Express.js becomes useful.

Express is one of the most popular Node.js frameworks because it makes backend code easier to read, easier to organize, and easier to scale step by step.

In this article, we will understand:

  • what Express.js is

  • why developers use it

  • how to create a first Express server

  • how to handle GET and POST requests

  • how responses are sent


What Is Express.js?

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

Simple meaning:

it gives you cleaner tools for handling:

  • routes

  • requests

  • responses

  • middleware

Without Express, you can still build a server in Node.js.

But Express removes a lot of low-level repetition and gives your backend code a more readable structure.

That is one of the biggest reasons beginners and professionals both use it so often.


Why Express Feels Easier Than Raw Node HTTP

With raw Node HTTP, code often looks more manual.

You may need to:

  • inspect the request URL yourself

  • check the request method yourself

  • write conditional logic for routes

  • manage response headers directly

In Express, route handling feels more declarative.

You can write things like:

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

That is much easier to read than a large block of manual route-checking logic.

This is exactly why Express became so popular in Node.js development.


Creating Your First Express Server

A basic Express server can look like this:

const express = require("express");

const app = express();

app.listen(3000, () => {
  console.log("Server is running on http://localhost:3000");
});

At this stage:

  • express() creates the app

  • app.listen() starts the server

This already feels simpler than writing the lower-level server setup manually.


What Is a Route?

A route is simply a path and method combination that your server knows how to handle.

For example:

  • GET / might show a home response

  • GET /about might show an about response

  • POST /login might handle login data

So a route answers this question:

when this kind of request arrives, what should the server do?

That is why routing is one of the core ideas in Express.


Handling a GET Request

A GET request is usually used when the client wants to fetch or read something.

Example:

const express = require("express");

const app = express();

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

app.listen(3000);

Here:

  • app.get() defines a GET route

  • "/" is the route path

  • (req, res) gives access to the request and response objects

  • res.send() sends the response

This is one of the first patterns every Express learner should get comfortable with.


Handling a POST Request

A POST request is commonly used when the client wants to send data to the server.

For example:

  • login form submission

  • user registration

  • creating a new resource

Basic example:

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

This does not yet process body data, but it shows the route idea clearly.

The important beginner understanding is:

  • GET usually reads

  • POST usually sends or creates


Sending Responses in Express

Express makes response sending simple.

Some common response methods are:

  • res.send()

  • res.json()

  • res.status()

For example:

app.get("/user", (req, res) => {
  res.status(200).json({ name: "Prash", role: "Student" });
});

This is one reason Express feels clean.

The response logic reads more naturally and the code is easier to follow.


Raw Node vs Express - The Real Advantage

The biggest Express advantage is not that it performs some mysterious magic.

Its real value is:

  • cleaner routing

  • better readability

  • simpler request-response handling

  • easier growth as the app gets bigger

That is why developers often learn raw Node HTTP first for understanding, but use Express for practical backend work.


A Good Beginner Mental Model

Think of Express like this:

Node.js gives you the runtime.

Express gives you a cleaner way to organize web server logic inside that runtime.

That means Express is not replacing Node.js.

It is helping you build on top of Node.js more comfortably.


Summary

  • Express.js is a minimal Node.js web framework

  • it simplifies route handling and request-response logic

  • GET routes are usually used for reading data

  • POST routes are usually used for sending or creating data

  • Express makes backend code cleaner than large manual route checks


Final Thought

Express became popular for a simple reason:

it turns backend code into something easier to read and easier to scale.

Once basic routing feels clear, many later backend topics start becoming much easier too.


Continue Reading