@flexshopper/hapi-handlers

3.0.0 • Public • Published

@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' }
    },

Dependencies (5)

Dev Dependencies (11)

Package Sidebar

Install

npm i @flexshopper/hapi-handlers

Weekly Downloads

27

Version

3.0.0

License

ISC

Last publish

Collaborators

  • mohanadsheikha
  • daniel.garavito
  • mcarballosa
  • fs-lstanley
  • danishkazmi
  • travis-fs