Facebook Pixel

When should I use the module pattern in Express?

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.

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.

Business logic. Functions that use the model and utilities to do work. createUser hashes the password, checks for duplicates, and creates the user. Services are reusable and testable without an HTTP context.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.