0
0
Expressframework~10 mins

Separating routes into files in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Separating routes into files
Create main app file
Create route file
Export router from route file
Import router in main app
Use router with app.use()
Start server and handle requests
This flow shows how to split routes into separate files, export them, and use them in the main Express app.
Execution Sample
Express
const express = require('express');
const app = express();
const userRoutes = require('./routes/users');
app.use('/users', userRoutes);
app.listen(3000);
Main app imports user routes from a separate file and uses them under '/users' path.
Execution Table
StepActionFileResult
1Create main app file with express appapp.jsExpress app instance created
2Create route file with express.Router()routes/users.jsRouter instance created with routes
3Export router from route fileroutes/users.jsRouter exported
4Import router in main appapp.jsRouter imported as userRoutes
5Use router with app.use('/users', userRoutes)app.jsRoutes under '/users' path registered
6Start server with app.listen(3000)app.jsServer listening on port 3000
7Request to '/users/profile'app.js + routes/users.jsRouter handles request and sends response
8Request to '/users/settings'app.js + routes/users.jsRouter handles request and sends response
9Request to '/other'app.jsNo matching route, 404 or default response
💡 Server runs indefinitely until stopped; requests handled by registered routers.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
appundefinedExpress app instanceExpress app instanceExpress app with userRoutes registeredRunning server
userRoutesundefinedRouter instanceRouter instanceRouter instanceRouter used for '/users' routes
Key Moments - 3 Insights
Why do we use express.Router() in a separate file instead of app directly?
Using express.Router() lets us group related routes in one file and export them. This keeps code organized. See execution_table step 2 and 3 where router is created and exported.
What does app.use('/users', userRoutes) do exactly?
It tells Express to use all routes defined in userRoutes under the '/users' path prefix. So '/users/profile' calls the profile route inside userRoutes. See execution_table step 5.
What happens if a request does not match any route?
Express returns a 404 or default response because no route handled the request. See execution_table step 9 where '/other' has no matching route.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result after step 3?
ARouter imported in main app
BRouter exported from route file
CServer started and listening
DRoutes registered under '/users'
💡 Hint
Check the 'Result' column for step 3 in the execution_table.
At which step does the main app start listening for requests?
AStep 5
BStep 4
CStep 6
DStep 2
💡 Hint
Look for 'Server listening on port 3000' in the execution_table.
If we change app.use('/users', userRoutes) to app.use('/api', userRoutes), what changes in the execution?
ARoutes will be under '/api' instead of '/users'
BRouter will not be imported
CServer will not start
DNo routes will work
💡 Hint
Refer to step 5 in execution_table where the path prefix is set.
Concept Snapshot
Separate routes into files using express.Router().
Export router from route file.
Import router in main app.
Use app.use('/path', router) to mount routes.
Keeps code organized and modular.
Server handles requests via mounted routers.
Full Transcript
This lesson shows how to organize Express routes by separating them into different files. First, you create a main app file that initializes the Express app. Then, you create a route file where you use express.Router() to define related routes. You export this router from the route file. In the main app file, you import the router and use app.use() to mount it under a specific path prefix like '/users'. This way, requests to '/users/profile' or '/users/settings' are handled by the routes defined in the separate file. The server listens on a port and handles requests accordingly. This approach keeps your code clean and easier to maintain.