InteractiveFrameworks

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
Start with First Steps

Building blocks

The four things every Nest application is made of, in the order the docs introduce them.

  1. 1
    First 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
  2. 2
    Controllers

    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
  3. 3
    Providers

    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
  4. 4
    Modules

    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.

  1. 5
    Pipes 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
  2. 6
    Middleware

    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
  3. 7
    Exception 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
  4. 8
    Guards

    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
  5. 9
    Interceptors

    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
  6. 10
    Custom 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