Basics
The mental model of every Nest application: modules, controllers, providers and dependency injection, then each stage of the pipeline Nest runs around a request handler. One lesson per page of the official docs' Overview chapter, on one growing cats API.
- lessons
- 10
- level
- beginner
- time
- 4 hours
Building blocks
The four things every Nest application is made of, in the order the docs introduce them.
- 1First Steps
Boot a Nest application from one module, one controller and one provider, read what Nest prints, and return your first JSON response.
Read the theory - 2Controllers
Read route and query parameters, accept a JSON body, await an async service, and override the status code Nest picks for a POST.
Read the theory - 3Providers
Move the logic into an injectable service that keeps state across requests, and answer 404 with NotFoundException when a cat does not exist.
Read the theory - 4Modules
Declare a feature module that owns the cats code, import it from the root module, and export the service so another module shares the same instance.
Read the theory
The request pipeline
What Nest runs around a handler, one stage per lesson: validation first, because it completes the API, then middleware, exception filters, guards, interceptors, and the decorators that tie them to handlers.
- 5Pipes and Validation
Validate request bodies with a DTO class and ValidationPipe, strip unknown fields, and let ParseIntPipe turn a route parameter into a number or a 400.
Read the theory - 6Middleware
Write a middleware class that records every request to the cats routes and nothing else, and apply it from the module with configure().
Read the theory - 7Exception Filters
Shape every HttpException response with a filter bound to the whole application, and see what the exceptions layer does with an error it does not recognise.
Read the theory - 8Guards
Decide who may call a handler with a guard that reads role metadata off the handler through the Reflector, and answer 401 or 403 depending on why.
Read the theory - 9Interceptors
Wrap handlers with interceptors: name the handler in a response header on the way in, and map every response body into an envelope on the way out.
Read the theory - 10Custom Decorators
Write a parameter decorator that hands handlers the current user or one of its properties, and compose role metadata and a guard into one @Auth() decorator.
Read the theory