Contents
NestJS
A framework for server applications in TypeScript: modules, controllers, services and databases.
1.Getting started
- What NestJS isWhy NestJS exists, how it relates to Express and what you need to get started.
- Creating a projectHow to create a NestJS project, what its folders contain and what decorators are.
- Generating code: the nest CLIHow to create modules, controllers, services and whole resources with nest generate.
2.Modules, controllers and services
- ModulesHow NestJS splits an application into modules and how one module uses another module's service.
- Controllers and routesHow a controller maps addresses and HTTP methods to class methods and what it returns to the client.
- Route parameters, query and bodyHow a controller method receives part of the address, query string parameters and JSON from the body.
- DTOs: describing input dataWhat a DTO is, why it is a class rather than an interface, and how to describe data for creating and updating a post.
- Services and dependency injectionWhere application logic lives, what a provider is and how NestJS passes a service into a controller for you.
3.The request pipeline
- Validation: ValidationPipe and class-validatorHow to check the request body with rules in a DTO and answer the client with a 400 error automatically.
- PipesBuilt-in pipes for converting parameters and how to write your own pipe.
- Exceptions and exception filtersHow to answer the client with a 404 or 400 error and how to change the format of all error responses.
- MiddlewareHow to run code before any controller: a middleware class, applying it in a module and choosing routes.
- GuardsHow to allow or deny a controller method call: the CanActivate interface and the UseGuards decorator.
- InterceptorsHow to run code before and after a controller method and change its response: the NestInterceptor interface and RxJS.
4.Configuration and database
- Configuration: @nestjs/config and .envHow to keep the port, paths and secret keys in a .env file and read them through ConfigService.
- Connecting a database: TypeORMHow to connect a database to NestJS through TypeORM and the @nestjs/typeorm module.
- Entities and repositoriesHow to describe a table with an entity class and read and save rows through a TypeORM repository.
- Relations: posts and commentsHow to link two entities with a one-to-many relation and load a post together with its comments.
- CRUD for posts end to endThe complete posts service and controller: a paginated list, viewing, creating, updating and deleting.
5.Users and access
- Users and password hashingA user entity, registration and storing the password as a hash with bcrypt.
- Login with a JWTHow to issue a JWT to the user at login and check it with a guard on protected routes.
- Roles and permissionsHow to allow an action only for users with the right role: a Roles decorator, Reflector and a roles guard.
- Custom decoratorsA parameter decorator for the current user with createParamDecorator and combining decorators with applyDecorators.
6.Useful features
- File uploadsHow to accept an image for a post: FileInterceptor, UploadedFile, size and type checks, serving uploaded files.
- Serializing responsesHow to hide the password and other fields in API responses with ClassSerializerInterceptor and the Exclude decorator.
- API documentation: SwaggerHow to get interactive OpenAPI documentation for your API with the @nestjs/swagger package.
- CachingHow to remember responses and database query results: CacheModule, CacheInterceptor and working with the cache by hand.
- Scheduled tasksHow to run code on a schedule in NestJS: the @nestjs/schedule module and the Cron, Interval and Timeout decorators.
- Logging: the built-in LoggerHow to write messages to the log with the Logger class, choose levels and switch to JSON output.
7.Testing and deployment
- Unit testsHow to test a service apart from the database: Jest, Test.createTestingModule and a fake repository.
- End-to-end tests with supertestHow to test the whole API: start the application in a test, send HTTP requests with supertest and check the responses.
- Running on a serverHow to build a NestJS project, run it in production mode, set environment variables and keep the process running.