NestJS Nest.js个人学习笔记

NestJS官网

  1. OpenAPI (Swagger) 相关
    npm install --S @nestjs/swagger swagger-ui-express

    // pic.controller.ts
    import { ApiTags, ApiProperty, ApiOperation } from '@nestjs/swagger';
    
    // 定义dto
    export class GetPicDto {
      @ApiProperty({
        default: '1',
      })
      sort: string;
    
      @ApiProperty({ default: '1' })
      pageNum: string;
    
      @ApiProperty({ default: '10' })
      pageSize: string;
    }
    
    // 添加分组
    @ApiTags('图片')
    @Controller('pic')
    export class PicController {
      @Post()
      @ApiOperation({
        // 添加接口描述
        summary: '添加图片',
      })
      async create(@Body() createPicDto: CreatePicDto) {
        return await this.PicModel.create(createPicDto);
      }
    }
    
  2. 使用@typegoose/typegoose 生成模型
    npm i -S @typegoose/typegoose mongoose
    npm i -D @types/mongoose

    // pic.model.ts
    import { prop } from '@typegoose/typegoose';
    
    export class Pic {
      @prop()
      sort: string;
    
      @prop()
      url: string;
    }
    
  3. 使用 nestjs-typegoose 注入模型
    npm install --S nestjs-typegoose

    // app.module.ts
    import { Module } from '@nestjs/common';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    import { TypegooseModule } from 'nestjs-typegoose';
    import { PicModule } from './pic/pic.module';
    
    @Module({
      imports: [
        //1. 连接mongoose
        TypegooseModule.forRoot('mongodb://localhost/pic-server', {
          useNewUrlParser: true, // 去除警告
          useFindAndModify: false, // 去除警告
          useCreateIndex: true, // 去除警告
        }),
        PicModule,
      ],
      controllers: [AppController],
      providers: [AppService],
    })
    export class AppModule {}
    
    // pic.module.ts
    import { Module } from '@nestjs/common';
    import { PicController } from './pic.controller';
    import { TypegooseModule } from 'nestjs-typegoose';
    import { Pic } from './pic.model';
    @Module({
      imports: [TypegooseModule.forFeature([Pic])], //2. 在这里导入模型
      controllers: [PicController],
    })
    export class PicModule {}
    
    // pic.controller.ts
    import { Pic } from './pic.model';
    import { Controller } from '@nestjs/common';
    import { InjectModel } from 'nestjs-typegoose';
    import { ReturnModelType } from '@typegoose/typegoose';
    
    @Controller('pic')
    export class PicController {
     //3. 在constructor 里注入模型
     constructor(
       @InjectModel(Pic) private readonly PicModel: ReturnModelType<typeof Pic>,
     ) {}
    
    }
    
  4. 使用 class-validator 进行参数校验
    npm i --S class-validator class-transformer

    //1. main.ts
    import { ValidationPipe } from '@nestjs/common';
    app.useGlobalPipes(new ValidationPipe())
    
    //2. create-cat.dto.ts
    import { IsString, IsInt } from 'class-validator';
    
    export class CreateCatDto {
      @IsString()
      @IsNotEmpty({
        message: '请填写名称',
      })
      name: string;
    
      @IsInt()
      age: number;
    
      @IsString()
      breed: string;
    }
    

个人学习笔记仅供参考,欢迎各位大佬批评

你可能感兴趣的:(node,nest.js)