Here, I would like to make a summary and a brief review of this chapter. Most of Nest’s common technology points are covered in this chapter, and they are relatively independent, but they need to work together to accomplish certain logical tasks for certain requirements.

Static architecture

If you look at Nest in two dimensions, dynamic and static, the static part has three key concepts: Provider, Module, and Decorator. Providers are used to complete specific service logic. A Module assembles business logic and its dependencies with an IoC architecture. A Decorator encapsulates some common business in another piece of code in the form of a Decorator (this particular function).

Module, Provider, controller class, and decorator relationship

Nest is an IoC container. A module acts as a group. Components in the group are marked with the @Injectable() decorator and injected into dependent objects when initialized. A controller is a special Provider that performs specific functions. General service classes, tool classes, and so on, can be generalized to the Provider.

Managed, managed scope of the injected object

Objects decorated with @Injectable() are hosted by Nest. There are three types of managed lifecycles (defined by the @Injectable() parameter), which are the same as the application lifecycles by default: initialized when initialized, destroyed when the application exits; The second is that each request generates one instance, which is relatively memory intensive and inefficient. The third is that each parent uses one instance.

Concept of dynamic

Implementation process

From the front end of the browser response to the Nest response back, the process goes as follows:

Object differences

type A declarative way use Interface or base class Realize the name Accessible object Function object
The middleware Classes, functions, Modular assembly NestMiddleware use(requires,next) Request/Response/Next Path (Mapping)
Anomaly filter class A decorator ExceptionFilter catch(exception,host) ArgumentsHost Global, controller, method
The pipe class A decorator PipeTransform transform(value,metadata) Parameter and parameter metadata Global, controller, method, parameter
The guards class A decorator CanActivate canActivate(context) ExecutionContext Global, controller, method
The interceptor class A decorator NestInterceptor intercept(context,next) ExecutionContext Global, controller, method

Platform independence and portability

Once again, Nest is not a Web framework, but an IoC container and a development philosophy. The real business is done by the underlying Web framework. By default, including in my case here, most implementations are based on Express. You can even build your own Nest based on KoA (though it doesn’t make much sense). Therefore, when dealing with certain details, it is particularly necessary to pay attention to the differences at the bottom. For example, when dealing with an ExecutionContext object, you want getType() to determine what type of connection it is.

The life cycle

The life cycle of a Nest application can be roughly divided into three phases: initialization, runtime, and completion. These three phases execute the following special methods throughout the lifecycle

Hook method instructions
onModuleInit() Execute after dependencies of the module are resolved (invalid in REQUEST scope)
onApplicationBootstrap() After all modules have been initialized, execute before listening on the port
onModuleDestory() Terminate signal received, executed before module destruction begins
beforeApplicationShutdown() The module destruction process completes (successfully or not), and once it completes, all listening connections are closed
onApplicationShutdown() Execute after the connection is closed

Flow chart:

In addition, Nest automatically calls (if any) methods on modules, providers, controllers, or other Nest hosted (@Injectable) classes at important nodes in the lifecycle. Such as:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [].controllers: [AppController],
  providers: [AppService],
})
export class AppModule {
  onApplicationBootstrap() {
    console.log('App Module Startup '); }}Copy the code

The onApplicationBootstrap of the above program will be executed during initialization. However, if you want to “gracefully” execute the program exit step before the application exits, there are two ways to do this: send the SIGTERM signal to the process, or execute app.close(). The former can utilize Kubernates to manage the lifecycle of the container.

Corresponds to the official document

If I were to compare everything I’ve published so far about Nest to the official Nest documentation, it would look something like this: Chapter 1 corresponds to the Fist Steps, Controllers, and a few other things in the official documentation overview; Chapter 2 deals with Controllers, Cookies, sessions in technology, and authorization and authentication in security in the official documentation overview. Chapter 3 corresponds to the rest of the official document overview and most of the basics.

When I first got to know Nest, I read the official documents carefully and found that they were too vague and messy. Neither Google nor Baidu had detailed descriptions. But I know from experience that Nest is an excellent framework (rated as the third most popular framework for Node JS in 2021) so it took a lot of careful research. The final result is a series of articles. Basically all the commonly used and key content has been introduced (95%+). There may be more to come in the future, so there should be some other IoC container application or experience how to use Nest. Anyway, I haven’t thought about it very clearly yet. Maybe you can give me some ideas and tell me what else can I dig?

That’s all, thank you all ~! (To be continued)