Using NodeJS, its easy to read a configuration file using the fs
module. To read a configuration file, we need to include the fs
module, read in the configuration file and then parse it from JSON into a JavaScript variable. Given the following configuration file (configuration.json):
{
"database" : "mymongodb",
"username" : "mongouser",
"password" : "secret"
}
The configuration can be read using the following Node.js code:
var fs, configurationFile;
configurationFile = 'configuration.json';
fs = require('fs');
var configuration = JSON.parse(
fs.readFileSync(configurationFile)
);
console.log(configuration.database);
console.log(configuration.username);
console.log(configuration.password);