Stop 03 · Express.js BasicsTopics · 2/8

Routing

Part of the MERN Stack Roadmap

In short

Your API might have fifty endpoints. Routing is how Express sends each request to the exact piece of code meant for it, and nowhere else.

A route connects an HTTP method and a path to a handler function, such as GET /api/products. Express checks routes in order and runs the first one that matches.

The mascot drops letters into a wall of wooden pigeonhole boxes.

Think of it like this

The post office sorting desk

At the sorting desk, every letter is checked for its PIN code and dropped into the right pigeonhole. Wrong slot, wrong city. Express routing checks each request's method and path and drops it into the right handler.

Watch it happen

How Express matches GET /products/7

app
GET
POST
/products
/products/:id
/products
404 fallback

1/5Request GET /products/7 arrives. Express checks routes in the order you added them.

The big ideas

What you need to know

One route per job

app.get, app.post, app.put, app.patch and app.delete each match one HTTP method.

Order matters

Express checks routes top to bottom. Put /products/featured before /products/:id, or :id will grab it.

app.route for one path

app.route("/products").get(list).post(create) keeps all methods for a path together.

End with a 404

A final app.use((req, res) => res.status(404)...) catches anything no route matched.

See it in code

Walk through the code

js
1app.get("/products", (req, res) => res.json(products));2 3app.get("/products/featured", (req, res) => {4  res.json(products.filter((p) => p.featured));5});6 7app.get("/products/:id", (req, res) => {8  res.json(products.find((p) => p.id === req.params.id));9});10 11app.post("/products", (req, res) => res.status(201).json(req.body));12 13app.use((req, res) => {14  res.status(404).json({ error: `No route for ${req.method} ${req.path}` });15});

1/4List all products.

Swap the order of /featured and /:id and GET /products/featured would try to find a product whose id is "featured".

Save yourself the pain

Common mistakes

Oops

Using GET /deleteProduct?id=7 to delete things.

Do this instead

Use the HTTP method for the action: DELETE /products/7. GET requests should never change data.

Oops

Letting unmatched routes return Express's default HTML 404 page in a JSON API.

Do this instead

Add your own final 404 handler that returns JSON, so clients always get the same format.

Your mission

Route a mini catalogue

~20 min

Quick check

Routes are added in order: GET /users/:id, then GET /users/me. What handles GET /users/me?

Go deeper

  1. 1Create GET, POST, PATCH and DELETE routes for a simple products resource.
  2. 2Prefix every API route with /api so it never clashes with frontend paths.
  3. 3Test each route in Postman or with curl as you write it.

Also part of these roadmaps

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