What Is NestJS?
Get introduced to NestJS.
We'll cover the following
In this lesson, we’ll explore the key components of NestJS and why developers choose this powerful framework. We’ll learn about the building blocks, such as modules, controllers, services, and more, that make NestJS a popular choice for web development.
NestJS architecture
NestJS uses a modular architecture that enables developers to generate reusable code and organize individual modules for specific concerns. Here are some building blocks in NestJS:
Modules: A module is a class mark with the
@Module()
decorator. It serves as a container for related controllers, providers, and other related codes. NestJS provides a powerful module system that allows developers to create reusable modules and organize their code.Controllers: A controller is responsible for handling incoming requests and returning responses to the client. It is a TypeScript class annotated with the
@Controller()
decorator. Controllers are used to define routes and endpoint handlers for the application.Services: In NestJS, a service is a TypeScript class annotated with the
@Injectable()
decorator. It is responsible for handling business logic, performing data operations, and providing functionality to other parts of the application through the NestJS dependency injection system.Pipes: Pipes can be applied to individual route handlers, controllers, or the entire application to transform input data before it is processed by a controller, making them useful for tasks such as validation and transformation.
Guards: A guard is a class mark with the
@Injectable()
decorator and implements theCanActivate
interface. NestJS applications use it to control access to endpoints by performing tasks such as authentication, authorization, and rate limiting.Middleware: Middleware functions access the request, response, and the next middleware in the stack. Middleware functions execute code, modify the request and response objects, end the request-response cycle, or call the next middleware in the stack.
Interceptors: NestJS uses interceptors as classes that intercept incoming HTTP requests and outgoing HTTP responses. Interceptors handle logging, error handling, caching, or response transformation.
After understanding the building blocks of NestJS, examining how these components integrate into the broader picture of handling HTTP requests and responses is essential. In NestJS, the framework structures the processing of a request through various stages, from the moment the application receives it until it returns a response. Middleware, guards, interceptors, pipes, controllers, and services collaborate in this workflow to process the incoming request and generate a response. The diagram below shows the typical life cycle of an HTTP request in a NestJS application:
Get hands-on with 1400+ tech skills courses.