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

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

问题描述

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

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的类似模式.只需一个配置文件就可以了,因此它不必与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,并且还受以下一项支持:作为 Flatiron 框架倡议,因此应该是面向未来的.

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天全站免登陆