Stop 05 · Backend with Node.js & ExpressTopics · 3/8

Express Basics & Routing

Part of the Full Stack Developer Roadmap

In short

Express is the most common way to build a Node backend. Its main job is simple: look at the method and URL of each request and send it to the right function.

A minimal Node.js web framework handling routing and HTTP plumbing — mapping HTTP methods and paths to handler functions, the core job of your backend.

The mascot at a hospital reception desk points towards corridors with coloured doors.

Think of it like this

The hospital reception desk

At a big hospital, reception asks what you need and points you down the right corridor: OPD, X-ray or pharmacy. They don't treat you, they route you. Express's router is that desk, and your handler functions are the departments.

Watch it happen

GET /api/users/7 through Express

React app
Express router
Route handler
Database

GET /api/users/7

1/6The frontend asks for one user.

The big ideas

What you need to know

Method + path = route

app.get, app.post, app.patch and app.delete register a handler for one method and one path pattern.

Params, query, body

req.params comes from :id in the path, req.query from ?page=2, req.body from JSON once express.json() is on.

Split with Router

Put user routes in routes/users.js with express.Router(), then mount them with app.use("/api/users", router).

Order matters

Express tries routes top to bottom. A broad route like /:slug placed first can swallow more specific ones below it.

See it in code

Walk through the code

js
1import express from "express";2const app = express();3app.use(express.json()); // parse JSON request bodies4 5app.get("/api/users", async (req, res) => {6  const page = Number(req.query.page ?? 1);7  res.json(await db.user.findMany({ skip: (page - 1) * 20, take: 20 }));8});9 10app.get("/api/users/:id", async (req, res) => {11  const user = await db.user.findUnique({ where: { id: Number(req.params.id) } });12  if (!user) return res.status(404).json({ error: "User not found" });13  res.json(user);14});15 16app.listen(4000, () => console.log("API on http://localhost:4000"));

1/4Create the app and turn on JSON body parsing for POST and PATCH requests.

Two routes, one for the list and one for a single user. This is the shape of almost every API you'll write.

Save yourself the pain

Common mistakes

Oops

Reading req.body and getting undefined.

Do this instead

Add app.use(express.json()) before your routes, and send Content-Type: application/json from the frontend.

Oops

Comparing req.params.id === 7 and never finding a match.

Do this instead

Params are strings. Convert with Number() and check for NaN before using it.

Your mission

Route a mini library

~30 min

Quick check

You request GET /api/users/42?fields=name. Where does Express put "42" and "name"?

Go deeper

  1. 1Build a 'Hello World' Express server with a couple of routes.
  2. 2Practice route parameters (`/users/:id`) for fetching a specific resource.
  3. 3Organize related routes into their own router files as the app grows.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge