odejs – How to store local configuration data

How to store local configuration data.

var fs = require(‘fs’); var myOptions = { name: ‘Avian’, dessert: ‘cake’ flavor: ‘chocolate’, beverage: ‘coffee’ }; var data = JSON.stringify(myOptions); fs.writeFile(‘./config.json’, data, function (err) {if (err) { console.log(‘There has been an error saving your configuration data.’); console.log(err.message); return; } console.log(‘Configuration saved successfully.’) });

var fs = require(‘fs’); var data = fs.readFileSync(‘./config.json’), myObj; try { myObj = JSON.parse(data); console.dir(myObj); } catch (err) { console.log(‘There has been an error parsing your JSON.’) console.log(err); }

 

npm install nconf

var nconf = require(‘nconf’); nconf.use(‘file’, { file: ‘./config.json’ }); nconf.load(); nconf.set(‘name’, ‘Avian’); nconf.set(‘dessert:name’, ‘Ice Cream’); nconf.set(‘dessert:flavor’, ‘chocolate’); console.log(nconf.get(‘dessert’)); nconf.save(function (err) {if (err) { console.error(err.message); return; } console.log(‘Configuration saved successfully.’); });

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s