0
0
Expressframework~10 mins

Namespaces for separation in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Namespaces for separation
Start Express App
Define Namespace 1
Add Routes to Namespace 1
Define Namespace 2
Add Routes to Namespace 2
Mount Namespaces on App
App Listens for Requests
Request Matches Namespace Route
Namespace Route Handler Executes
Response Sent
End
This flow shows how an Express app defines separate namespaces (route groups) to organize routes, mounts them, and handles requests within each namespace.
Execution Sample
Express
const express = require('express');
const app = express();

const apiV1 = express.Router();
apiV1.get('/users', (req, res) => res.send('Users v1'));

const apiV2 = express.Router();
apiV2.get('/users', (req, res) => res.send('Users v2'));

app.use('/api/v1', apiV1);
app.use('/api/v2', apiV2);

app.listen(3000);
This code creates two namespaces (/api/v1 and /api/v2) with separate /users routes, then starts the server.
Execution Table
StepActionNamespaceRouteRequest URLHandler Response
1Create Express app----
2Create Router apiV1apiV1---
3Add GET /users to apiV1apiV1/users--
4Create Router apiV2apiV2---
5Add GET /users to apiV2apiV2/users--
6Mount apiV1 at /api/v1apiV1---
7Mount apiV2 at /api/v2apiV2---
8Request GET /api/v1/usersapiV1/users/api/v1/usersUsers v1
9Request GET /api/v2/usersapiV2/users/api/v2/usersUsers v2
10Request GET /api/v3/users (no match)--/api/v3/users404 Not Found
💡 Requests outside defined namespaces return 404; app listens indefinitely.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 7Final
appExpress app instanceExpress app instanceExpress app instanceExpress app with apiV1 mountedExpress app with apiV1 and apiV2 mountedRunning server
apiV1undefinedRouter instanceRouter instanceRouter instanceRouter instanceRouter with /users route
apiV2undefinedundefinedRouter instanceRouter instanceRouter instanceRouter with /users route
Key Moments - 3 Insights
Why do requests to /api/v1/users and /api/v2/users return different responses?
Because each namespace (apiV1 and apiV2) has its own router with a separate /users route handler, as shown in execution_table rows 8 and 9.
What happens if a request is made to a URL not matching any namespace?
The app returns a 404 Not Found response since no route matches, as shown in execution_table row 10.
Why do we use express.Router() to create namespaces?
express.Router() creates isolated route groups (namespaces) so routes can be organized and mounted separately, as seen in steps 2, 4, 6, and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 8. What response does the GET request to /api/v1/users return?
A404 Not Found
BUsers v1
CUsers v2
DError
💡 Hint
Check the Handler Response column in row 8 for the /api/v1/users request.
At which step is the apiV2 router mounted on the app?
AStep 4
BStep 6
CStep 7
DStep 9
💡 Hint
Look at the Action column for mounting routers in the execution_table.
If we add a new route /products to apiV1, how would the variable_tracker for apiV1 change after Step 7?
AapiV1 router now has /users and /products routes
BNo change, still only /users route
CapiV1 router replaced by apiV2 router
DapiV1 router removed
💡 Hint
Variable tracker shows routes inside routers; adding a route updates the router's routes.
Concept Snapshot
Namespaces in Express use express.Router() to group routes.
Each namespace handles its own routes separately.
Mount namespaces on the main app with app.use(path, router).
Requests match namespace path + route path.
This keeps routes organized and separated.
Unmatched requests return 404.
Full Transcript
This visual execution shows how Express uses namespaces to separate routes. We start by creating an Express app. Then we create two routers, apiV1 and apiV2, each with their own /users route. We mount apiV1 at /api/v1 and apiV2 at /api/v2 on the main app. When a request comes in, Express matches the URL to the mounted namespace and then to the route inside that namespace. For example, GET /api/v1/users triggers the apiV1 router's /users handler, returning 'Users v1'. Similarly, GET /api/v2/users triggers apiV2's handler, returning 'Users v2'. Requests to unknown paths like /api/v3/users get a 404 Not Found response. This separation helps organize routes clearly and avoid conflicts. The variable tracker shows how routers and app state change as we add and mount namespaces. Key moments clarify why different namespaces respond differently and why unmatched routes return 404. The quiz tests understanding of these steps and state changes.