In our application, there will always exist common config items in different files. You may break the atomicity and consistency of the data if you occationally modified one file while others remain unchanged. Also, if you want to correctlly change one config item, you may search every file using that item. So keep all these configurations in one single file will save you a lot of time. This article is mainly focused on how to solve this problem in the front end field.
var _ = require('lodash')
var default_config = require('./default')
module.exports = (function () {
var version = (process.env.DEPLOY_VERSION || 'dev').toLowerCase()
var config = _.extend({ version: version }, default_config)
return config
})()
Finally, you could import you module
var config=require('./config')
On your server, use bellow directive to set the process.env.whatever
# windows
set DEPLOY_VERSION = "production"
# unix
export DEPLOY_VERSION="production"
In our application, there will always exist common config items in different files. You may break the atomicity and consistency of the data if you occationally modified one file while others remain unchanged. Also, if you want to correctlly change one config item, you may search every file using that item. So keep all these configurations in one single file will save you a lot of time. This article is mainly focused on how to solve this problem in the front end field.
React with webpack
Learnt from Petr Bela on ques How to store Configuration file and read it using React. With webpack you can put env-specific config into the
externals
field inwebpack.config.js
If you want to store the configs in a separate JSON file, that's possible too, you can require that file and assign to
Config
:Then in your modules, you can use the config:
Node.js Application
Learnt from node.js 下定制你的 config 配置文件
Create two file in config directory:
In
default.json
, store all you config dataIn
index.js
, export a config module:Finally, you could import you module
On your server, use bellow directive to set the process.env.whatever