Nest中的控制器、路由、get()、post()与装饰器
nest的cli命令
nest g --help 命令 可以调出cli命令菜单
| application │ application │ Generate a new application workspace │
│ class │ cl │ Generate a new class │
│ configuration │ config │ Generate a CLI configuration file │
│ controller │ co │ Generate a controller declaration │
│ decorator │ d │ Generate a custom decorator │
│ filter │ f │ Generate a filter declaration │
│ gateway │ ga │ Generate a gateway declaration │
│ guard │ gu │ Generate a guard declaration │
│ interceptor │ in │ Generate an interceptor declaration │
│ interface │ interface │ Generate an interface │
│ middleware │ mi │ Generate a middleware declaration │
│ module │ mo │ Generate a module declaration │
│ pipe │ pi │ Generate a pipe declaration │
│ provider │ pr │ Generate a provider declaration │
│ resolver │ r │ Generate a GraphQL resolver declaration │
│ service │ s │ Generate a service declaration │
│ library │ lib │ Generate a new library within a monorepo │
│ sub-app │ app │ Generate a new application within a monorepo │
│ resource │ res │ Generate a new CRUD resource |
NestJS 中的路由
NestJS 中没有单独配置路由的地方,定义好控制器后nestjs会自动配置对应的路由
import { Controller, Get, Query, Request, Body } from '@nestjs/common';
@Controller('user')
export class UserController {
@Get()
index() {
return '此get请求通过 http://localhost/user 访问';
}
// 通过 @Query装饰器获取get传值
@Get('add')
addData(@query() query) {
console.log(query + '是获取到get请求的参数');
retrun '此get请求通过 http://localhost/user/add 访问';
}
// 通过 @Request装饰器获取get传值
@Get('edit')
addEdit(@Request() req) {
console.log(req.query);
retrun '此get请求通过 http://localhost/user/edit 访问';
}
// 通过 @Body装饰器获取post传值
@Post('create')
addEdit(@Body() body) {
console.log(body);
retrun '此get请求通过 http://localhost/user/create 访问';
}
// 获取动态路由 localhost/user/123
@Get(":id")
index(@Param() param) {
console.log(param); // {id: '123'}
return '获取动态路由'
}
}
类似的还有 Put() 、Delete()、 ... 等装饰器

浙公网安备 33010602011771号