How do I apply middlewares to a group of routes in Express?
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.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Express Router Best Practices for Large Apps
One router per feature (routes/auth.js, routes/users.js, etc.). Mount each under a path in app.js. Apply per-router middlewares. Keep public routes outside auth routers. Keep app.js thin: only global middlewares, router mounts, and the error handler.
Mount routers under /api/v1, /api/v2, etc. Each version has its own router. Bump the version on breaking changes. Non-breaking changes (adding fields) do not need a new version.
Yes for nested resources (e.g., /users/:userId/posts). Use router.use('/:userId/posts', postsRouter). Limit nesting to one level. For deeper resources, use a separate top-level router with a filter.
Still have questions?
Browse all our FAQs or reach out to our support team
