How to use the any function from nconf

Find comprehensive JavaScript nconf.any code examples handpicked from public code repositorys.

nconf.any is a function in the nconf library that retrieves the value of a configuration key from any of the registered configuration sources.

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;
fork icon0
star icon3
watch icon2

6
7
8
9
10
11
12
13
14
15
16
17
const activePlugins = require('./build/active_plugins.json');


let relativePath = nconf.get('relative_path');
if (relativePath === undefined) {
    nconf.file({
        file: path.resolve(__dirname, nconf.any(['config', 'CONFIG']) || 'config.json'),
    });


    const urlObject = url.parse(nconf.get('url'));
    relativePath = urlObject.pathname !== '/' ? urlObject.pathname.replace(/\/+$/, '') : '';
fork icon1
star icon2
watch icon0

+ 2 other calls in file

How does nconf.any work?

nconf.any is a function in the nconf library that retrieves the value of a configuration key from any of the registered configuration sources. When you call nconf.any, the function first checks if the key exists in any of the registered configuration sources. If the key exists in a source, nconf.any returns its value. If the key does not exist in any of the registered configuration sources, nconf.any returns undefined. Here's an implementation of nconf.any to illustrate how it works: javascript Copy code {{{{{{{ const nconf = require('nconf'); nconf.any = (keys) => { for (const key of keys) { const value = nconf.get(key); if (value !== undefined) { return value; } } return undefined; }; In this implementation, we extend the nconf library by adding a new method called any using nconf.any = (keys) => {...}. The any method takes an array of keys as its argument. We then iterate through each key in the keys array using a for...of loop and call nconf.get(key) to get the value of the key from any of the registered configuration sources. If the value of the key is not undefined, we return it. If the value of the key is undefined for all sources, we return undefined. Overall, nconf.any is a useful function in the nconf library that allows you to retrieve the value of a configuration key from any of the registered sources.

10
11
12
13
14
15
16
17
18
19
const _ = require('lodash');

process.env.NODE_ENV = process.env.NODE_ENV || 'production';

// Alternate configuration file support
const configFile = path.resolve(__dirname, '../../../', nconf.any(['config', 'CONFIG']) || 'config.json');
const prestart = require('../../prestart');

prestart.loadConfig(configFile);
prestart.setupWinston();
fork icon0
star icon1
watch icon1

Ai Example

1
2
3
4
5
6
7
8
const nconf = require("nconf");

nconf.argv().env().file({ file: "config.json" });

const databaseUrl = nconf.any(["DATABASE_URL", "DB_URL"]);

console.log(databaseUrl);
// Output: 'postgres://localhost/mydatabase'

In this example, we first require the nconf library using const nconf = require('nconf'). We then register three configuration sources using nconf.argv().env().file({ file: 'config.json' }). The first configuration source is argv, which reads command-line arguments. The second configuration source is env, which reads environment variables. The third configuration source is a JSON file called config.json. We then call nconf.any(['DATABASE_URL', 'DB_URL']) to retrieve the value of a configuration key called DATABASE_URL or DB_URL from any of the registered sources. If the key exists in any of the sources, nconf.any returns its value, which in this case is 'postgres://localhost/mydatabase'. If the key does not exist in any of the sources, nconf.any returns undefined. Finally, we log the databaseUrl variable to the console, which outputs the value of the DATABASE_URL key: 'postgres://localhost/mydatabase'. Overall, this example demonstrates how to use nconf.any to retrieve the value of a configuration key from any of the registered sources in Node.js.