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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With