好像nest真没什么前途,弃坑了
初始化项目
pnpm i -g @nestjs/cli
nest new project-name
项目结构
|-- README.md
|-- nest-cli.json
|-- package.json
|-- pnpm-lock.yaml
|-- src
| |-- app.controller.spec.ts
| |-- app.controller.ts // 控制器
| |-- app.module.ts
| |-- app.service.ts
| |-- main.ts // 入口文件
|-- test
| |-- app.e2e-spec.ts
| `-- jest-e2e.json
|-- tsconfig.build.json
`-- tsconfig.json
常用命令
使用查看
nest -h
快速生成模块
nest g [模块名]
控制器
使用@request()获取参数
@Get()
getAllUser(@Request() req) {
console.log(req.query);
return {
code: 200,
};
}
可以使用@Query()
@Get()
getAllUser(@Query() query) {
return {
code: 200,
msg: 'success',
data: query,
};
}
Post读Body也差不多
@Post()
create(@Body() body) {
console.log(body);
return {
code: 200,
};
}
动态路由的Param传参
@Put(':id')
update(@Param() id) {
console.log(id);
return {
code: 200,
};
}
使用@header读取请求头
@Put(':id')
update(@Param() id, @Headers() headers) {
console.log(headers);
console.log(id);
return {
code: 200,
};
}