Node.js:如何为 prod 和 staging 设置不同的变量
我正在使用 Express,并且需要为每个服务器(登台和生产)使用不同的凭据。
我可以在 server.coffee 文件中设置变量,但随后我需要访问不同文件中的这些变量。
server.coffee:
app.configure 'production', () ->
app.use express.errorHandler()
解决办法是什么?设置变量然后导出它们?
I'm using Express and I need to use different credentials for each server (staging and production).
I could setup the variables in the server.coffee file but then I'd need to access those variables in different files.
server.coffee:
app.configure 'production', () ->
app.use express.errorHandler()
What's the solution? Setup the variables and then export them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已将实现上传到 https://github.com/qiangyu/nodejsconfig。我相信它会满足您的需求。基本上,您只需要提供一个配置文件:
然后,您可以使用以下代码在生产环境中读取appAddress:
I have uploaded an implementation into https://github.com/qiangyu/nodejsconfig. I believe it will satisfy your needs. Basically, you only need to provide one configuration file:
Then, you use following code to read the appAddress in prod environments:
如果您不想要确定每个文件中使用哪个配置的逻辑(这看起来相当难看),则必须将其导出到某个地方。
我的建议是:有一个包含不同配置的 config.json 文件。主文件需要它并执行类似
config.default = config.(condition ? 'product':'development')
的操作。在所有其他文件中,您现在只需执行require('./config').default
即可。If you don't want the logic for determining which config to use in each file (which would look pretty ugly), you'll have to export it somewhere.
What I would suggest: Have a
config.json
file containing the different configs. The main file requires it and does something likeconfig.default = config.(condition ? 'production':'development')
. In all other files, you can now just dorequire('./config').default
.我有一个应用程序,它使用三种不同的方法来声明配置变量(uri、api 密钥、凭据等),具体取决于环境(生产 = 环境变量;暂存 = 命令行参数;本地 = 配置文件。)
我编写了一个小“配置”模块来处理将所有这些选项合并到一个可以在我的应用程序中使用的对象,并将其作为要点上传: https://gist.github.com/1616583
它可能不是最好的实现,但到目前为止它运行得很好:)。
I have an app which uses three different methods of declaring config variables (uris, api keys, credentials, etc.) depending upon the environment (production = environment variables; staging = command line args; local = config files.)
I wrote a little "config" module to handle merging all of these options into one object that I can use in my app and uploaded it as a gist: https://gist.github.com/1616583
It might not be the best implementation, but it's been working pretty well so far :).
./config.js
./app.js
./config.js
./app.js
这可能是使用 npm-config 的好地方。
我不会将它们用于每种类型的变量配置设置,但我认为对于 URL 和端口等简单情况来说这是一个很好的解决方案,因为:
需要注意的是,package.json 中的
config
参数仅在您通过 npm 运行代码时自动导出到环境中。因此,如果您只是使用 Node 运行它,例如node ./myapp.js
,那么您不能指望process.env.npm_package_config_foo
将包含您的值。但是,您始终可以var pkg = require('./package.json');
并访问pkg.config
中的值。因为它可能不是立即显而易见的,我还要补充一点,
npm_package_config
环境变量不会冒泡到依赖于 npm 包的应用程序。因此,如果您的依赖包引用process.env.npm_package_config_foo
,那么依赖包必须在自己的 package.json 中定义它。我想因为它是一个“npm_package_config”,所以将它们一直推到树上是没有意义的。那么,我将如何使用一个 npm 配置变量并让它在基础包和依赖它的包中一直沿树工作?这实际上有点令人困惑,我必须通过反复试验来解决这个问题。
假设您有一个包 connector 和包 client。 客户端依赖于连接器,并且您希望为连接器指定一个可以在客户端中使用或覆盖的配置参数。如果您在 connector 模块中使用
process.env.npm_package_config.port
,那么当它在 client 模块中依赖时,该变量将不会起作用不会被导出,最终会变成未定义。但是,如果您改用
process.env.npm_config_connector_port
(请注意第一个以 npm_package_config 开头,另一个以 npm_config_packagename 开头),那么您可以至少使用npm config set Connector:port 80
在 .npmrc 中设置它,它将被“命名空间”为process.env.npm__config_connector_port
在您运行 npm 的任何地方,包括您在依赖于连接器的客户端中运行的脚本,并且您将始终能够在命令行、ENV 或 .npmrc 中覆盖它。您只需记住,与任何环境变量一样,它可能并不总是被设置。因此,我将使用默认运算符,并将 process.env.npm_config_connector_port 作为第一个(首选)值:var port = process.env.npm_config_connector_port || sane_default
这里,sane_default 可以从其他推荐的方法之一填充。就我个人而言,我喜欢至少将这些配置数据保留在 JSON 文件中,而 package.json 似乎是放置它们的最佳 JSON 文件。将它们存储在数据而不是代码中,然后您可以轻松使用静态 JSON 内联、动态生成它们或从文件系统、URL 或数据库中提取它们。
This might be a good place to use npm-config.
I would not use them for every type of variable configuration setting, but I think it's a good solution for simple cases like URLs and ports because:
The one caveat is that the
config
parameter in your package.json is only automatically exported into the environment when you run your code through npm. So, if you just run it with node, like,node ./myapp.js
, then you can't expect thatprocess.env.npm_package_config_foo
will contain your value. However, you can alwaysvar pkg = require('./package.json');
and access the values atpkg.config
.Because it might not be immediately obvious, I'd also add that the
npm_package_config
environment variables do not bubble up to apps that depend on your npm package. So, if your depended-on package refers toprocess.env.npm_package_config_foo
, then the dependent package would have to define that in its own package.json. I guess since it's an "npm_package_config" it wouldn't make sense to push them all the way up the tree.So, how would I use one npm config variable and have it work all the way up the tree, in both the base package and the packages that depend on it? It's actually a little confusing, and I had to figure this out through trial and error.
Let's say you have a package connector and package client. client depends on connector and you want to specify a config parameter for connector that can be used or overwritten in client. If you use
process.env.npm_package_config.port
in your connector module, then when it's depended on in client module, then that variable won't be exported and it will end up as undefined.However, if you instead use
process.env.npm_config_connector_port
(notice the first one starts with npm_package_config and the other with npm_config_packagename), then you can at least set that in your .npmrc usingnpm config set connector:port 80
and it will be "namespaced" asprocess.env.npm__config_connector_port
everywhere that you run npm, including scripts that you run in client that depend on connector, and you'll always be able to overwrite it on the command line, in your ENV, or in your .npmrc. You just have to keep in mind that, as with any environment variable, it may not always be set. So, I would use the default operator with theprocess.env.npm_config_connector_port
as the first (preferred) value:var port = process.env.npm_config_connector_port || sane_default
Here, sane_default could be populated from one of the other recommended methods. Personally, I like keeping configuration data like these in JSON files at the very least, and package.json seems like the best JSON file to put them in. Store them in data instead of code and then you can easily use the static JSON in-line, generate them dynamically, or pull them from the filesystem, URLs or databases.