谁还没个明天 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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分