A Complete NestJS CRUD Module, Layer by Layer
NestJS's opinionated three-layer architecture is the default for enterprise Node.js backends, and this pattern — Controller, Service, Module — repeats identically whether the module handles todos, auth, or payments. Understanding the exact wiring and data flow removes the guesswork when scaling from a toy to a production service.
Starting from a simple in-memory data model, a complete Todos API comes together across four distinct layers. The Service layer houses all business logic and uses NestJS's built-in `NotFoundException` to produce standard HTTP 404 responses. The Controller layer maps HTTP verbs to routes, extracts path parameters and request bodies with dedicated decorators, and delegates work to the injected Service. The Module layer registers both Controller and Service, then plugs into the root AppModule. The full request lifecycle — from route matching and parameter parsing through dependency injection and exception filtering — is traced end-to-end for a `GET /todos/1` call. A Jest unit-test scaffold and curl verification commands round out the implementation.
The tutorial's emphasis on `Partial<Todo>` and `Object.assign` for PATCH operations highlights a common NestJS design choice: the Controller trusts the Service to handle partial updates correctly, keeping HTTP concerns out of business logic.
Using an in-memory array rather than a database is a deliberate pedagogical trade-off that forces attention onto the framework's wiring — the exact same decorator and injection patterns carry over unchanged to real persistence layers, which is the real lesson.
The explicit mapping of exception types to HTTP status codes (400, 401, 403, 404, 500) reveals how NestJS treats HTTP as an output concern of the framework, not something the developer manually constructs in every handler.