整个团队依赖初版Apr作为统一的Node方案,随着业务场景越来越丰富,一些插件、middle等公共配置成为最佳实践后,逐渐下沉到Apr框架中,让Apr越来越…
一、快速开始
1. create demo & init
$ mkdir apr-example && cd apr-example
$ npm init
2. 安装使用 @lianpf/apr
apr 涉及到公司保密级别影响,暂不能对外开放。故此处安装一个apr外部简易版。
Tips:
内部会使用脚手架快速搭建,此处仅演示手动创建各文件流程。
$ npm i @lianpf/apr -S
$ npm i egg-bin egg-mock -D
3. 修改package.json 文件
修改scripts
"scripts": {
  "dev": "egg-bin dev",
  "test": "egg-bin test"
},
增加"egg"
"egg": {
  "framework": "@lianpf/apr"
}
最终package.json版本:
{
  "name": "apr-example",
  "version": "1.0.0",
  "dependencies": {
    "@lianpf/apr": "^1.0.0"
  },
  "devDependencies": {
    "egg-bin": "^4.3.5",
    "egg-mock": "^3.13.1"
  },
  "scripts": {
    "dev": "egg-bin dev",
    "test": "egg-bin test"
  },
  "egg": {
    "framework": "@lianpf/apr"
  }
}
4. 搭建: controller、service、configs、router.js、views
参考以下结构,搭建文件
apr-example
├── app
│   ├── controller
│   │   └── home.js
│   ├── public
│   ├── router.js
│   ├── service
│   │   └── code.js
│   └── view
│       └── home.tpl
├── config
│   ├── config.default.js
│   └── plugin.js
├── logs
│   └── apr-example
│       ├── apr-example-web.log
│       ├── common-error.log
│       ├── egg-agent.log
│       ├── egg-schedule.log
│       └── egg-web.log
├── package-lock.json
├── package.json
├── run
│   ├── agent_config.json
│   ├── agent_config_meta.json
│   ├── agent_timing_57111.json
│   ├── application_config.json
│   ├── application_config_meta.json
│   ├── application_timing_57112.json
│   └── router.json
└── test
    └── app
        └── controller
            └── home.test.js
(1) router.js
app/router.js 示例:
use strict';
module.exports = app => {
  app.router.get('/', app.controller.home.index);
};
(2) controller
app/controller/home.js 示例:
'use strict';
// 引入框架
const Controller = require('@lianpf/apr').Controller;
class HomeController extends Controller {
  async index() {
    const ctx = this.ctx;
    // use service defined in framework
    const data = await ctx.service.test.get(123);
    const result = await ctx.service.code.storageCustomCode();
    await ctx.render('home.tpl', Object.assign({}, data, result));
  }
}
module.exports = HomeController;
(3) service
app/service/code.js 示例:
const Service = require('@lianpf/apr').Service;
class CodeService extends Service {
  async storageCustomCode() {
    // 连接db获取用户数据
    const result = await this.app.mysql.get('custom_code', { id: 11 });
    // console.log('--storageCustomCode-result--', result)
    return result;
  }
}
module.exports = CodeService;
(4) 其他文件
- views
 - public
 - configs
 - …
 
请直接参考demo 源码,欢迎 star。
二、API介绍及示例
1. mysql
- 配置
app/config/config.default.js - 配置
app/config/plugin.js app/service/code.js使用
app/config/config.default.js 配置mysql信息
/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};
  // 用于cookie签名密钥,应更改为你自己的 Cookie 安全字符串
  config.keys = appInfo.name + '_1617081780536_9800';
  
  config.mysql = {
    // 单数据库信息配置
    client: {
      // host
      host: '127.0.0.1',
      // 端口号
      port: '3306',
      // 用户名
      user: 'root',
      // 密码
      password: '12345678',
      // 数据库名
      database: 'localTestDB',
    },
    // 是否加载到 app 上,默认开启
    app: true,
    // 是否加载到 agent 上,默认关闭
    agent: false,
  };
  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };
  return {
    ...config,
    ...userConfig,
  };
};
app/config/plugin.js 开启mysql插件
module.exports = {
  // 开启插件
  mysql: true,
  nunjucks: true,
}
使用详见 app/service/code.js 源码示例。
2. nunjucks
- 配置
app/config/plugin.js app/controller/home.js使用
app/config/plugin.js 开启nunjucks插件
module.exports = {
  // 开启插件
  mysql: true,
  nunjucks: true,
}
使用详见 app/controller/home.js 源码示例。
3. 其他
待补充…
最后, 希望大家早日实现:成为编程高手的伟大梦想!
欢迎交流~

本文版权归原作者曜灵所有!未经允许,严禁转载!对非法转载者, 原作者保留采用法律手段追究的权利!
若需转载,请联系微信公众号:连先生有猫病,可获取作者联系方式!