新手入门NestJS(十一)- 控制器payloads请求

控制器payloads请求

如果通过Post请求来接收客户端的payloads参数

Nest.js通过使用@Body装饰器

首先创建一个DTO类,create-cats.dto.ts

1
2
3
4
5
export class CreateCatDto {
name: string;
age: number;
bread: string;
}

然后修改create方法

1
2
3
4
5
6
7
import { CreateCatDto } from 'src/create-cats.dto';

@Post()
async create(@Body() createCatDto: CreateCatDto) {
console.log(createCatDto);
return 'This action will create a new cat';
}

运行npm run start:dev

我们测试下

1
2
$ curl -d 'name=durban&age=12&bread=ddd' http://127.0.0.1:3000/cats
This action will create a new cat

可以看到console.log的输出结果如下

1
{ name: 'durban', age: '12', bread: 'ddd' }