InteractiveFrameworks

Techniques

The docs' Techniques chapter on the same cats API: configuration from .env, validation and serialization, a real database with TypeORM, then the HTTP toolbox (versioning, cookies, sessions, uploads, streamed files, server-sent events, calling other services) and what runs behind a request (caching, logging, events, scheduled tasks).

lessons
18
level
intermediate
time
9 hours
Start with Configuration

Data

What comes in and what goes out: environment configuration, request validation, response serialization, and a database the cats finally live in.

  1. 1
    Configuration

    Move the shelter's name and capacity out of the code and into .env: load it with ConfigModule, shape it with a configuration factory, validate it before the app boots, and read it through ConfigService.

    Read the theory
  2. 2
    Validation

    Enforce the API's contract on every route: a global ValidationPipe that strips and refuses unknown properties and transforms payloads, an update DTO derived from the create DTO, and validated arrays in the body and the query string.

    Read the theory
  3. 3
    Serialization

    Decide once, on the class, what leaves the server: exclude the microchip, expose a computed label, flatten the owner to a name, and apply the same rules to plain objects with SerializeOptions.

    Read the theory
  4. 4
    Entities and Repositories

    Move the cats into a database: connect with TypeOrmModule, turn the Cat class into an entity, register it so a repository can be injected, and write the service against find, save, delete and the query builder.

    Read the theory
  5. 5
    Relations

    Give cats owners: a one-to-many and its many-to-one, typed with Relation<> so the two entity files can import each other under ES modules, loaded on request and set by assignment.

    Read the theory
  6. 6
    Transactions and Subscribers

    Make an adoption all-or-nothing with dataSource.transaction() and its explicit QueryRunner form, and tidy every cat's name before it is written with an entity subscriber.

    Read the theory
  7. 7
    Testing with Repositories

    Unit-test a service that talks to the database without a database: provide a mock under the repository's token with getRepositoryToken, script its answers, and prove the service's rules from what it asked the repository for.

    Read the theory

HTTP

The protocol's toolbox: several API versions side by side, cookies and sessions, uploads, files streamed down, events streamed out, and calling another service.

  1. 8
    Versioning

    Ship version 2 of the cats API without breaking version 1: URI versioning with a default for the controllers that existed before, a version 2 controller on the same path, a route shared by both versions, and a health check outside them.

    Read the theory
  2. 9
    Cookies

    Remember a visitor between requests: parse cookies with cookie-parser, read them through custom decorators, set a plain cookie for a theme and a signed one for a favourite cat that nobody can forge.

    Read the theory
  3. 10
    Session

    Keep each visitor's data on the server behind a signed session cookie: count visits, remember the cats they looked at, and forget them on request, with express-session and @Session().

    Read the theory
  4. 11
    File Upload

    Accept a photo for a cat: parse multipart with FileInterceptor, validate the file's size and real type with ParseFilePipe, read the fields beside it, and take several documents at once with FilesInterceptor.

    Read the theory
  5. 12
    Streaming Files

    Send bytes, not JSON: stream a file from disk with StreamableFile, declare its type and download name three ways, refuse a missing file before streaming, and export the cats as a CSV built in memory.

    Read the theory
  6. 13
    Server-Sent Events

    Push adoptions to clients as they happen: a replaying stream of events in the service, an @Sse() route that merges it with a heartbeat, and the text/event-stream wire format Nest writes for you.

    Read the theory
  7. 14
    HTTP Module

    Call another service from the cats API: HttpModule with a timeout, HttpService's observables turned into promises, and every way the upstream can fail translated into the right status for your own client.

    Read the theory

Behind the request

What runs beside or after the handler: a cache in front of slow work, a log that says what happened, events that decouple the parts, and tasks on a clock.

  1. 15
    Caching

    Compute once, serve many times: CacheModule with a default lifetime, a route cached automatically with CacheInterceptor and invalidated on writes, and a cache-aside lookup by hand with the cache manager.

    Read the theory
  2. 16
    Logging

    Log through Nest's Logger with a class context, then make the application use a logger of your own that extends ConsoleLogger and keeps what it prints, with the boot log buffered until it exists.

    Read the theory
  3. 17
    Events

    Decouple what happens after an adoption from the code that adopts: CatsService emits namespaced events with typed payloads, and an audit module that never imports it listens, with a wildcard for the whole namespace.

    Read the theory
  4. 18
    Task Scheduling

    Work on a clock rather than on a request: feed the cats on an interval, open the shelter once after boot, report on a cron schedule, and pause and resume a named task through the SchedulerRegistry.

    Read the theory