NestJS architecture
NestJS architecture: modules and dependency injection, provider scopes, the request pipeline and validation at the boundary.
4 lessons
Composition
- 1NestJS modules, custom providers and why DI makes testing cheapConsumers ask by token; the root decides what answers, and a test overrides that token, not the class.
- 2Provider scopes and request context: singleton fields and AsyncLocalStorageNever park a request's value on a singleton; carry it in the request's own AsyncLocalStorage store.
The request pipeline
- 3The request pipeline: middleware, guards, interceptors, pipes, handler, filtersOrder is behaviour: what a stage can see, and whether it runs at all, depends on what ran before it.
- 4Validation at the boundary: ValidationPipe, whitelisting, mass assignmentThe pipe checks rules, not shapes; strip or refuse the rest, and give every write a DTO naming only what that client may change.