zoukankan      html  css  js  c++  java
  • egg 项目实战(四)Egg.js 中的请求方法

    1.GET 方法

    // 商品详情
    async detail() {
      const { ctx } = this;
      console.log(ctx.query);
      ctx.body = `id==${ctx.query.id}`;
    }
    
    async detail2() {
      const { ctx } = this;
      console.log(ctx.params);
      ctx.body = `id==${ctx.params.id}`;
    }
    
    router.get('/product/detail', controller.product.detail);
    router.get('/product/detail2/:id', controller.product.detail2);

    2.POST 方法

    需要先关闭 csrf j检查

    config/config.default.js

    config.security = {
      csrf: {
        enable: false,
      }
    };
    // 新增
    async create() {
      const { ctx } = this;
      console.log(ctx.request.body);
      const { name, weight } = ctx.request.body;
      ctx.body = {
        name,
        weight
      };
    }
    
    router.post('/product/create', controller.product.create);

    3.PUT 方法

    // 更新
    async update() {
      const { ctx } = this;
      console.log(ctx.params);
      ctx.body = {
        id: ctx.params.id
      }
    }
    
    router.put('/product/update/:id', controller.product.update);

    4.DELETE 方法

    // 删除
    async delete() {
      const { ctx } = this;
      console.log(ctx.params);
      ctx.body = {
        id: ctx.params.id
      }
    }
    
    router.delete('/product/delete/:id', controller.product.delete);

    .

  • 相关阅读:
    递归部门
    web攻击几种方法
    β版本apk下载地址及源代码github地址
    软件工程第六组U-Helpβ版使用说明
    软件工程第六组(六扇门)β版本最终答辩博客
    alpha版本展示
    用户Bug修补报告
    任务墙最终版
    个人总结-尹童欣
    个人总结-齐天浩
  • 原文地址:https://www.cnblogs.com/crazycode2/p/12421883.html
Copyright © 2011-2022 走看看