The first step in learning NestJs

Prerequisites for installing NestJS and Installing NestJS

The version of NodeJS needs to be greater than or equal to 16.

The command to install NestJS is: npm i -g @nestjs/cli.

Create a project using the command

Use nest new <项目名称>to create a project, if you want to enable the strict syntax function of TS, you can pass --strictthe flag to nest new <项目名称>, that is, the final generated command is nest new <项目名称> --strict.

The project created with --strictthe flag will enable the parameter settings of TS's strong check verification in the TS configuration file. The specific comparison is as follows:

insert image description here

Directory Structure Description

After using the command to create the project, we can see the following files in the src directory of the project:

  • app.controller.spec.ts (unit tests for controllers)
  • app.controller.ts (basic controller, this file will be deleted later when you make your own project)
  • app.module.ts (the root module of the application, that is, log configuration, business module, scheduled tasks, etc. can be set here, which is the most important file)
  • app.service.ts (basic service, this file will be deleted later when you do your own project)
  • main.ts (the entry file of the application of the Nest application instance, this is the core, cannot be deleted, and will be used when the project starts)

main.ts file analysis

import {
    
     NestFactory } from "@nestjs/core";
import {
    
     AppModule } from "./app.module";

async function bootstrap() {
    
    
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

In the above code, we can see that we use NestFactoryto create services, so the core class in NestJS is NestFactorythe class.

create()method returns an application object that implements the INestApplication interface. The objects of this application will be slowly elaborated in subsequent notes.

createBy default, the application created by the method closes the application and prints the error message when encountering an exception. When we want the application to throw an exception, we can create the application in this way. The specific code is as follows:

const app = await NestFactory.create(AppModule, {
    
     abortOnError: false });

Platform Instructions for NestJS

NestJS is a platform-independent framework. In order to be able to plug in reusable logic and simplify development, NestJS now supports two platforms, one is Express (NestExpressApplication), and the other is fastify (NestFastifyApplication). And we generally use the Express platform by default when we create.

When we need to use a specific platform, we can create a service like this.

const app = await NestFactory.create<NestExpressApplication>(AppModule);

Project begining

The command to start the project is as follows:

$ npm run start

If we want to be in development mode, we can enable the following command to monitor file changes at all times, so that the program can be restarted hot.

$ npm run start:dev

Guess you like

Origin blog.csdn.net/qq_33003143/article/details/131886683