An Express Router helps organize routes by grouping related route handlers together. It makes the code cleaner and easier to manage, like having different folders for different topics.
Click to reveal answer
beginner
How do you create a new Express Router?
Use <code>const router = require('express').Router();</code> or <code>const router = express.Router();</code> after importing Express. This creates a mini app to define routes separately.
Click to reveal answer
beginner
How do you add a GET route to an Express Router?
Use router.get('/path', (req, res) => { ... }). This sets a handler for GET requests on the specified path inside the router.
Click to reveal answer
beginner
How do you use an Express Router in your main app?
Use app.use('/prefix', router); to tell the main app to use the router for routes starting with the prefix. This connects the router to the app.
Click to reveal answer
beginner
Why is using Express Router better than putting all routes in one file?
It keeps code organized and easier to read. Like sorting books by topic, routers help separate routes by feature or area, making maintenance simpler.
Click to reveal answer
How do you create a new Express Router?
Aconst router = require('router')();
Bconst router = new Router();
Cconst router = express.createRouter();
Dconst router = express.Router();
✗ Incorrect
The correct way is to call express.Router() after importing express.
Which method adds a GET route to an Express Router?
Arouter.post()
Brouter.get()
Crouter.route()
Drouter.use()
✗ Incorrect
router.get() defines a handler for GET requests on a route.
How do you connect a router to the main Express app with a prefix '/api'?
Aapp.use('/api', router);
Bapp.router('/api', router);
Capp.use(router, '/api');
Dapp.connect('/api', router);
✗ Incorrect
app.use('/api', router) mounts the router on the '/api' path.
What is the main benefit of using Express Router?
AAutomatically handles errors
BFaster server response
COrganizing routes into groups
DReplaces middleware
✗ Incorrect
Express Router helps organize routes by grouping related ones together.
Which of these is NOT a valid way to define a route on a router?
Arouter.listen(3000);
Brouter.post('/submit', handler);
Crouter.get('/home', handler);
Drouter.put('/update', handler);
✗ Incorrect
router.listen() is not a method; listening is done on the main app, not the router.
Explain how to create and use an Express Router in a simple app.
Think of router as a mini app for routes.
You got /4 concepts.
Why should you use Express Router instead of putting all routes in one file?
Compare it to organizing files in folders.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using express.Router() in an Express app?
easy
A. To handle database connections
B. To start the Express server
C. To create a modular set of routes that can be mounted separately
D. To serve static files
Solution
Step 1: Understand what express.Router() does
This function creates a new router object that can hold routes separately from the main app.
Step 2: Identify the purpose of modular routes
Modular routes help organize code by grouping related routes, making the app easier to maintain.
Final Answer:
To create a modular set of routes that can be mounted separately -> Option C
Quick Check:
Router = modular routes [OK]
Hint: Router organizes routes separately for cleaner code [OK]
Common Mistakes:
Thinking router starts the server
Confusing router with database handling
Assuming router serves static files
2. Which of the following is the correct way to create a new Express router?
easy
A. const router = express.Router();
B. const router = new express.Router();
C. const router = express.createRouter();
D. const router = Router();
Solution
Step 1: Recall the Express Router creation syntax
The correct method is calling Router() as a function on the express object.
A. Missing parentheses when creating router instance
B. Route path should be '/info/' with trailing slash
C. res.send should be res.json
D. module.exports should export an object, not router
Solution
Step 1: Check router creation syntax
The code uses express.Router without parentheses, so it assigns the function itself, not an instance.
Step 2: Confirm correct router instantiation
It should be express.Router() to create a router instance that supports route methods.
Final Answer:
Missing parentheses when creating router instance -> Option A
Quick Check:
Router needs () to create instance [OK]
Hint: Router must be called as a function with () [OK]
Common Mistakes:
Forgetting () after express.Router
Thinking trailing slash is required
Confusing res.send with res.json
5. You want to create two routers: one for /users and one for /products. How do you correctly mount them in your Express app so that requests to /users/list and /products/list work as expected?
hard
A. app.use(usersRouter); app.use(productsRouter);
B. app.use('/users', usersRouter); app.use('/products', productsRouter);
C. app.use('/list', usersRouter); app.use('/list', productsRouter);
D. app.get('/users', usersRouter); app.get('/products', productsRouter);
Solution
Step 1: Understand router mounting with path prefixes
To handle routes under /users and /products, mount each router with those paths using app.use().
Step 2: Check each option for correct usage
app.use('/users', usersRouter); app.use('/products', productsRouter); mounts routers correctly with prefixes. app.use(usersRouter); app.use(productsRouter); mounts without prefixes, so routes won't be under desired paths. app.use('/list', usersRouter); app.use('/list', productsRouter); mounts both under /list, causing conflicts. app.get('/users', usersRouter); app.get('/products', productsRouter); uses app.get incorrectly with routers.
Final Answer:
app.use('/users', usersRouter); app.use('/products', productsRouter); -> Option B
Quick Check:
Mount routers with correct path prefixes [OK]
Hint: Use app.use with path prefix for each router [OK]
Common Mistakes:
Mounting routers without path prefix
Using app.get instead of app.use for routers
Mounting multiple routers on same path
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.