Skip to content

Nest中的装饰器

Nest中的装饰器#

TS中的常见的装饰器类型有5种,包括类装饰器。方法装饰器、属性装饰器、参数装饰器和访问器装饰器。

在Nest中实现了前4种装饰器:

当然,常用的装饰器远远不只我列举的这些,这里只是简单给大家提及一下最常用的,其他的一些大家可以慢慢认识

上面的这些装饰器,我们之前用过一些,有一些我们后面课程会慢慢解释,这里先把我们可能常用到一些参数装饰器集中解释一下,对于前端来说,后端主要是提供 http 接口来传输数据,而这种数据传输的方式主要有 5 种:

对于url paramquery其实就是GET方式的请求,只是url处理不一样

@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
@Get('find')
query(@Query('name') name: string, @Query('age') age: number) {
return this.userService.query(name, age)
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.userService.findOne(+id)
}
}

这里要注意

find的路由要放到 的路由前面,Nest 是从上往下匹配的,如果放在后面,就会先匹配到 :id 路由

url param

image-20250126112317190

query

image-20250126112410183

form-urlencoded

首先来看请求的样式:

image-20250126112605687

用 Nest 接收的话,使用 @Body 装饰器,Nest 会解析请求体,然后注入到 dto

dto 就是 data transfer object,就是用于封装传输的数据的对象,我们之前在讲解三层架构的时候专门提到过

create-user.dto.ts

export class CreateUserDto {
name: string
age: number
}

user.controller.ts

@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
@Post()
create(@Body() createUserDto: CreateUserDto) {
return this.userService.create(createUserDto)
}
}

由于使用了三层架构,service中稍微做一下处理

@Injectable()
export class UserService {
create(createUserDto: CreateUserDto) {
return (
'This action adds a new user ---' + JSON.stringify(createUserDto) + '---'
)
}
}

JSON

JSON方式无非就是前端传输的时候,选择的content-typapplication/json,内容会以 JSON 的方式传输。后端代码同样使用@Body 来接收,在Nest中的处理和form-urlencoded是一样的

image-20250126113400805

form-data的方式由于还需要用到其他的一些内容,我们后面通过例子一并讲解,除了这些常用的参数装饰器之外,还有其他的参数装饰器,比如,如果我们想拿到IP地址,拿到请求头信息这些内容,就可以直接通过参数装饰器获取

@Get('other')
other(
@Ip() ip: string,
@Headers() headers: Record<string, any>,
@Req() request: Request,
) {
console.log(ip);
console.log(headers);
console.log(request.url);
return 'other';
}

上面通过@Ip()@Headers()装饰器就获取IP地址和头信息,通过@Req()直接获取request请求对象。在Nest中@Req@Request是一致的

既然能获取请求对象,那么响应对象Response也能获取,可以通过注解@Res或者@Response,不过注意一点,如果手动加入了响应对象,那么就必须自己通过响应对象处理返回信息,比如,上面的方法如果我们手动加上了@Res,直接请求就会出现问题

@Get('other')
other(
@Ip() ip: string,
@Headers() headers: Record<string, any>,
@Req() request: Request,
@Res() response: Response,
) {
console.log(ip);
console.log(headers);
console.log(request.url);
return 'other';
}

image-20250126115731436

这里handler就不会通过返回的字符串帮我们进行响应了,需要我们自己手动处理。

@Get('other')
other(
@Ip() ip: string,
@Headers() headers: Record<string, any>,
@Req() request: Request,
@Res() response: Response,
) {
console.log(ip);
console.log(headers);
console.log(request.url);
response.end('other');
}

注意:

这里的RequestResponse对象,需要从express中导入,才能方便的使用相关方法

import { Request, Response } from 'express';