本文为个人学习整理所得,希望对读者有所帮助。
GET
传递参数
在一般情况下,项目中会有两种get传参方式:1 是以?
和&
分割的、2 是以/
分割;
例如客户端向传递约定的id
和name
这两个参数:
-
localhost:7001/goods/detail?id=2333&name=hefen
-
localhost:7001/goods/detail/2333/hefen
接收参数
对应的,在服务器接收这两个参数的方式:1 query
方式、2 params
,以下是代码示例:
- 在app/controllergoods.js中,新建一个函数
detail
:
// GET
async detail() {
const {
ctx } = this;
console.log(ctx.query);
ctx.body = `hello, my id is ${
ctx.query.id} and my name is ${
ctx