0
0
Expressframework~10 mins

Resource-based route organization in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Resource-based route organization
Define resource name
Create router object
Add routes for CRUD operations
GET /resource
Export router
Use router in main app
This flow shows how to organize routes by resource, defining all CRUD routes in one router and then using it in the main app.
Execution Sample
Express
const express = require('express');
const router = express.Router();

router.get('/', (req, res) => res.send('List books'));
router.post('/', (req, res) => res.send('Create book'));

module.exports = router;
Defines GET and POST routes for the 'books' resource in a router, then exports it.
Execution Table
StepActionRoute RegisteredHandler BehaviorEffect
1Create router objectNo route yetNo handler yetRouter ready to add routes
2Add GET / routeGET /Responds with 'List books'GET / route available
3Add POST / routePOST /Responds with 'Create book'POST / route available
4Export routerRoutes readyHandlers readyRouter can be imported
5Use router in appRoutes active in appHandlers respond to requestsApp serves resource routes
6Request GET /booksMatches GET /Sends 'List books'Client receives list
7Request POST /booksMatches POST /Sends 'Create book'Client receives confirmation
8Request GET /books/123No route matches404 Not FoundClient gets error
💡 Execution stops after all routes are registered and requests handled or not matched.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
routerundefinedRouter with GET /Router with GET and POST /Router exportedRouter used in app
Key Moments - 2 Insights
Why does GET /books/123 return 404 even though we have routes for /books?
Because the router only defines routes for / (which is mounted at /books) and does not define a route with an id parameter like /:id. The route for /books/:id is missing, so the request does not match any route (see execution_table step 8).
Why do we create a router instead of adding routes directly to the app?
Creating a router groups all resource routes together, making code organized and reusable. Then we attach the router to the app once (see concept_flow steps 2-5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the router state after step 3?
ARouter has GET / route only
BRouter is exported
CRouter has GET and POST / routes
DRouter is used in the app
💡 Hint
Check the 'Route Registered' column at step 3 in execution_table
At which step does the app start responding to requests?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for when the router is used in the app in execution_table
If we add router.get('/:id', ...), what changes in the execution_table?
AStep 8 request GET /books/123 will match and respond
BStep 6 request GET /books will fail
CPOST /books route will be removed
DRouter will no longer export
💡 Hint
Consider how adding a route affects request matching in execution_table step 8
Concept Snapshot
Resource-based route organization in Express:
- Create a router with express.Router()
- Define CRUD routes for a resource (GET, POST, PUT, DELETE) on the router's root path
- Export the router
- Use router in main app with app.use('/resource', router)
- Keeps routes organized and reusable
Full Transcript
This visual execution trace shows how to organize routes in Express by resource. First, a router object is created. Then routes for the resource are added, such as GET / and POST / on the router. The router is exported and used in the main app mounted at /books. Requests to these routes match the handlers and respond accordingly. Requests to undefined routes like GET /books/123 return 404. This method groups related routes, making code cleaner and easier to maintain.