0
0
Expressframework~10 mins

Creating an Express Router - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating an Express Router
Import express
Create router with express.Router()
Define routes on router
Export router
Import router in main app
Use router with app.use()
Server listens for requests
This flow shows how to create a router, add routes, export it, then use it in the main app to handle requests.
Execution Sample
Express
const express = require('express');
const router = express.Router();

router.get('/hello', (req, res) => {
  res.send('Hello World');
});

module.exports = router;
This code creates a router, adds a GET route for '/hello', and exports the router.
Execution Table
StepActionEvaluationResult
1Import expressexpress module loadedexpress object available
2Create router with express.Router()router = express.Router()router object created
3Define GET route '/hello'router.get('/hello', handler)Route added to router
4Export routermodule.exports = routerrouter ready to be imported
5Import router in main appconst router = require('./router')router object imported
6Use router with app.use('/api', router)app.use('/api', router)Router mounted at '/api'
7Server listensapp.listen(port)Server ready to accept requests
8Request to '/api/hello'Matches router GET '/hello'Response: 'Hello World' sent
9Request to '/hello'No match404 Not Found
10Request to '/api/goodbye'No match404 Not Found
💡 Execution stops after server handles requests or no matching route found.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
expressundefinedexpress objectexpress objectexpress objectexpress objectexpress objectexpress object
routerundefinedrouter objectrouter with '/hello' routeexported routerimported routerrouter mounted on '/api'router handles '/hello' requests
appundefinedundefinedundefinedundefinedapp objectapp with router mountedapp listening for requests
Key Moments - 3 Insights
Why do we create a router instead of adding routes directly to the app?
Creating a router lets us group routes and keep code organized. The execution_table rows 2 and 3 show router creation and route addition separate from the main app.
What happens if we request '/hello' instead of '/api/hello' after mounting the router at '/api'?
The request to '/hello' does not match any route because the router is mounted at '/api'. See execution_table row 9 where '/hello' returns 404.
How does the router know to respond with 'Hello World'?
The router has a GET route for '/hello' defined in step 3. When a request matches, the handler sends 'Hello World' as in step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'router' after step 3?
AUndefined
BRouter object without any routes
CRouter object with '/hello' route added
DRouter object exported
💡 Hint
Check the 'Result' column at step 3 in execution_table.
At which step does the server start listening for requests?
AStep 6
BStep 7
CStep 5
DStep 4
💡 Hint
Look for 'Server listens' action in execution_table.
If we change app.use('/api', router) to app.use('/', router), what happens to the request path for '/hello'?
AIt will match and respond with 'Hello World'
BIt will still return 404
CIt will cause an error
DIt will redirect to '/api/hello'
💡 Hint
Refer to execution_table rows 6 and 9 about router mounting and request matching.
Concept Snapshot
Create an Express router with express.Router().
Add routes to the router (e.g., router.get('/path', handler)).
Export the router module.
Import and mount the router in the main app with app.use('/prefix', router).
Requests to '/prefix/path' are handled by the router.
This keeps route code organized and modular.
Full Transcript
This lesson shows how to create an Express router to organize routes. First, we import express and create a router object. Then we add routes to this router, like a GET route for '/hello' that sends 'Hello World'. We export this router so it can be used elsewhere. In the main app, we import the router and mount it at a path prefix like '/api' using app.use. When the server listens and receives requests, requests to '/api/hello' match the router's route and respond with 'Hello World'. Requests outside the prefix or to undefined routes return 404. This modular approach helps keep code clean and manageable.