Controller

2018-11-22 14:47:54

定义Controller

Controller的定义方式与EggJS一致

src/module/test-todo/backend/src/controller/version.js

module.exports = app => {
  class VersionController extends app.Controller {

    async update() {
      await this.service.version.update(this.ctx.request.body);
      this.ctx.success();
    }

  }
  return VersionController;
};

引用Controller

Controller直接在API路由中引用

src/module/test-todo/backend/src/routes.js

const version = require('./controller/version.js');

module.exports = app => {
  const routes = [
    // version
    { method: 'post', path: 'version/update', controller: version, middlewares: 'inner' },
    ...
  ];
  return routes;
};


评论: