Skip to content
On this page

基础

获取参数

Controller 中通过 ctx 获取;三种方式:

  • ctx.query/user?id=id

  • ctx.params/user/:id

  • ctx.request.body/user 表单或 JSON 提交,在请求体中

js
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    const { id } = ctx.query;
    ctx.body = id;
  }

  async userId() {
    const { ctx } = this;
    const { id } = ctx.params;
    ctx.body = id;
  }

  async post() {
    const { ctx } = this;
    const { name } = ctx.request.body;
    ctx.body = {
      name,
    };
  }
}