Catalog / NestJS Cheat Sheet
NestJS Cheat Sheet
A comprehensive cheat sheet for NestJS, covering core concepts, modules, controllers, services, and common utilities.
Core Concepts
Modules
Definition: Modules organize application components. Usage: Use Example:
|
Imports: Used to import other modules. |
Controllers
Definition: Controllers handle incoming requests and return responses. Usage: Use Example:
|
@Get(), @Post(), @Put(), @Delete(), @Patch(), @Options(), @Head(): Methods to handle specific HTTP requests. |
Services
Definition: Services contain business logic and are used by controllers. Usage: Use Example:
|
Dependency Injection
Providers and Injection
@Injectable(): Marks a class as a provider. Constructor Injection Example:
|
Custom Providers: Allows defining custom provider logic. Value Provider Example:
|
Scopes
Default Scope (Singleton) |
Instances are created once and shared across the application. |
Request Scope |
Instances are created per request. Use |
Transient Scope |
Instances are not shared. New instance on each injection. |
Middleware & Interceptors
Middleware
Definition: Functions that are executed before the route handler. Usage: Implement Example:
|
Applying Middleware:
|
Interceptors
Definition: Interceptors augment request/response flow. Usage: Implement Example:
|
Applying Interceptors:
|
Pipes
Pipes Overview
Definition: Pipes transform or validate request data. Types:
|
Built-in Pipes
|
Validates request body using class-validator. |
|
Parses a string to an integer. |
|
Parses a string to a boolean. |
|
Parses a string to an array. |
|
Parses a string to a UUID. |
Custom Pipes
Creating a Custom Pipe:
|
Applying a Pipe:
|