0
0
Expressframework~10 mins

Mounting routers with app.use in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mounting routers with app.use
Create Express app
Create Router
Define routes on Router
Mount Router on app with app.use(path, router)
Incoming request to app
Check if request URL matches mount path
Yes
Pass request to Router
Router handles route
Send response
Request complete
This flow shows how an Express app creates a router, mounts it with app.use, and how requests matching the mount path are handled by the router.
Execution Sample
Express
const express = require('express');
const app = express();
const router = express.Router();
router.get('/hello', (req, res) => res.send('Hello from router'));
app.use('/api', router);
This code creates an Express app, a router with a /hello route, and mounts the router at /api.
Execution Table
StepActionRequest URLMatch Mount Path?Router Route Matched?Response Sent
1Request arrives at app/api/helloYes (/api)Yes (/hello)Hello from router
2Request arrives at app/api/goodbyeYes (/api)No404 Not Found
3Request arrives at app/helloNoN/A404 Not Found
💡 Requests not matching the mount path or router routes result in 404 responses.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
appExpress app instanceReceives /api/helloReceives /api/goodbyeReceives /hello
routerExpress router instanceHandles /hello routeNo matching routeNo matching route
request URLN/A/api/hello/api/goodbye/hello
responseN/ASent 'Hello from router'Sent 404Sent 404
Key Moments - 2 Insights
Why does a request to /hello not get handled by the router mounted at /api?
Because the router is mounted at /api, only requests starting with /api match the mount path (see execution_table row 3). Requests to /hello do not match and are not passed to the router.
What happens if the router does not have a route matching the request after matching the mount path?
If the router has no matching route (see execution_table row 2), Express returns a 404 Not Found response because no handler was found.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response is sent when the request URL is /api/hello?
A"Hello from router"
B404 Not Found
CNo response sent
D"Hello from app"
💡 Hint
Check execution_table row 1 under 'Response Sent'
At which step does the request URL not match the mount path?
AStep 1
BStep 2
CStep 3
DNone
💡 Hint
Look at 'Match Mount Path?' column in execution_table
If the router was mounted at '/' instead of '/api', what would happen to the request at step 3?
AIt would be handled by the router if a matching route exists
BIt would still return 404 immediately
CThe app would crash
DThe request would be ignored
💡 Hint
Consider how mounting at '/' matches all paths, see concept_flow
Concept Snapshot
Mount routers with app.use(path, router)
- Create router with express.Router()
- Define routes on router
- Mount router on app with app.use('/path', router)
- Requests starting with '/path' go to router
- Router handles routes relative to mount path
- Non-matching requests skip router
Full Transcript
This lesson shows how to mount routers in Express using app.use. First, you create an Express app and a router. Then you define routes on the router. Next, you mount the router on the app at a specific path using app.use. When a request comes in, Express checks if the URL starts with the mount path. If yes, it passes the request to the router. The router then tries to match the rest of the URL to its routes. If a route matches, it handles the request and sends a response. If no route matches, Express sends a 404 Not Found. Requests that do not start with the mount path do not reach the router and also get 404 if no other handler exists. This way, routers help organize routes under specific URL prefixes.