定义Service

The definition of controller is the same as EggJS Service

src/suite-vendor/test-party/modules/test-party/backend/src/service/version.js

module.exports = app => {

  class Version extends app.Service {

    async update(options) {
      ...
    }
    ...
  }

  return Version;
};

Reference Service

Different from EggJS: EggJS specifies the file location of Service, which is loaded automatically when system start. While the Service of EggBornJS needs to be explicitly referenced by require, so as to facilitate module compilation

src/suite-vendor/test-party/modules/test-party/backend/src/services.js

const version = require('./service/version.js');
const party = require('./service/party.js');

module.exports = app => {
  const services = {
    version,
    party,
  };
  return services;
};

Usage of Service

EggBornJS Service only can be performed in the same module

When the backend receive access from the frontend, the system automatically creates a object ctx, and injects all Services of the current module into the object ctx

Just because of this module isolation mechanism, we don’t have to worry about whether there is a service with the same name in other modules in the process of large-scale business development

For Example:

src/suite-vendor/test-party/modules/test-party/backend/src/controller/version.js

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

If there is a similar demand for across module performing, please refer to the section: API Route