Nest.js —— A progressive Node.js framework

1.nest指令

nest <command> [options]

Options:
    -h
    -v
Commands:
    new|n [options] [name]
    build [options] [app]
    start [options] [app]
    generate|g [options] <schematic> [name] [path]

    schematics:
        application|app
        module|mo
        controller|co
        library|lib

2.控制器

控制器负责处理传入的 请求 和向客户端返回 响应

// cats.controller.ts

import { Controller, Get } from '@nestjs/common';

class CreateCatDto {
  @ApiProperty({ description: '名称' }) // 模型属性
  name: string
}

@Controller('cats')
export class CatsController {
  @Get()
  findAll(): string {
    return 'This action returns all cats';
  }

  @Post()
  create(@Body() body: CreateCatDto ) {
    return {
      success: true
    }
  }
}

// localhost:3000/cats

 

 在上面的示例中,当对此端点发出 GET 请求时,Nest 会将请求路由到我们的用户定义 findAll() 方法。请注意,我们在此处选择的函数名称完全是任意的。我们显然必须声明一个绑定路由的函数,但 Nest 不会对所选的函数名称附加任何意义。

默认情况下,响应的状态码总是200,除了 POST 请求外,此时它是201,我们可以通过在处理程序层添加 @HttpCode(...)  装饰器来轻松更改此行为。

 3.swagger

 $ npm install --save @nestjs/swagger swagger-ui-express 

 打开引导文件(主要是 main.ts )并使用 SwaggerModule 类初始化 Swagger:

const options = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api-docs', app, document);

 

 装饰器

@ApiProperty({ description: '...', example: '...' })  // 模型属性
@ApiTags()  // api分组
@ApiOperation({ summary: 'description...' })  // 接口描述

 

 4.数据库

 $ npm install --save @typegoose/typegoose nestjs-typegoose mongoose   NPM

 $ npm install --save-dev @types/mongoose 

 连接mongodb

// db.module.ts

import { Module } from "@nestjs/common";
import { TypegooseModule } from "nestjs-typegoose";
import { CatsModule } from "./cat.module.ts";
 
@Module({
  imports: [
    TypegooseModule.forRoot("mongodb://localhost:27017/nest", {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
      useFindAndModify: true
    }),
    CatsModule,
  ],
})
export class ApplicationModule {}

定义模型

// cat.model.ts

import { prop } from "@typegoose/typegoose";
 
export class Cat {
  @prop({ required: true })
  name: string;
}

 

 

 全局引用模型

// db.module.ts

import { Module, Global } from '@nestjs/common';
import { DbService } from './db.service';
import { TypegooseModule } from 'nestjs-typegoose'
import { Cat } from './models/cat.model'

const models = TypegooseModule.forFeature([Cat])

@Global()
@Module({
  imports: [
    ...(连接数据库),
    models
  ],
  providers: [DbService],
  exports: [DbService, models],
})
export class DbModule {}

 

 依赖注入

// cats.controller.ts

import { Controller } from '@nestjs/common';
import { InjectModel } from 'nestjs-typegoose';
import { Cat } from '@libs/db/models/user.model';
import { ApiTags } from '@nestjs/swagger';

@Controller('users')
@ApiTags('用户')
export class UsersController {
  constructor(@InjectModel(Cat) private readonly model) {}
}

 

posted @ 2020-09-08 15:29  尾野  阅读(193)  评论(0编辑  收藏  举报