Skip to main content

Command Palette

Search for a command to run...

Creating Routes and Handling Requests with Express

Updated
6 min readView as Markdown
N

I build clean and simple web experiences and learn something new every day.

The first time I created a server using pure Node.js… it felt cool.
But also… a little painful.

I had code like this:

const http = require("http");

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

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

server.listen(3000);

And honestly… even for two routes, it already started looking messy.

Then I imagined:

What if my app had 50 routes?
APIs?
Authentication?
Middleware?

That’s when I understood why developers use:

Express.js

Because Express doesn’t replace Node.js.

It makes Node.js easier.

And honestly…
that’s the reason it became one of the most popular backend frameworks ever.


What is Express.js?

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

Think of it like this:

Node.js = engine

Express = easier control system

Node.js gives you raw backend power. Express gives you cleaner tools to use that power.


Why Express Exists

Technically… you CAN create servers using only Node.js. But raw Node.js becomes repetitive very quickly.

You manually handle:

  • routes

  • headers

  • request parsing

  • responses

  • status codes

Again and again. Express simplifies all of this.


Raw Node.js vs Express

This comparison made everything click for me.


Raw Node.js

if (req.url === "/about") {
  res.end("About");
}

Express

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

Cleaner.
Readable.
Much easier to scale.

And this is why developers loved Express.


Installing Express

First, create a project folder.

Then initialize Node project:

npm init -y

Now install Express:

npm install express

This downloads Express into your project.


Creating the First Express Server

Create:

app.js

Inside it:

const express = require("express");

const app = express();

app.listen(3000);

Run:

node app.js

And boom. Your Express server is running.


But What Is express()?

This confused me initially.

const app = express();

This creates an Express application.

You can think of app as: your backend application controller.
Everything happens through it.


What Are Routes?

Routes are basically: rules for handling requests.

Example:

If user visits "/"
      ↓
run this function

That function is called:

Route Handler


Handling Your First GET Request

Now let’s create a route.

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

What’s Happening Here?

This line:

app.get()

means: handle GET requests.

And:

"/"

means homepage route.


Understanding req and res

This is SUPER important.

(req, res)

These are objects provided by Express.


req

Represents: incoming request

Contains:

  • URL

  • headers

  • body

  • query params

Basically: information sent by client.


res

Represents: response you send back.

You use it to:

  • send data

  • send JSON

  • send status codes

  • end request


Real Mental Model

Browser Request
       ↓
Express Route
       ↓
Route Handler Runs
       ↓
Response Sent Back

That’s backend routing.


Testing the Route

Run server:

node app.js

Visit:

http://localhost:3000

Output:

Hello Express

And honestly… this simplicity is why Express became so popular.


Multiple Routes

Now things become interesting.

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

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

Now:

/           → Home Page
/about      → About Page

Different URLs. Different responses.


This Is Routing

Express checks: which route was requested
Then runs matching handler.

Simple.
But powerful.


Why Routing Matters

Imagine websites without routes. Everything would exist on one page. No structure. Routes organize backend behavior.

They help apps decide: what happens for each request.


Handling POST Requests

GET requests are mostly used for: fetching data

POST requests are usually used for: sending data

Example:

  • login forms

  • signup forms

  • creating posts


Basic POST Route

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

Now Express handles POST requests for /login.


Difference Between GET and POST

This is important.


GET

Used for: requesting data

Example:

Get products
Get users
Get homepage

POST

Used for: sending data to server

Example:

Login form
Register user
Create blog post

Sending Responses

Express makes responses very easy.


Sending Text

res.send("Hello");

Sending JSON

res.json({
  name: "Nausheen",
});

Output:

{
  "name": "Nausheen"
}

Why Express Feels Beginner-Friendly

This is what stood out to me initially. Raw Node.js feels low-level. Express feels readable.
You spend less time managing technical complexity… and more time building features.


Express Routing Visualization

Incoming Request
        ↓
Express Checks Route
        ↓
Matching Route Handler
        ↓
Response Returned

Another Small But Important Insight

Express is still Node.js underneath. This is important.

People often think: “Express replaced Node.js.”

No.
Express RUNS on Node.js. Without Node.js… Express cannot exist.


Common Beginner Confusion

People often think:

app.get()

creates pages automatically. Not exactly.

It simply defines: what happens when a route is visited.

The browser still sends requests. Express simply responds.


Why Developers Loved Express

Express solved a huge problem: backend development complexity.

Instead of manually handling everything…

developers got:

  • cleaner routing

  • easier request handling

  • simpler responses

  • better structure

And honestly… that simplicity helped Node.js explode in popularity.


Assignment Practice

Try these yourself:

1. Create Express server
2. Add / route
3. Add /about route
4. Create POST route
5. Send JSON response
6. Visit routes in browser

Experiment with different paths. That’s where routing truly starts making sense.


Final Understanding

At first, backend development feels complicated.

Requests.
Responses.
Servers.
Routing.

Everything sounds overwhelming.

But Express simplifies the entire process into something very understandable:

A request comes in
a route matches
a function runs
a response goes back

And honestly… once that flow clicks… backend development starts feeling way less scary.

More from this blog

C

codeXninjaDev

54 posts

I build clean and simple web experiences and learn something new every day.