1. Concept library
  2. Backend with Node.js
  3. Module 3: Building REST APIs

Concept

Routing

A router maps a method and path pattern to a handler, with parameters pulled from the path. Group routes by resource and version them from the start so a redesign does not break existing clients.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain how a router matches a method and path and extracts parameters
  2. Define routes for a resource with GET, POST, PATCH and DELETE
  3. Spot the state change encoded as a GET

In practice

A few lines that show Routing doing its job.

JavaScript
import Fastify from "fastify";
const app = Fastify();

app.register(async (orders) => {
  orders.get("/", listOrders);
  orders.get("/:id", getOrder);         // req.params.id
  orders.post("/", createOrder);
  orders.delete("/:id", cancelOrder);
}, { prefix: "/api/v1/orders" });

await app.listen({ port: 3000 });

The common mistake

Watch for

Encoding actions in paths, like /orders/123/cancel with GET. Reads should be safe to repeat; a state change belongs on POST, PATCH or DELETE so proxies and browsers treat it correctly.

What to know first

Your roadmap puts these before Routing, unless you already know them.

How it stays learned

Once a session shows you understand Routing, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Routing.

Next in the moduleMiddleware →