0
0
Expressframework~5 mins

Creating an Express Router - Quick Revision & Summary

Choose your learning style9 modes available
Recall & Review
beginner
What is an Express Router used for?
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();
Which method adds a GET route to an Express Router?
Arouter.post()
Brouter.get()
Crouter.route()
Drouter.use()
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);
What is the main benefit of using Express Router?
AAutomatically handles errors
BFaster server response
COrganizing routes into groups
DReplaces middleware
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);
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.