在sails.js 中处理生产/开发/测试配置 [英] Handling production/dev/testing configs in sails.js
问题描述
有谁知道在sails 中处理prod/dev/test 配置切换的最佳方法吗?我真的很喜欢 actionhero.js 如何根据 NODE_ENV 的内容自动加载 config/environment/{env}.js 但我没有看到在帆中做类似事情的内置方法.我注意到在服务器引导期间,sails 会加载 config/中的任何文件,所以我现在的hacky 解决方案是设置以下内容:
config/|-- 本地.js|-- 环境/|---- production.js|---- 暂存.js|---- 开发.js|---- 测试.js
然后在每个 {env}.js 文件中,我只是像这样扩展配置:
if (process.env.NODE_ENV === '生产') {//输入任何特定于环境的配置更改配置.db = {db_host: foo,db_port: 酒吧}config.otherThing = {somevar: 'someval'}
自从 Sails 0.10-rc6 以来,这得到了改进,您现在可以在 config
中添加一个 env
子文件夹到更改不同环境的设置.
所以你可以简单地添加一个文件 /config/env/development.js
或 /config/env/production.js
来覆盖所有必要的设置>
参见 https://github.com/balderdashy/sails/pull/1638了解更多详情.
更改端口和数据库适配器的示例,例如对于 production.js
中的 production
环境:
module.exports = {端口:80};模块.exports.models = {//您的应用程序的默认连接.//即您的应用程序的连接之一的名称(请参阅`config/connections.js`)////(默认为localDiskDb)连接:'someMongodbServer'};
Does anyone know the best way to handle prod/dev/test config switching in sails? I really like how actionhero.js automatically loads config/environment/{env}.js based on the contents of NODE_ENV but I don't see a built-in way to do something similar in sails. I noticed that sails will load any file in config/ during server bootstrap so my hacky solution for now is to setup the following:
config/
|-- local.js
|-- environment/
|---- production.js
|---- staging.js
|---- development.js
|---- testing.js
Then in each {env}.js file, I just extend config like this:
if (process.env.NODE_ENV === 'production') {
// Enter any environment specific config changes
config.db = {
db_host: foo,
db_port: bar
}
config.otherThing = {
somevar: 'someval'
}
Since Sails 0.10-rc6 this got improved and you can now add an env
subfolder in config
to change settings for different environments.
So you could simply add a file /config/env/development.js
or /config/env/production.js
that can override all the necessary settings
See https://github.com/balderdashy/sails/pull/1638 for more details.
Example to change the port and database adapter, e.g. for production
environment in your production.js
:
module.exports = {
port: 80
};
module.exports.models = {
// Your app's default connection.
// i.e. the name of one of your app's connections (see `config/connections.js`)
//
// (defaults to localDiskDb)
connection: 'someMongodbServer'
};
这篇关于在sails.js 中处理生产/开发/测试配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!