📖 Scenario: You are building a simple Express web server. To keep your code clean and organized, you want to separate your routes into different files instead of putting all routes in one file.
🎯 Goal: Create an Express app that uses a separate route file for handling requests to /users. The main app file should import and use this route file.
📋 What You'll Learn
Create a route file named users.js that exports an Express router.
In users.js, define a GET route for / that sends the text 'User list'.
In the main app file app.js, import the users.js router.
Use the imported router in app.js for the path /users.
💡 Why This Matters
🌍 Real World
Separating routes into files helps keep your server code organized and easier to maintain as your app grows.
💼 Career
Many professional Express apps use routers in separate files to manage complexity and collaborate with teams.
Progress0 / 4 steps
1
Create the users route file
Create a file named users.js. Inside it, import express and create a router using express.Router(). Then define a GET route on '/' that sends the text 'User list'. Finally, export the router using module.exports = router.
Express
Hint
Use express.Router() to create a router. Define routes on it like on an app. Export the router at the end.
2
Set up the main app file
In app.js, import express and create an app using express(). Also import the users.js router using require('./users'). Set the app to listen on port 3000.
Express
Hint
Import express and create the app. Import the users router from './users'. Use app.listen(3000) to start the server.
3
Use the users router in the app
In app.js, use app.use('/users', usersRouter) to connect the imported usersRouter to the path /users.
Express
Hint
Use app.use to mount the router on the /users path.
4
Add a root route to the app
In app.js, add a GET route on '/' that sends the text 'Welcome to the main page'.
Express
Hint
Use app.get to define a route for the root path /.
Practice
(1/5)
1. What is the main benefit of separating routes into different files in an Express app?
easy
A. It makes the app run faster.
B. It keeps the app organized and easier to maintain.
C. It automatically secures the routes.
D. It removes the need for middleware.
Solution
Step 1: Understand code organization
Separating routes into files helps keep code clean and manageable as the app grows.
Step 2: Identify benefits
This separation does not affect speed or security directly but improves maintainability.
Final Answer:
It keeps the app organized and easier to maintain. -> Option B
Quick Check:
Organized code = easier maintenance [OK]
Hint: Think about code clarity and maintenance benefits [OK]
Common Mistakes:
Confusing organization with performance improvements
What error will occur when starting the app or accessing /products/list?
medium
A. SyntaxError due to missing export
B. TypeError: Cannot read property 'use' of undefined
C. No error, but route won't respond
D. productsRouter is undefined, causing an error
Solution
Step 1: Check module export in products.js
Since module.exports is missing, require('./products') returns undefined.
Step 2: Understand impact in app.js
Using undefined as router in app.use causes an error when app tries to use it.
Final Answer:
productsRouter is undefined, causing an error -> Option D
Quick Check:
Missing export = undefined import error [OK]
Hint: Always export router to avoid undefined imports [OK]
Common Mistakes:
Assuming app runs without export
Confusing syntax error with runtime error
Not checking module.exports presence
5. You want to separate routes for /api/users and /api/products into two files: users.js and products.js. Each file exports a router with routes starting at /. How should you mount these routers in app.js to correctly handle requests to /api/users/profile and /api/products/list?
hard
A. app.use('/api', usersRouter); app.use('/api', productsRouter);
B. app.use('/users', usersRouter); app.use('/products', productsRouter);
C. app.use('/api/users', usersRouter); app.use('/api/products', productsRouter);
D. app.use('/', usersRouter); app.use('/', productsRouter);
Solution
Step 1: Understand router base paths
Each router handles routes starting at /, so mounting them at specific paths adds prefixes.
Step 2: Mount routers with full API paths
Mount usersRouter at /api/users and productsRouter at /api/products to match desired URLs.
Final Answer:
app.use('/api/users', usersRouter); app.use('/api/products', productsRouter); -> Option C
Quick Check:
Mount routers at full paths for correct URL handling [OK]
Hint: Mount routers at full URL prefixes to match routes [OK]
Common Mistakes:
Mounting routers at wrong or incomplete paths
Mounting both routers at same path causing conflicts
Assuming routers handle full paths internally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.