Knowledgebase

Structuring NestJS Applications Print

  • backenddevelopment, backend, caching, guide, howto, solution, zillionkinghost, hosting
  • 0

Modules and providers.

WHAT A MODULE IS

A grouping of related controllers and providers, with declared imports and exports.

WHY MODULES MATTER

They define what is available where, which prevents accidental coupling.

WHAT A PROVIDER IS

Anything injectable: services, repositories, factories.

WHAT TO EXPORT

Only what other modules genuinely need.

WHY

Exporting everything defeats the boundary.

WHAT TO PUT IN A CONTROLLER

Request handling, validation binding, and response shaping.

WHAT TO PUT IN A SERVICE

Business logic.

WHAT TO AVOID

Services depending on controllers Circular imports between modules A single module containing everything

WHAT PIPES PROVIDE

Transformation and validation of incoming data.

WHAT GUARDS PROVIDE

Authorisation decisions, before a handler runs.

WHAT INTERCEPTORS PROVIDE

Behaviour around handlers: logging, transformation, caching.

WHAT TO USE THEM FOR

Cross-cutting concerns, rather than repeating logic in every handler.

WHAT TO CONFIGURE GLOBALLY

Validation, so every endpoint validates by default.

WHY

Opt-in validation is eventually forgotten somewhere.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot