Nest specific tools
Nest provides a set of Nest.js specific tools that can be used throughout the application to help with writing reusable code and following SOLID principles. These decorators will be used in each of the subsequent chapters, as they define a specific functionality:
- @Module: The definition for this reusable package of code within the project, it accepts the following parameters to define its behavior. ⋅⋅ Imports: These are the modules that contain the components used within this module. ⋅⋅ Exports: These are the components that will be used in other modules, that import this module. ⋅⋅ Components: These are the components that will be available to be shared across at least this module through the Nest Injector. ⋅⋅ Controllers: The controllers created within this module, these will define the API endpoints based on the routes defined.
- @Injectable: Almost everything in Nest is a provider that can be injected through constructors. Providers are annotated with
@Injectable()
. .. Middleware: A function that is run before a request is passed to the route handler. In this chapter, we will talk about the difference between Middleware, Async Middlewares and Functional Middleware. .. Interceptor: Similar to Middleware, they bind extra logic before and after the execution of a method, and they can both transform or completely override a function. Interceptors are inspired by Aspect-Oriented Programming (AOP). .. Pipe: Similar to part of an Interceptors functionality, Pipe transforms input data to the desired output. .. Guard: A smarter and more niche Middleware, Guards have the singular purpose of determining if a request should be handled by the router handler or not. ..* Catch: Tell anExceptionFilter
what exception to look for and then bind data to it. - @Catch: Binds metadata to the exception filter and tells Nest that a filter is looking only for the exceptions listed in the
@Catch
.
Note: In Nest Version 4 not everything under @Injectable()
listed above uses the @Injectable()
decorator. Components, Middlewares, Interceptors, Pipes, and Guards each have their own decorator. In Nest Version 5 these have all been combined to @Injectable()
to reduced the differences between Nest and Angular.