基于Vue2和Node.js的反欺诈系统设计与实现 (10)

eggjs提供了根据开发、生产、测试环境的配置文件,具体的以config.env.js表示,因为项目不是很复杂,而且都是我一个人写的,这里就简单点都写在了一个文件config.default.js里面。

在这里面可以对中间件、安全、数据库、日志、文件上传、session、loader等进行配置,具体的如下:

/* eslint valid-jsdoc: "off" */ 'use strict'; /** * @param {Egg.EggAppInfo} appInfo app info */ module.exports = appInfo => { /** * built-in config * @type {Egg.EggAppConfig} * */ const config = (exports = {}); // use for cookie sign key, should change to your own and keep security config.keys = `${appInfo.name}_ataola`; // add your middleware config here config.middleware = ['cost', 'errorHandler']; // add your user config here const userConfig = { myAppName: 'egg', }; config.security = { xframe: { enable: true, }, csrf: { enable: true, ignore: '/user/login', // queryName: '_csrf', // bodyName: '_csrf', headerName: 'x-csrf-token', }, domainWhiteList: [ 'http://localhost:7001', 'http://127.0.0.1:7001', 'http://localhost:9528', 'http://localhost', 'http://127.0.0.1', ], }; // https://github.com/eggjs/egg-sequelize config.sequelize = { dialect: 'mysql', // support: mysql, mariadb, postgres, mssql database: 'anti-fraud', host: 'hzga-mysql', port: 3306, username: 'root', password: 'ataola', // delegate: 'myModel', // load all models to `app[delegate]` and `ctx[delegate]`, default to `model` // baseDir: 'my_model', // load all files in `app/${baseDir}` as models, default to `model` // exclude: 'index.js', // ignore `app/${baseDir}/index.js` when load models, support glob and array // more sequelize options define: { timestamps: false, underscored: false, }, }; exports.multipart = { mode: 'file', fileSize: '100mb', whitelist: [ // images '.jpg', '.jpeg', // image/jpeg '.png', // image/png, image/x-png '.gif', // image/gif '.bmp', // image/bmp '.wbmp', // image/vnd.wap.wbmp '.webp', '.tif', '.psd', // text '.svg', '.js', '.jsx', '.json', '.css', '.less', '.html', '.htm', '.xml', '.xlsx', '.xls', '.doc', '.docx', '.ppt', '.pptx', '.pdf', // tar '.zip', '.rar', '.gz', '.tgz', '.gzip', // video '.mp3', '.mp4', '.avi', ], }; config.session = { key: 'SESSION_ID', // 设置session key,cookie里面的key maxAge: 24 * 3600 * 1000, // 1 天 httpOnly: true, // 是否允许js访问session,默认为true,表示不允许js访问 encrypt: true, // 是否加密 renew: true, // 重置session的过期时间,延长session过期时间 }; config.logger = { level: 'NONE', consoleLevel: 'DEBUG', disableConsoleAfterReady: false, }; config.errorHandler = { match: 'http://www.likecs.com/', }; config.customLoader = { enum: { directory: 'app/enum', inject: 'app', loadunit: true, }, utils: { directory: 'app/utils', inject: 'app', loadunit: true, }, }; config.cluster = { listen: { path: '', port: 7001, hostname: '0.0.0.0', }, }; return { ...config, ...userConfig, }; };

eggjs中的配置:https://eggjs.org/zh-cn/basics/config.html

eggjs中的插件

这里主要是针对一些egg集成的插件进行配置,比如sequelize, cors等等

plugin.js具体的如下:

'use strict'; /** @type Egg.EggPlugin */ module.exports = { // had enabled by egg static: { enable: true, }, sequelize: { enable: true, package: 'egg-sequelize', }, cors: { enable: true, package: 'egg-cors', }, validate: { enable: true, package: 'egg-validate', }, };

eggjs中的插件: https://eggjs.org/zh-cn/basics/plugin.html

eggjs中的扩展extend

在app文件夹下新建extend文件夹,它可以对egg的agent,application,context,helper,request,response,validator内置对象进行扩展。

这里以context.js为例,我想封装一下上下文返回的格式,可以这么写:

'use strict'; module.exports = { success(data, message = 'success') { const res = { status: 200, message, data, }; this.app.logger.info(JSON.stringify(res)); this.body = res; }, };

调用的时候ctx.success(data)。

eggjs中的扩展:https://eggjs.org/zh-cn/basics/extend.html

eggjs中的中间件

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzfxzj.html