Configuration file(conf.json):
{
"mongodb": {
"host": "127.0.0.1",
"port": "3000",
"db": "myproject"
}
}
usage in .js file
var conf = require('./conf.json')
var host = conf.mongodb.host;
var port = conf.mongodb.port;
var db = conf.mongodb.db;
var url = "mongodb://"+host+":"+port+"/"+db;
After this I'm establising connection to the mongodb using the url variable.
What I want to achieve is before trying to connect to mongodb it should validate all the variables that are being set from the conf.json file. If they are undefined or something, the control shoud go back and not try and attempt the following code.
Note: I've thought of putting a check for undefined on this variables using an if condition. But I'm looking for better solution.
I've heard "||" operator of javascript can help, not sure how though?