APIs with Express · Chapter 8 of 29
Routers
By the end of this chapter you can split an API into routers, each with its own paths, its own middleware and its own parameters, and mount them wherever you like. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Middleware
- 5 tasks
- Express
- In the all labs plan
The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.
Why you need this
Tapri's app file has grown long. Menu routes sit between order routes, the ops check is copied onto every admin route, and Anjali wants the menu under a second prefix, /api/v2/menu, for the new app.
With one app and full paths, a second prefix means copying every menu route with a new path, and every new admin route needs the ops check pasted in again. Forget it once, and that route is open to anyone.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
menuRouter: the menu under any prefix
Export menuRouter(), which returns a new router (it is not mounted by itself). Its paths are relative, so it works wherever it is mounted, under /menu or /api/v2/menu:
GET / answers 200 with the whole menu. GET /:id answers 200 with that item, so /menu/cutting-chai gives { id: 'cutting-chai', name: 'Cutting chai', price: 25 }. An id that is not on the menu answers 404 with { error: 'No <id> on the menu' }, for example { error: 'No filter-coffee on the menu' }.
Nothing lives at the root: mounted under /menu, GET /cutting-chai is a 404.
- Task 2
ordersRouter: orders, loaded once with router.param
Export ordersRouter(store), which returns a router for orders, kept in the store it is given:
POST / creates an empty order with store.create() and answers 201 with it: { id: 1, items: [], total: 0 }, then id 2. GET /:orderId answers 200 with that order. Mount itemsRouter(store) at /:orderId/items.
Load the order with router.param for orderId: an unknown order answers 404 with { error: 'Order <id> not found' }, using the id as it was written ({ error: 'Order 42 not found' }, { error: 'Order chai not found' }). The same 404 guards the items path: GET /orders/42/items answers { error: 'Order 42 not found' }. Reading orders never creates one.
- Task 3 · interview question
Interview: itemsRouter cannot see :orderId
Fix itemsRouter(store). It is mounted under a path with :orderId in it (app.use('/orders/:orderId/items', itemsRouter(store))), but today every order answers 404 with { error: 'Order undefined not found' }.
Afterwards: GET /orders/1/items answers 200 with that order's items ([] for a new order). POST /orders/1/items with JSON { item: 'masala-chai', qty: 2 } answers 201 with the whole order { id: 1, items: [{ item: 'masala-chai', qty: 2 }], total: 80 }. Order 2 keeps its own items. A line that is not on the menu answers 400 with { error: 'Pick a menu item and a quantity' }, and an unknown order, such as 9, answers 404 with { error: 'Order 9 not found' }.
- Task 4
adminRouter: one ops check at the door
Export adminRouter(store), which returns a router where every route needs the x-role header to be exactly ops. Without the header, or with any other role (rider, say), it answers 403 with { error: 'Operations only' }.
GET /orders answers 200 with every order in the store, as store.all() gives them: [{ id: 1, items: [{ item: 'bun-maska', qty: 2 }], total: 70 }].
- Task 5
createApp: three routers on one store
In createApp, mount menuRouter() under /menu, ordersRouter(store) under /orders and adminRouter(store) under /admin, all with the same store, so an order made through /orders shows up in GET /admin/orders.
The ops check stays inside /admin: GET /menu and GET /orders/1 answer 200 with no headers, and GET /admin/orders without x-role answers 403. Each createApp() has its own store, so a new app has no order 1 (404).
What you will learn
- express.Router(): a small app of its own, mounted under a prefix with app.use.
- Middleware at the router's door: router.use runs for that router's requests only.
- router.param: load the thing a parameter names, once, for every route that has it.
- Nested routers: a router mounted inside another, and the parameter it cannot see.
More in Middleware
Build "Routers" yourself
Partner cafés want Tapri’s menu online by Friday: you build its API. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.