egg-common-modules
Introduction
It includes middlewares, utils, basis for service/controller, etc.
Middlewares:
- errorHandler: It will handle all errors thrown from the application and convert to a common http response.
- retrieveCookie: It will retrieve the cookie from the request header and set it to field - cookie.
e.g.const cookie = ctx.request.cookie;
Utils:
- sign: It includes md5 and suffix function.
- decorator function for egg-mysql transaction mode.
Exception:
This a common error type.Error { code: 1, msg: "通用错误" }
Basis:
- BasicService: It is a basic service for the application.
- BasicController: It is a basic controller for the application.
Install
$ npm i egg-common-modules --save
Usage
// {app_root}/config/plugin.jsexportscommonModules = enable: true package: 'egg-common-modules';
Configuration
// {app_root}/config/config.default.jsexportscommonModules = {};
see config/config.default.js for more detail.
Example
// {app_root}/config/config.default.jsexportscommonModules = {}; // {app_root}/app/controller/mainController.jsconst BasicController = basis; async { const ctx = this; const result = await ctxservicemainService; this; } moduleexports = mainController; // {app_root}/app/service/mainService.jsconst BasicService = basis;const Exception = exception; async { const result = await this; return result; } @transaction // this will make the database operations be in transaction mode async { const ctx = this; if !ctxuserid throw code: 1 msg: '未登录' ; const user = await ctxserviceuserService; if userisPrime const rn = Math; if rn > 05 return true; return false; } moduleexports = mainService;
Questions & Suggestions
Please open an issue here.
Author
Chris