This package has been deprecated

Author message:

<=v0.7.1

hapi-configue

0.7.1 • Public • Published

Hapi Configue Plugin

Config plugin for Hapi.

NPM Version Experimental Build Status Coverage Status Dependency Status bitHound Overalll Score

RENAMED TO CONFIGUE

This node.js module is being renamed to Configue, dropping the hapi- Prefix

Release v0.7.1 is to warn of the migration!

Configue is a wrapper on nconf node hierarchical plugin tool. It defines a standard workflow to load a config from environment variables, command line arguments, files, that you can easily configure and extend.

About Configue

Configue builds up on nconf and its Hierarchical configuration system. It defines a list of configuration step that are executed in order. Every property defined on a steps will shadow the same key in the following steps.

Quoting nconf: "The order in which you attach these configuration sources determines their priority in the hierarchy"

Here are the standard steps Configue does define:

  • argv : command line option
  • env : environment variables
  • file : config files
  • defaults : default objects

The plugin loads the various configurations in order using predefined steps. It starts by parsing argv then goes through the env and the files options and finishes by loading the default config objects if any. Hence why every option defined as an argument commandline will override defaults and environment variables.

Installation

Just add hapi-configue has a dependency installing it with npm.

npm install --save hapi-configue

Usage

Basic usage without customization

server.register({register: Configue}, (err) => {
    if (err) return console.log("Error loading plugins");
 
    const who = server.configue('who') || "World";
 
    server.route({
        method: 'GET', path: '/', handler: function (request, reply) {
            reply("Hello " + who);
        }
    });
 
    server.start(function () {
        console.log('Server running at:', server.info.uri);
        console.log('With "who" as ' + who)
    });
});

You can specify the who configue in different manners. Here are some:

node basic.js --who=Woman
# configue through Env 
export who=Man ; node basic.js
who=Human node basic.js

The full example is available in the examples folder.

Usage with customization of the configuration workflow

Specifying Files

The files key can contain a single object or an array of objects containing a file key containing the path to the config file. The object can also reference a nconf plugin tasked with the formatting using the key format.

const server = new Hapi.Server();
server.connection();
server.register({
    register: Configue,
    options: {
        files: [
            {file: 'path/to/config.json'},
            {
                file: 'path/to/config2.yaml',
                format: require('nconf-yaml')
            }
        ]
    }
}, (err) => {
    // Your code here
});

Disabling Steps

The argv and env steps can be skipped using the disable object in options.

const server = new Hapi.Server();
server.connection();
server.register({
    register: Configue,
    options: {
        disable: {
            argv: true
        }
    }
}, (err) => {
    // Your code here
});

Step hooks

Every step has a post hook available. Those can be defined using the postHooks key and accept a function that take nconf as a parameter and a callback as a parameter.

The special hooks overrides enables you to respectively apply a hook at the very beginning.

const server = new Hapi.Server();
server.connection();
server.register({
    register: Configue,
    options: {
        postHooks: {
            overrides: function first(nconf, done){
                //Your code here
            },
            argv: function postArgv(nconf, done){
                //Your code here
            }
        }
    }
}, (err) => {
    // Your code here
});

Custom Workflow

If needed you can have your full custom configuration workflow, simply by providing an object with the single key customWorkflow

const configueOptions = { customWorkflow: function(nconf, done){
  // my own config setting
}};
 
server.register({register: Configue, options: configueOptions}, (err) => {
 
});
 

Readme

Keywords

none

Package Sidebar

Install

npm i hapi-configue

Weekly Downloads

1

Version

0.7.1

License

BSD-3-Clause

Last publish

Collaborators

  • mlequain
  • adriean.khisbe