L13 HTTP and routing
18 minSuggested first: C2 or equivalent
Outcome
Identify a request's method, path, query, and status, then select its handler under stated rules.
Learn
An HTTP request has a method and a target, and can carry headers and a body. A response has a status, and can carry headers and a body. A router connects a request to a handler.
In the toy router below, both the method and the path must match; query parameters don't choose the route.
Worked example
Which handler receives GET /books/42?format=short, and what is id?
GET /books/:id → readBook
POST /books → createBook
Other requests → 404 in this toy router
Predict first, then show the worked explanation
GET /books/42?format=short matches the first rule, so readBook runs with id bound to the
string "42". The format stays available as a query parameter. POST /books would match
createBook instead.
Practice and check
Common mistake
Assuming a framework's defaults are HTTP requirements. Check precedence, trailing slashes, and the fallback in the pinned implementation.
Practice on a real repository
Try one of the puzzles below.
- The path a route is matched againstFind · Easy · encode-starlette
- The strict-routing switchFind · Easy · honojs-hono
Related topics: Routing
Practice is optional. Solving a puzzle doesn't complete a lesson, and lessons never change a puzzle's points.
References
- RFC 9110: HTTP Semantics (IETF)
- Middleware (Hono documentation)