API Route Records

EggBornJS automatically merges the API Route records of modules into the unified records of system

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

const testKitchensinkGuide = require('./controller/kitchen-sink/guide.js');

module.exports = app => {
  const routes = [
    { 
      method: 'post', 
      path: 'kitchen-sink/guide/echo9', 
      controller: testKitchensinkGuide,
      action: 'echo9',
      middlewares: 'test,transaction'
      meta: {
        right: {
          type: 'function',
          name: 'kitchenSink',
        },
      },
    },
  ];
  return routes;
};
Name Description
method such as get, post, etc.
path supporting the parameterized path
controller controller
action controller action. If not specified, the path trailing words will be used as action automatically
middlewares a set of middlewares can be specified. For example, middleware transaction is to enable database transaction
meta the metadata of API Route can specify middleware related parameters, such as the configuration of global middleware right: type and name, so as to verify whether the current user has access to this API Route

Perform API Route in Frontend

There are two scenarios for performing API Route in frontend: same module, across module

Scene Specification Example
same module controller/action kitchen-sink/guide/echo9
across module /{providerId}/{moduleName}/controller/action /test/party/kitchen-sink/guide/echo9

Same Module

const body = {};
this.$api.post('kitchen-sink/guide/echo9', body).then(data => {
  console.log(data);
}).catch(err => {
  console.log(err);
});

Across Module

const body = {};
this.$api.post('/test/party/kitchen-sink/guide/echo9', body).then(data => {
  console.log(data);
}).catch(err => {
  console.log(err);
});

Perform API Route in Backend

Not only can perform API Route in frontend, but also in backend

There are also two scenarios for performing API Route in frontend: same module, across module

Here, take across module as an example:

const body = {};
const data = await this.ctx.performAction({
  method: 'post',
  url: '/test/party/kitchen-sink/guide/echo9',
  body,
});

ctx.performAction

Perform API Route in backend

Name Description
method such as get, post, etc.
url path of API Route
body parameters of method post

ctx.performActionInBackground

Perform API Route as background mode in backend

Name Description
method such as get, post, etc.
url path of API Route
body parameters of method post

For specific application scenarios, please refer to: CabloyJS:Progress Bar