七、Controller控制器 Get 请求
7.1、get请求 -- 带参数
src/products/products.controller.ts
@Get('/getProductsById') // @Request()装饰器 // getProductsById(@Request() req):any{ // let id:number = parseInt(req.query.id) // return this.productsService.getProductsById(id) // } // @Query()装饰器 getProductsById(@Query() query):any{ let id:number = parseInt(query.id) return this.productsService.getProductsById(id) }
src/products/products.service.ts
getProductsById(id:number){ let resJson:any = {} switch (id) { case 1: resJson = {id:1, name: 'huawei', type: 'shouji'} break; case 2: resJson = {id:2, name: 'changcheng', type: 'qiche'} break; case 3: resJson = {id:3, name: 'biyadi', type: 'qiche'} break; } return resJson; }
RESTClient/demo.http
7.2、Get请求-@Request()装饰器-动态参数id
src/products/products.controller.ts
// Get请求-@Request()装饰器-动态参数id @Get('/getProductsByIdRouter/:id') getProductsByIdRouter(@Request() req):any{ console.log() let id:number = parseInt(req.params.id) return this.productsService.getProductsByIdRouter(id) }
src/products/products.service.ts
getProductsByIdRouter(id:number){ let resJson:any = {} switch (id) { case 1: resJson = {id:1, name: 'huawei', type: 'shouji'} break; case 2: resJson = {id:2, name: 'changcheng', type: 'qiche'} break; case 3: resJson = {id:3, name: 'biyadi', type: 'qiche'} break; } return resJson; }
RESTClient/demo.http
http://localhost:3000/api/products/getProductsByIdRouter/1 HTTP/1.1
7.3、Get请求-@Param()装饰器-动态参数id,name
src/products/products.controller.ts
// Get请求-@Param()装饰器-动态参数id,name @Get('/getProductsByIdRouter/:id/:name') getProductsByIdRouter2(@Param() Params):any{ console.log() let id:number = parseInt(Params.id) let name:string = Params.name return this.productsService.getProductsByIdRouter2(id, name) }
src/products/products.service.ts
getProductsByIdRouter2(id:number, name:string = "huawei"){ let resJson:any = {} switch (id) { case 1: resJson = {id:1, name: name, type: 'shouji'} break; case 2: resJson = {id:2, name: name, type: 'qiche'} break; case 3: resJson = {id:3, name: name, type: 'qiche'} break; } return resJson; }
RESTClient/demo.http
http://localhost:3000/api/products/getProductsByIdRouter/1/huawei-mate-60 HTTP/1.1
八、Controller控制器 Post 请求
8.1、post请求
src/products/products.controller.ts
@Post('/addProducts') addProducts(): any{ return this.productsService.addProducts() }
src/products/products.service.ts
addProducts(){ return { code: 200, data: ['huawei', 'changcheng', 'biyadi'], msg: '增加产品成功' } }
8.2、post请求 -- 带参数
src/products/products.controller.ts
// @Query()装饰器 getProductsById(@Query() query):any{ let id:number = parseInt(query.id) return this.productsService.getProductsById(id) }
src/products/products.service.ts
addProducts2(id: any){ console.log(id) let resJson:any = { code: 200, data: ['huawei', 'changcheng', 'biyadi'], msg: '增加产品成功' } switch (id) { case 1: resJson.data = ['huawei'] break; case 2: resJson.data = ['changcheng'] break; case 3: resJson.data = ['biyadi'] break; } return resJson }
RESTClient/demo.http
POST http://localhost:3000/api/products/addProducts2 Content-Type: application/json { "id": 1 }
九、TypeORM 连接mysql数据库,增删改查
NestJS:TypeORM 连接mysql数据库,增删改查_snow@li的博客-CSDN博客
十、其他内容待更新
过程记录:
记录一、nestjs热更新
nestjs自带nodemon可以热更新,启动方式npm run start:dev(package.json里)
记录二、使用VsCode插件 REST Client
项目根目录创建RESTClient目录,demo.http文件
POST 接口地址 HTTP/1.1 点击Send Request 请求结果如右图
记录三、
Content-Type 和 json字符串中间必须有一个空行 url和conten-type之间不能有空行
增加空行后即可
相关内容:
express、koa、egg、Fastify