Node之Apr框架使用文档
source link: https://lianpf.github.io/posts/%E5%BC%80%E5%8F%91%E6%97%A5%E8%AE%B0/13.apr%E6%A1%86%E6%9E%B6%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
整个团队依赖初版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
源码示例。
最后, 希望大家早日实现:成为前端高手的伟大梦想!
欢迎交流~
本文版权归原作者曜灵所有!未经允许,严禁转载!对非法转载者, 原作者保留采用法律手段追究的权利!
若需转载,请联系微信公众号:连先生有猫病,可获取作者联系方式!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK