📖 Scenario: You are building a simple web server using Express.js. As your app grows, you want to keep your routes organized and easy to manage.
🎯 Goal: Create a modular routing setup by separating routes into their own file and then use them in the main Express app.
📋 What You'll Learn
Create a route module file with a router
Add a configuration variable for the base path
Use the router in the main app with the base path
Export and import the router correctly
💡 Why This Matters
🌍 Real World
Large Express apps often have many routes. Modular routing keeps code clean and manageable.
💼 Career
Understanding modular routing is essential for backend developers working with Express to build scalable web servers.
Progress0 / 4 steps
1
Create a router module with a simple route
Create a file called users.js that exports an Express router. Inside it, create a GET route for / that sends the text 'User list'.
Express
Hint
Use express.Router() to create a router. Then add a GET route on '/' that sends 'User list'. Export the router as default.
2
Add a base path configuration variable
In your main app file app.js, create a variable called usersBasePath and set it to '/users'.
Express
Hint
Just create a constant variable named usersBasePath and assign it the string '/users'.
3
Use the router module in the main app with the base path
In app.js, import the users router from './users.js'. Then use app.use(usersBasePath, users) to mount the router on the base path.
Express
Hint
Import the router from './users.js' and use app.use() with usersBasePath and users to mount it.
4
Complete the Express app with listen
Add app.listen(3000) to start the server on port 3000.
Express
Hint
Use app.listen(3000) to start the server on port 3000.
Practice
(1/5)
1. Why is modular routing important in Express applications?
easy
A. It allows routes to run without defining HTTP methods.
B. It automatically improves server speed without code changes.
C. It helps organize routes into separate files for easier maintenance.
D. It removes the need to use express.Router().
Solution
Step 1: Understand modular routing purpose
Modular routing splits routes into separate files to keep code organized and easier to maintain.
Step 2: Evaluate other options
The other options describe incorrect or unrelated effects: allowing routes without HTTP methods, automatic speed improvement, and removing the need for express.Router().
Final Answer:
It helps organize routes into separate files for easier maintenance. -> Option C
Quick Check:
Modular routing = better organization [OK]
Hint: Modular routing means splitting routes into files [OK]
Common Mistakes:
Thinking modular routing speeds up server automatically
Believing modular routing removes need for express.Router()
Assuming routes work without HTTP methods
2. Which of the following is the correct way to create a modular router in Express?
easy
A. const router = express.createRouter();
B. const router = express.Router();
C. const router = new Router();
D. const router = express.route();
Solution
Step 1: Recall Express router creation syntax
The correct method to create a router is express.Router().
Step 2: Check other options
The other options use methods that do not exist or are incorrect: express.createRouter(), new Router(), and express.route().
Final Answer:
const router = express.Router(); -> Option B
Quick Check:
Router creation = express.Router() [OK]
Hint: Use express.Router() to create a router [OK]
Common Mistakes:
Using express.createRouter() which does not exist
Trying to instantiate Router without express
Calling express.route() instead of express.Router()
3. Given this Express app code, what will be the output when accessing /users?