Express: an API server

Lesson 01 · 11 min

Routes, req, and res

A route is a method + a path + a function. req is what came in, res is what goes out.

Express is a minimal framework over Node's HTTP server. You register a route: a method, a path, and a handler (req, res) => …. req.params holds path parts like :id, req.query the query, req.body the parsed body.

A first API
AI

You can ask about this rule«One response per request»

  • req.params.id is always a string. To match a number, convert it: Number(id).
  • res.status(201).json(data) chains the status and body.
  • Without express.json(), req.body is undefined.
AI

You can ask about this point«Lesson summary»

Check

Lock it in before you move on

  1. 1. Route /users/:id, request /users/5. What's in req.params.id?

  2. 2. What happens if you call res.json twice?

  3. 3. req.body is empty though the client sent JSON. What's missing?

PracticeSolve the lesson tasksA few short tasks in the editor. Check your code right away and see where it goes wrong.Start practice