谁还没个明天 2011-05-03 12:09 采纳率: 50%
浏览 533
已采纳

如何存储 Node.js 部署设置 / 配置文件?

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.

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?

转载于:https://stackoverflow.com/questions/5869216/how-to-store-node-js-deployment-settings-configuration-files

  • 写回答

22条回答 默认 最新

  • larry*wei 2011-12-19 15:28
    关注

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

    A simple example:

    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'));
    

    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.

    Check out nconf at Github.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(21条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大