0
0
Expressframework~10 mins

Virtual path prefixes in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Virtual path prefixes
Define app
Create router
Attach router with prefix
Request to prefixed path
Router handles request
Send response
This flow shows how Express uses a virtual path prefix to route requests through a router mounted at a specific path.
Execution Sample
Express
const express = require('express');
const app = express();
const router = express.Router();
router.get('/', (req, res) => res.send('Hello from router'));
app.use('/prefix', router);
This code creates a router and mounts it on the app at the '/prefix' path, so requests to '/prefix' go to the router.
Execution Table
StepRequest URLRouter Mounted AtMatch?Handler CalledResponse Sent
1/prefix/prefixYesrouter.get('/')Hello from router
2/prefix//prefixYesrouter.get('/')Hello from router
3/prefix/extra/prefixYesNo handlerNo response from router
4/other/prefixNoNo handlerNo response from router
💡 Requests not starting with '/prefix' do not match the router and are not handled by it.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Request URL/prefix/prefix/prefix//prefix/extra/other
Router Mounted At/prefix/prefix/prefix/prefix/prefix
Match?N/AYesYesYesNo
Response SentN/AHello from routerHello from routerNo responseNo response
Key Moments - 2 Insights
Why does the router handle '/prefix' and '/prefix/' but not '/prefix/extra'?
Because the router is mounted at '/prefix' and only matches requests starting with that path. However, the router has a route defined only for '/', so '/prefix/extra' matches the router but does not match any route inside it, so no handler is called (see execution_table rows 1, 2, and 3).
What happens if a request URL does not start with the prefix?
The router does not match the request, so no handler inside the router runs and no response is sent from it (see execution_table row 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response is sent when the request URL is '/prefix/'?
A"Hello from router"
B"No response from router"
C"404 Not Found"
D"Error"
💡 Hint
Check execution_table row 2 under 'Response Sent'
At which request URL does the router NOT handle the request?
A/prefix
B/prefix/extra
C/other
D/prefix/
💡 Hint
Look at execution_table rows where 'Match?' is 'No'
If the router was mounted at '/api', which request URL would it handle?
A/prefix
B/api
C/other
D/prefix/extra
💡 Hint
Router matches requests starting with its mount path, see variable_tracker 'Router Mounted At'
Concept Snapshot
Express virtual path prefixes let you mount routers at specific paths.
Use app.use('/prefix', router) to route requests starting with '/prefix' to that router.
Router paths are relative to the prefix.
Requests not matching the prefix skip the router.
This helps organize routes under common URL segments.
Full Transcript
In Express, you can create a router and attach it to your main app at a specific path prefix. When a request comes in, Express checks if the URL starts with that prefix. If it does, the router handles the request using its defined routes. For example, mounting a router at '/prefix' means requests to '/prefix' or '/prefix/' trigger the router's handlers. Requests to '/prefix/extra' or other paths do not match the router's root route and so are not handled by it. This lets you group routes logically under a common path segment, making your app easier to manage.