How to use nconf

Comprehensive nconf code examples:

How to use nconf.remove:

54
55
56
57
58
59
60
61
62
63
    args = args[0]; 
  }
  return nconf.any(args.map(d => encrypt(d)));
},
remove: d => {
  nconf.remove(encrypt(d));
  return module.exports;
},
required: d => {
  nconf.required(encrypt(d));

How to use nconf.setEncryptionKey:

129
130
131
132
133
134
135
136
137
138
  }
}
function validateEncryptionKey(d) {
  const key = d || ENCRYPTION_KEY;
  if (typeof key === 'undefined') {
    throw new Error('Encryption Key must be set - nconf.setEncryptionKey()');
  } else if (key.length !== 32) {
    throw new Error('Encryption Key must be 256 bits (32 characters)');
  }
}

How to use nconf.clear:

66
67
68
69
70
71
72
73
74
75
  reset: () => {
    Object.keys(nconf.stores).forEach(store => { delete nconf.stores[store]; });
    return module.exports;
  },
  clear: key => {
    nconf.clear(key);
    return module.exports;
  },
//  save
//  load

How to use nconf.use:

28
29
30
31
32
33
34
35
36
37
  if (options.type === 'literal') {
    store = options.store;
  } else if (options.type === 'file') {
    store = JSON.parse(fs.readFileSync(options.file)); 
  }
  nconf.use(encrypt(name), { type: 'literal', store: encrypt(store) });
  return module.exports;
},
add: (name, options) => module.exports.use(name, options),
file: (name, file) => {

How to use nconf.required:

42
43
44
45
46
47
48
49
50
51
    return nconf.get();
}

// Get single item, return value as is
if (typeof keys === 'string') {
    nconf.required([keys]);
    return nconf.get(keys);
}

// Multiple keys provided, return object

How to use nconf.overrides:

134
135
136
137
138
139
140
141
142
143
                config.redis.password = process.env.REDIS_PASSWORD;
        }
}

// Set overrides from container environment
nconf.overrides(config);

// Cleanup
config = null;
testSSL = null;

How to use nconf.load:

54
55
56
57
58
59
60
61
62
63
    whitelist: this.whitelist,
  })

  nconf.defaults(this.defaults)

  nconf.load()
  logger(`config ready: ${this.basePath}/${file_name}`)
  this.started = true
  return Promise.resolve(this)
}

How to use nconf.formats:

190
191
192
193
194
195
196
197
198
199
200
 * Returns the settings for parsing a configuration file.
 * @param {string} filename The path of the configuration file.
 * @return {{file: string, logicalSeparator: string, format: object}} The parsing options.
 */
function getFileParsingOptions(filename) {
    return { file: filename, logicalSeparator: '-', format: nconf.formats.yaml };
}


/**
 * Creates an absolute path from the provided relative path if necessary.

How to use nconf.env:

14
15
16
17
18
19
20
21
22
23
  ENCRYPTION_KEY = d;
  return module.exports;
},
get: key => key ? get(key) : nconf.get(),
env: options => {
  nconf.env(Object.assign({ transform }, options));
  return module.exports;
},
argv: options => {
  nconf.argv(Object.assign({ transform }, options));

How to use nconf.argv:

17
18
19
20
21
22
23
24
25
26
 * @return {Hash|Boolean} Returns config or false if missing
 * @api public
 */
loadConfig: function( defaultConfig ) {

  return nconf.argv()
          .overrides( defaultConfig )
          .defaults(require('../config.json'))
          .env()
          .file({ file: process.cwd() + '/config.json' });

How to use nconf.save:

66
67
68
69
70
71
72
73
74
75
logger('clear')
return Promise((resolve,reject)=>{
  logger('clearing')
  nconf.set(null, {})

  nconf.save((err)=>{
    if(err){ return reject(err) }

    return resolve()
  })

How to use nconf.any:

51
52
53
54
55
56
57
58
59
60
any: function() {
  let args = Array.from(arguments);
  if (Array.isArray(args[0])) {
    args = args[0]; 
  }
  return nconf.any(args.map(d => encrypt(d)));
},
remove: d => {
  nconf.remove(encrypt(d));
  return module.exports;

How to use nconf.defaults:

39
40
41
42
43
44
45
46
47
48
    file = name;
  }
  return module.exports.use(name, { type: 'file', file });
},
defaults: d => {
  nconf.defaults(encrypt(d));
  return module.exports;
},
overrides: d => {
  nconf.overrides(encrypt(d));

How to use nconf.Provider:

14
15
16
17
18
19
20
21
22
23
debug('config start');
options = options || {};

const baseConfigPath = options.baseConfigPath || __dirname;
const customConfigPath = options.customConfigPath || process.cwd();
const nconf = new Nconf.Provider();

// ## Load Config

// no channel can override the overrides

How to use nconf.file:

19
20
21
22
23
24
25
26
27
28
const allKeys = serverConfigKeys.concat(templateConfigKeys, clientConfigKeys);
nconf.env({whitelist: allKeys, parseValues: true});

// Read config_dev.json if in development-mode. Will not overwrite configs from env.
if (process.env.NODE_ENV === 'development') {
    nconf.file({file: path.join(paths.ROOT, 'config_dev.json'), parseValues: true});
}

// Defaults, fall back to these if they are not found in any of the previous sources
nconf.defaults({

How to use nconf.stores:

124
125
126
127
128
129
130
131
132
133
        nconf.set('url_parsed', url.parse(nconf.get('url')));
}

// Explicitly cast 'jobsDisabled' as Bool
var castAsBool = ['jobsDisabled'];
nconf.stores.env.readOnly = false;
castAsBool.forEach(function (prop) {
        var value = nconf.get(prop);
        if (value) {
                nconf.set(prop, typeof value === 'boolean' ? value : String(value).toLowerCase() === 'true');

How to use nconf.set:

64
65
66
67
68
69
70
71
72
73
/*
 * Hapi server instance
 */

if (_.isUndefined(nconf.get('HOST'))) {
  nconf.set('HOST', 'localhost');
}

if (_.isUndefined(nconf.get('PORT'))) {
  nconf.set('PORT', 1330);

How to use nconf.get:

50
51
52
53
54
55
56
57
58
59
name: '',	// Something unique to your OAuth provider in lowercase, like "github", or "nodebb"
oauth: {
	requestTokenURL: '',
	accessTokenURL: '',
	userAuthorizationURL: '',
	consumerKey: nconf.get('oauth:key'),	// don't change this line
	consumerSecret: nconf.get('oauth:secret'),	// don't change this line
},
oauth2: {
	authorizationURL: '',