Egg Learning Notes (2) - Runtime Environment and Configuration
Runtime Environment
A Web application itself should be stateless and have the ability to set itself according to the runtime environment.
Original document address: Runtime Environment
Specifying the Runtime Environment
It is specified through the config/env file. The content of this file is the runtime environment, such as prod.
// config/env
prodGetting the Runtime Environment Within the Application
The framework provides the variable app.config.env to represent the application's current runtime environment.
Custom Environments
A conventional development process may have more than just the environments mentioned above. Egg supports custom environments to adapt to your own development workflows.
For example, to add an integration testing environment SIT to the development workflow. Set EGG_SERVER_ENV to sit (and it is recommended to set NODE_ENV = production). Upon startup, config/config.sit.js will be loaded, and the runtime environment variable app.config.env will be set to sit.
Config Configuration
The framework provides powerful and extendable configuration capabilities. It can automatically merge configurations from the application, plugins, and the framework, overwriting them sequentially, and can maintain different configurations depending on the environment. The merged configuration can be directly obtained from app.config.
Multi-environment Configuration
The framework supports loading configurations based on the environment and defining configuration files for multiple environments. For specific environments, please refer to Runtime Environment Configuration.
config
|- config.default.js
|- config.prod.js
|- config.unittest.js
`- config.local.jsconfig.default.js is the default configuration file, and all environments will load this configuration file. It generally also serves as the default configuration file for the development environment.
When an env is specified, both the default configuration and the corresponding named configuration file will be loaded. The named configuration and the default configuration will be merged (using extend2 deep clone) into the final configuration. Named configuration items will overwrite items with the same name in the default configuration file. For instance, the prod environment will load both config.prod.js and config.default.js files, and config.prod.js will overwrite the corresponding configurations in config.default.js.
Configuration Syntax
The configuration file returns an object. It can overwrite some framework configurations, and applications can also place their own business configurations here for convenient management.
// Configure the directory for logger files; the default logger configuration is provided by the framework
module.exports = {
logger: {
dir: "/home/admin/logs/demoapp",
},
};The configuration file can also be simplified into the exports.key = value format.
exports.keys = "my-cookie-secret-key";
exports.logger = {
level: "DEBUG",
};The configuration file can also return a function, which can accept appInfo as an argument.
// Place the logger directory under the code directory
const path = require("path");
module.exports = (appInfo) => {
return {
logger: {
dir: path.join(appInfo.baseDir, "logs"),
},
};
};AI Translation | AI 翻译
This article was translated from Chinese to English by AI. If there are any inaccuracies, please refer to the original Chinese version.
本文由 AI 辅助从中文翻译为英文。如遇不准确之处,请以中文原版为准。
