elliott.david 2011-05-03 12:09 采纳率: 25%
浏览 1054
已采纳

如何存储 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

  • 写回答

23条回答 默认 最新

  • 乱世@小熊 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.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。