In controllers or services, not in routes. Routes should be thin: they declare the endpoint and bind it to a controller. router.get('/', listUsers) is good. Putting logic in the router makes the file hard to maintain.
Express Router: Grouping and Mounting Explained
Where should business logic live in an Express app?
The answer
More FAQs in Express Router: Grouping and Mounting Explained
For grouping routes by feature in separate files. You create a router in routes/users.js, add routes to it, and mount it in app.js with app.use('/users', usersRouter). This keeps app.js clean as features grow.
Use app.use('/path', router). The router's paths are then relative to the mount path. For example, router.get('/:id') mounted at /users responds to /users/:id.
Yes. Use router.use(middleware) at the top of the router file. Every route in the router now runs that middleware. Useful for auth and role checks across a feature.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.