0
0
Expressframework~10 mins

Route prefixing in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route prefixing
Create Express app
Create Router
Define routes on Router
Use Router with prefix
App listens for requests
Request comes in
Check prefix match
Yes
Route handler runs
Send response
End
This flow shows how an Express app uses a router with a prefix to handle requests starting with that prefix.
Execution Sample
Express
const express = require('express');
const app = express();
const router = express.Router();
router.get('/hello', (req, res) => res.send('Hi'));
app.use('/api', router);
app.listen(3000);
This code creates a router with a '/hello' route, then prefixes it with '/api' in the main app.
Execution Table
StepActionRequest URLPrefix Match?Route MatchedResponse Sent
1App starts listening----
2Request received/api/helloYes/hello'Hi'
3Request received/helloNoNo route404 Not Found
4Request received/api/goodbyeYesNo route404 Not Found
5Request received/api/hello/worldYesNo route404 Not Found
💡 Requests stop processing when no matching route is found or response is sent.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
request.url-/api/hello/hello/api/goodbye/api/hello/world
prefix '/api'setmatchednot matchedmatchedmatched
route matchednone/hellononenonenone
responsenone'Hi'404404404
Key Moments - 2 Insights
Why does '/hello' not match when prefix '/api' is used?
Because the router only handles routes starting with '/api'. The request '/hello' lacks this prefix, so it does not match any route in the router (see execution_table row 3).
What happens if the request URL has the prefix but no matching route?
The router checks the prefix and matches it, but since no route matches the rest of the path, it returns 404 Not Found (see execution_table rows 4 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response is sent when the request URL is '/api/hello'?
A404 Not Found
B'Hi'
CNo response
DError
💡 Hint
Check row 2 in the execution_table under 'Response Sent'
At which step does the prefix '/api' not match the request URL?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Prefix Match?' column in execution_table
If we change the prefix to '/v1' instead of '/api', what happens to the response for request '/api/hello'?
AResponse is 404 Not Found
BResponse is an error
CResponse is 'Hi'
DResponse is empty
💡 Hint
Changing prefix means '/api/hello' no longer matches the router prefix, see variable_tracker for prefix matching
Concept Snapshot
Route Prefixing in Express:
- Create a router with routes (e.g., router.get('/hello'))
- Use app.use('/prefix', router) to add a prefix
- Requests must start with prefix to reach router routes
- Router matches routes after prefix
- Non-matching routes or prefixes result in 404
- Helps organize routes under common path
Full Transcript
Route prefixing in Express means you create a router with some routes, then tell the main app to use that router but only for URLs starting with a certain prefix. When a request comes in, Express checks if the URL starts with the prefix. If yes, it removes the prefix and tries to match the rest of the path to the router's routes. If a route matches, its handler runs and sends a response. If no route matches, Express sends a 404 Not Found. If the prefix does not match, the router is skipped. This helps organize routes under a common path segment, like '/api'.