What is the Express.js Router?
Answer
The Express Router is a mini-application that handles routing logic, allowing you to modularize your route definitions into separate files. Create a router: const router = express.Router();. Define routes on it: router.get('/', handler); router.post('/', handler);. Then mount it in the main app: app.use('/users', router). A GET to /users/ now matches router.get('/'). This is essential for organizing large applications — you create separate router files for each resource (routes/users.js, routes/posts.js) and mount them all in app.js. Routers can also have their own middleware, creating middleware scoped to specific route groups.
Previous
How do you parse request bodies in Express.js?
Next
How do you send different types of responses in Express.js?