API Route Records

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

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

  1. 1module.exports = app => {
  2. 2 const routes = [
  3. 3 {
  4. 4 method: 'post',
  5. 5 path: 'kitchen-sink/guide/echo9',
  6. 6 controller: 'testKitchensinkGuide',
  7. 7 action: 'echo9',
  8. 8 middlewares: 'test,transaction'
  9. 9 meta: {
  10. 10 right: {
  11. 11 type: 'resource',
  12. 12 name: 'appComponentsGuide',
  13. 13 },
  14. 14 },
  15. 15 },
  16. 16 ];
  17. 17 return routes;
  18. 18};
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

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

Across Module

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

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:

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

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