How do you version Express APIs with routers?
Mount routers under /api/v1, /api/v2, etc. app.use('/api/v1/users', require('./routes/v1/users')). Each version has its own router. Bump on breaking changes; non-breaking changes (adding fields) do not need a new version.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Express Router Mounting and Nesting Explained
app.use('/path', router). The router's paths are then relative to /path. For example, router.get('/:id') mounted at /users responds to /users/:id. Mount in app.js to keep it thin.
Yes. Mount one router inside another: router.use('/:userId/posts', postsRouter). Now /users/:userId/posts/... hits postsRouter. Limit nesting to one level; for deeper resources, use a separate top-level router with a filter.
router.use(middleware) at the top of the router file applies the middleware to every route in the router. Useful for auth and role checks across a feature without repeating per route.
Still have questions?
Browse all our FAQs or reach out to our support team
