Wheel series: annotation and plug-in node.js Web / restful API framework

Time:2021-2-27

polixIt’s based onkoa v2.5.0OfIOC, plug-in development framework, and ordinaryNode.js Web FrameworkIn contrast, it does not need to bind the routing set, and is extensible and easy to developjavaThe famous dependency injection framework ofspringLet developers focus on logic.polixMulti service and multi process architecture is adopted to ensure the stability and rapid response of services.polixMiddleware andkoa v2.xThe middleware is compatible. DefaultORMyessequelize(it will be provided laterpolix-orm)。 Developers can choose ES6 / 7 / 8 or typescript for development.

$ npm i polix --save

Getting Started

usepolix-cliInitialize application

$ npm i polix-cli -g
$ pol init example && cd example
$ make build && make dev

Service

stayserviceAdd under folderuser.js

const { Service } = require('polix');

class UserService extends Service {
  constructor(){
    super();
    this._name = {};
  }

  async addUser(userId,name){
    this._name[userId] = name;
    return this;
  }

  async getUser(userId){
    return this._name[userId];
  }
}

module.exports = UserService;

Controller

staycontrollerAdd under folderuser.js

const { Controller, GET, POST, DEL, PUT  } = require('polix');

class UserController extends Controller {
  
  // POST /user/addUser
  @POST
  async addUser(param, ctx){
    await this.service.user.addUser(param.userId,param.name);
    ctx.body = {
      result: 'ok'
    };
  }

  // GET /user/getUser
  @GET
  async getUser(param, ctx){
    let user = await this.service.user.getUser(param.userId);
    ctx.body = {
      user
    };
  }

  // GET /user/info
  @GET('info')
  async getInfo(param, ctx){
    ctx.body = {
      v: 'v1.0'
    }
  }

  // PUT /user/updateUser
  @PUT
  async updateUser(param, ctx){
    ctx.body = {
      status: true
    }
  }

  // DEL /user/delUser
  @DEL
  async delUser(param, ctx){
    ctx.body = {
      status: true
    };
  }

  // GET /user/status/:userId
  @GET('status/:userId')
  async getStatus(param, ctx){
    ctx.body = {
      status: true,
      userId: param.userId
    };
  }

}

module.exports = UserController;

Middware

polixThe middleware of is compatible with the middleware of KOA 2. X
Frame load by defaultkoa-bodyMiddleware, if you need to add another middleware, create a new onemiddwareFolder (andcontrollerFolder level
Add cross domain middleware, newcors.js:

# cors.js

const cors = require('koa2-cors');
module.exports = function(){
  return cors({
    origin: function(ctx) {
      return '*';
    },
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    maxAge: 5,
    credentials: true,
    allowMethods: ['GET', 'POST', 'DELETE'],
    allowHeaders: ['Content-Type', 'Authorization', 'Accept']
  });
}

This folder must existindex.jsAs the total output middleware file, the middleware is bound according to the order of exported objects when loading

# index.js

const cors = require('./cors');

module.exports = {
    CORS // must be a function. The binding method is: app.use (cors())
}

Plugin

$ npm i --save polix-request

In the project root directoryconfigFolderplugin.default.jsAdd the following code to the

//'curl' will eventually mount to` this.app `Next
exports.curl = {
  //Indicates whether the plug-in is enabled
  enable: true,
  //Plug in 'NPM' package name
  package: 'polix-request'
};

staycontrollerIn usepolix-request

  @GET
  async getWebInfo(param, ctx){
    let result = await this.app.curl.get('https://www.baidu.com');
    ctx.body = {
      data: result
    }
  }

polixBuilt inpolix-requestPlug in, this is just a demo

Start

$ make dev

Address:polix.js