如何存储 Node.js 部署设置/配置文件? [英] How to store Node.js deployment settings/configuration files?

查看:24
本文介绍了如何存储 Node.js 部署设置/配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一些 Node 应用程序,并且一直在寻找一种存储部署相关设置的良好模式.在 Django 世界(我来自哪里)中,通常的做法是拥有一个包含标准设置(时区等)的 settings.py 文件,然后是一个 local_settings.py 用于部署特定设置,即.与什么数据库对话,什么内存缓存套接字,管理员的电子邮件地址等等.

I have been working on a few Node apps, and I've been looking for a good pattern of storing deployment-related settings. In the Django world (where I come from), the common practise would be to have a settings.py file containing the standard settings (timezone, etc), and then a local_settings.py for deployment specific settings, ie. what database to talk to, what memcache socket, e-mail address for the admins and so on.

我一直在为 Node.js 寻找类似的模式.只需要一个配置文件就好了,所以它不必与 app.js 中的其他所有内容一起塞进来,但我发现有一种方法可以在文件中包含特定于服务器的配置很重要这不在源代码管理中.同一个应用程序很可能部署在具有截然不同设置的不同服务器上,并且必须处理合并冲突以及所有这些并不是我的乐趣所在.

I have been looking for similar patterns for Node. Just a config file would be nice, so it does not have to be jammed in with everything else in app.js, but I find it important to have a way to have server-specific configuration in a file that is not in source control. The same app could well be deployed across different servers with wildly different settings, and having to deal with merge conflicts and all that is not my idea of fun.

那么是否有某种框架/工具可以解决这个问题,还是每个人都自己一起破解一些东西?

So is there some kind of framework/tool for this, or does everyone just hack something together themselves?

推荐答案

很久以后,我发现了一个非常好的 Node.js 管理配置模块:nconf.

Much later, I found a pretty good Node.js module for managing configuration: nconf.

一个简单的例子:

var nconf = require('nconf');

// First consider commandline arguments and environment variables, respectively.
nconf.argv().env();

// Then load configuration from a designated file.
nconf.file({ file: 'config.json' });

// Provide default values for settings not provided above.
nconf.defaults({
    'http': {
        'port': 1337
    }
});

// Once this is in place, you can just use nconf.get to get your settings.
// So this would configure `myApp` to listen on port 1337 if the port
// has not been overridden by any of the three configuration inputs
// mentioned above.
myApp.listen(nconf.get('http:port'));

它还支持在 Redis 中存储设置,编写配置文件,并且具有相当可靠的 API,并且还得到了其中之一的支持备受推崇的 Node.js 商店 Nodejitsu,作为 Flatiron<的一部分/a> 框架倡议,所以它应该是相当面向未来的.

It also supports storing settings in Redis, writing configuration files, and has a fairly solid API, and is also backed by one of the more well-respected Node.js shops, Nodejitsu, as part of the Flatiron framework initiative, so it should be fairly future-proof.

在 Github 上查看 nconf.

Check out nconf at Github.

这篇关于如何存储 Node.js 部署设置/配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆