@flexshopper/hapi-handlers

Standard NPM package with Flexshopper coding standards and guidelines.

Stats

StarsIssuesVersionUpdatedCreatedSize
@flexshopper/hapi-handlers
3.0.05 years ago6 years agoMinified + gzip package size for @flexshopper/hapi-handlers in KB

Readme

@flexshopper/hapi-handlers

Build Status Dependencies Dev dependencies

Plugin to autoload handlers based on patterns.

How to use:

  • Install hapi-handlers npm package in your project our plugin. npm i @flexshopper/hapi-handlers
  • Register plugin in your hapi server:

Registering

const server = new Hapi.Server();

server.connection();

server.register({
    register: require('hapi-handlers'),
    options: {
        relativeTo: proccess.cwd() + '/handlers',
        includes: ['path/to/**/*Handlers.js'],
        ignore: ['*.git'],
    }
}, (err) => {
  // continue application
});

manifest style:

registrations: [
    ...
    {
        plugin: {
            register: 'hapi-handlers',
            options: {
                relativeTo: proccess.cwd() + '/handlers',
                includes: ['path/to/**/*Handlers.js'],
                ignore: ['*.git'],
            }
        }
    }
];

Your handlers are available in your routes using the handle file name:


server.route({
    method: 'GET',
    path: '/route',
    config: {
        handler: {
            handlerName: {} // assuming your handle file is handlerName
        }
    }
})

Options

includes

Required
Type: array

The glob pattern you would like to include

ignore

Type: array

The pattern or an array of patterns to exclude

relativeTo

Type: string

The current working directory in which to search (defaults to process.cwd())

Handler Signature

'use strict';

module.exports = (route, options) => {

    return (request, reply) => {

        return reply({
            message: 'Hello World.'
        });
    };
};

Filename adapting

No matter what format your filenames are in, use camelcase to access the method.

For example, if a handler file name is: foo-bar.js with a method 'myMethod'. You can get access to it as:

    handler: {
        fooBar: { method: 'myMethod' }
    },

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.