1.0.3 • Published 5 years ago

egg-fortress v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Eggjs项目基础框架

项目目的

  1. 抽离各个Eggjs项目中的重复代码,提高项目代码复用
  2. 项目公共依赖抽离,方便统一管理依赖,包括模块版本升级,规范插件
  3. 规范项目异常、返回结构,统一项目风格,提高项目可维护性

NPM地址:https://www.npmjs.com/package/egg-fortress

框架特点

  1. 集成 lodashmoment到helper对象, egg-corsegg-validate插件

使用方式:

// lodash
ctx.helper._  

// moment
ctx.helper.moment
  1. 扩展ctx方法,JSON数据返回使用统一的格式 

统一的格式:

{
  "code": 0,
  "data": {},
  "msg": ""
}

说明:code为0,msg为空表示正常返回,code不为0,表示请求异常,msg为异常提示

扩展的方法:

// 设置正常响应
// data: 返回的数据,不传则不返回该字段
// status: HttpStatus Code 默认为200
ctx.success(data, status);

// 设置错误响应,一般不需要手动调用,只需要在Controller、Service throw error即可
// 返回结构中 code 为 err.code 如不存在则默认 -1
// 返回结构中 msg 为 err.message 如不不存在则默认 未知异常
// 响应HttpStatus Code 为 err.status 如不存在则默认为 500
ctx.error(err);
  1. 添加配置的方式生成系统error类型

使用方式:

// config.xxx.js
// name: error名称
// code: error编码
// status: 响应HttpStatus Code
config.errors = [{
  name: 'InvalidParam',
  code: 101,
  status: 500,
}];

// Controller
throw new ctx.app.errors.InvalidParam('用户名格式不正确')

使用方式

  1. 修改package.json文件添加:
"egg": {
  "framework": "egg-fortress"
}
  1. 修改应用中的引用的基类
// Controller
const Controller = require('egg-fortress').Controller;

class MyController extends Controller {}

// Service
const Service = require('egg-fortress').Service;

class MyService extends Service {}