How do I mount modules in app.js?
app.use('/api/v1/users', require('./modules/users/users.router')). Each module exports its router. Mount under a versioned path. app.js stays thin: global middlewares, module mounts, error handler.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Building Modular Express APIs With Routers and Services
Group each feature into a module: router, controller, service, model, validations in one folder (src/modules/users/). Mount modules in app.js. Each module is cohesive, independent, testable, and reusable.
Cohesion (everything for a feature in one folder), independence (a module can be extracted into a microservice later), testability (test the service without HTTP context), team scalability (different teams own different modules), and reusability across apps.
For medium to large apps. For tiny apps with 3 routes, a flat structure (routes/, controllers/, models/) is fine. The module pattern adds folder nesting that is not worth it for tiny apps.
Still have questions?
Browse all our FAQs or reach out to our support team
