使用Node.js/Heroku env变量管理应用程序设置的方法
目前,我有几个“环境”是使用NODE_ENV加载的使用Node.js/Heroku env变量管理应用程序设置的方法,node.js,environment-variables,express,Node.js,Environment Variables,Express,目前,我有几个“环境”是使用NODE_ENV加载的 (我正在使用RailwayJS/express) development.js 应用程序集('mongodb_连接_字符串','mongodb://localhost/')//示例 production.js: app.set('mongodb_connection_string',process.env.mongodab_URI) 因此,在生产中,我们使用heroku中的MONGOLAB_URI变量集 只是想知道是否有更好的方法来管理这些,而
(我正在使用RailwayJS/express) development.js
应用程序集('mongodb_连接_字符串','mongodb://localhost/')//示例 production.js:
app.set('mongodb_connection_string',process.env.mongodab_URI) 因此,在生产中,我们使用heroku中的MONGOLAB_URI变量集
只是想知道是否有更好的方法来管理这些,而不必加倍设置app.set('mongodb\u connection\u string')??我使用这个非常小的模块来实现这一点。它是用咖啡脚本写的,如果你看不懂,请告诉我,我会翻译:
extend = (source, replacement)->
for k, v of replacement
source[k] = v
source
module.exports =
create: ->
env_settings = this[process.env.NODE_ENV]
extend this.general, env_settings
这允许我编写以下设置:
settings = require 'square-settings'
settings.general =
domain: '0.0.0.0:3000'
settings.production =
domain: 'www.somewhere.com'
module.exports = settings.create()
settings.general中的所有内容都是默认设置。我可以通过添加settings.environment来覆盖每个环境。也许只需将其包装到设置这些内容并为您初始化应用程序的模块中即可。