Nest入门到精通
2022-07-17 00:26:12
2025-01-02 15:52:28
起步
js
npm i -g @nestjs/cli
创建项目
js
nest new project-name
创建 Module
modules文件下的user
language
nest g module user modules
创建 Controller
language
nest g controller user modules
创建 Provider
language
nest g service user modules
直接生成一个CRUD模块的navigation,选择PEST API
shell
nest g resource navigation modules
配置swagger
新建文件src/swagger/index.ts
language
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
const swaggerOptions = new DocumentBuilder()
.setTitle('小白龙博客接口文档')
.setDescription('小白龙博客文档')
.setVersion('1.0.0')
.addBearerAuth()
.build();
export function createSwagger(app) {
const document = SwaggerModule.createDocument(app, swaggerOptions);
SwaggerModule.setup('swagger-doc', app, document);
}
配置功能模块标签与接口名字
language
import { LinksSetDto } from './dto/links.set.dto';
import { ApplyLinksService } from './apply-links.service';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { Controller, Get, Post, Query, Body } from '@nestjs/common';
@ApiTags('申请友链')
@Controller('apply-links')
export class ApplyLinksController {
constructor(private readonly applyLinksService: ApplyLinksService) {}
@Get('/set')
@ApiOperation({ summary: '创建申请友链' })
set(@Query() params: LinksSetDto) {
return this.applyLinksService.set(params);
}
@Get('/query')
@ApiOperation({ summary: '获取申请友链列表' })
query(@Query() params) {
return this.applyLinksService.query(params);
}
@Post('/del')
@ApiOperation({ summary: '删除{id}申请友链' })
del(@Body() params) {
return this.applyLinksService.del(params);
}
}
目录