一、常用取参方式
- 请求参数装饰器,
Nest与express比较:
| Nest | express | 备注 |
@Request(), @Req() |
req |
请求对象 |
@Response(), @Res() |
res |
响应对象 |
@Next() |
next |
|
@Session() |
req.session |
请求携带的 session |
@Param(key?: string) |
req.params | req.params[key] |
获取请求携带的动态参数 xxx/user/dzm,dzm 就是动态参数 |
@Body(key?: string) |
req.body | req.body[key] |
获取请求体参数 |
@Query(key?: string) |
req.query | req.query[key] |
获取 url?id=dzm 携带的参数,通常是 get |
@Headers(key?: string) |
req.headers | req.headers[key] |
请求头内容 |
@Ip() |
req.ip |
请求访问 IP |
@HostParam() |
req.hosts |
Nest为所有标准HTTP方法提供装饰器@Get()、@Post()、@Put()、@Delete()、@Patch()、@Options()、@Head(),此外@All()定义处理所有这些的端点。- 请求参数场景取法,以
Get为例,参数按JSON传入,取值方式也适用其他的的装饰器:
import { Controller, Get, Query } from '@nestjs/common'; import { UserService } from './user.service'; @Controller('user') export class UserController { constructor(private readonly userService: UserService) { } @Get('dzm') getDzm(@Query() query: Record<string, any>): string { // 返回传入的整个对象 return JSON.stringify(query) } @Get('dzm1') getDzm1(@Query() query: Record<string, any>): Record<string, any> { // 返回传入的整个对象 return query } @Get('dzm2') getDzm2(@Query('name') name: string): string { // 取传入的指定字段进行返回 return name } }
二、Get 取传案例
Get案例1:@Query
@Get('dzm') getDzm(@Query() query: Record<string, any>): Record<string, any> { // 返回传入的整个对象 return query }
Get案例2:@Body
@Get('dzm') getDzm(@Body() body: Record<string, any>): Record<string, any> { // 返回传入的整个对象 return body }
Get案例3:@Param、@Body
@Get(':id') getDzm(@Param() param: Record<string, any>, @Body() body: Record<string, any>): Record<string, any> { // 返回传入的整个对象 return { ...param, ...body } }
三、POST 取传案例
POST案例1:@Body
@Post('dzm') getDzm(@Body() body: Record<string, any>): Record<string, any> { // 返回传入的整个对象 return body }
POST案例2:@Param、@Body
@Post(':id') getDzm(@Param() param: Record<string, any>, @Body() body: Record<string, any>): Record<string, any> { // 返回传入的整个对象 return { ...param, ...body } }
- 其他
@Put、@Delete ...请求取参也一样。
四、@Request()、@Response() 案例
@Req()是@Request()缩写,@Res()与@Response()同理,两者使用并没有什么区别。
@Get('/dzm') getDzm(@Query() query: any): any { // 返回传入的整个对象 return query } // 上面跟下面这么返回两者接口没有什么区别,只是上面不可以异步返回,下面的可以,看需求使用或者都使用下面这种。 // 下面这种还可以配置其他响应数据属性。 @Get('/dzm') getDzm(@Req() req: any, @Res() res: any): any { // 返回传入的整个对象 res.send(req.query) }
五、完整示例
- 示例代码,例如
@Body可以按约定的DTO传入
import { Controller, Get, Query, Post, Body, Put, Param, Delete } from '@nestjs/common'; import { CreateCatDto, UpdateCatDto, ListAllEntities } from './dto'; @Controller('cats') export class CatsController { @Post() create(@Body() createCatDto: CreateCatDto) { return 'This action adds a new cat'; } @Get() findAll(@Query() query: ListAllEntities) { return `This action returns all cats (limit: ${query.limit} items)`; } @Get(':id') findOne(@Param('id') id: string) { return `This action returns a #${id} cat`; } @Put(':id') update(@Param('id') id: string, @Body() updateCatDto: UpdateCatDto) { return `This action updates a #${id} cat`; } @Delete(':id') remove(@Param('id') id: string) { return `This action removes a #${id} cat`; } }





