Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs config module optimist or nconf?

Tags:

node.js

I've just started writing my first app with NodeJS and I must say it's a pleasure learning how to work with :)

I've reached the point where I'm making some configuration before starting the server, and I would like to load the config from a config.json file.

I have found a few ways so far, either request that json file and leaver node require parse it, use a config.js file and export my config, use nconf, which seems pretty easy to use, or the last option I've seen is using optimist which I thought it would be better than ncond. Though I'm starting to think that the latter, optimist, can only be used for parsing arguments from the node cli.

So I'm asking here, can I use node optimist to get my config from a file, or, if not, should I use nconf ? Or maybe, there's something even better and lightweight out there that I don't know of ? (my options at this point are pretty vague, since I'm not sure if at some point I would like to parse any config from the cli).

like image 318
Roland Avatar asked May 31 '13 19:05

Roland


1 Answers

I use a config.js file like this:

var config = {}
config.web = {};
config.debug = {};

config.server_name =  'MyServerName';
config.web.port = process.env.WEB_PORT || 32768;

config.debug.verbositylevel = 3;

module.exports = config;

then i can just call config variables like this:

var port = config.web.port;

I find it much easier to maintain like this. Hope that helps you.

like image 174
Chris Avatar answered Sep 28 '22 10:09

Chris