eggjs连接多个oracle,Egg.js 配置多环境

一、默认配置

config.default.js 为默认的配置文件,所有环境都会加载这个配置文件,不过其他环境的配置的会覆盖默认配置的同名配置。

/* eslint valid-jsdoc: "off" */

'use strict';

/**

* @param {Egg.EggAppInfo} appInfo app info

*/

module.exports = appInfo => {

/**

* built-in config

* @type {Egg.EggAppConfig}

**/

const config = exports = {};

// 关闭 csrf,默认开启

config.security = {

csrf: {

enable: false,

},

};

// use for cookie sign key, should change to your own and keep security

config.keys = appInfo.name + '_1599042231246_4485';

// add your middleware config here

config.middleware = [];

// add your user config here

const userConfig = {

// myAppName: 'egg',

};

return {

...config,

...userConfig,

};

};

二、调试环境

本地启动 egg-bin dev 的应用是以 env: local 启动的,读取的配置也是 config.default.js 和 config.local.js 合并的结果。

对本地的定制化需要在 config 文件夹下面新建 config.local.js。

三、测试环境

egg-scripts NPM 主页:egg-scripts

env - server env, default to process.env.EGG_SERVER_ENV

env 指向的值便是 config.${that}.js 中的值,测试环境的部署只需要添加脚本:

"start-test": "egg-scripts start --env=test --daemon --title=egg-server-test",

"stop-test": "egg-scripts stop --env=test --title=egg-server-test",

四、生产环境

生产环境只需要修改 env 的值:

"start-prod": "egg-scripts start --env=prod --daemon --title=egg-server-prod",

"stop-prod": "egg-scripts stop --env=prod --title=egg-server-prod",

五、文件结构

└── config

├── config.default.js

├── config.local.js

├── config.prod.js

└── config.test.js

六、使用配置

以 Controller 层为例,配置文件会注册在上下文 ctx 中。

class MazeyController extends Controller {

async say() {

const { ctx } = this;

console.log(ctx.app.config.attr);

ctx.body = 'hi, Mazey.';

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值