@flexshopper/hapi-pres
Hapi plugin to autoload pres.
Last updated 4 years ago by ar4mirez .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @flexshopper/hapi-pres 
SYNC missed versions from official npm registry.

@flexshopper/hapi-pres

Build Status Dependencies Dev dependencies

Plugin to autoload pres based on patterns.

How to use:

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

Registering

Kraken service style:

const server = new Hapi.Server();

server.connection();

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

Manifest style:

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

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())

Pre-requirement Signature(Version 2.x.x and before)

const internals = module.exports = {};

internals.assign = 'myAwesomePre';
internals.method = (request, reply) => {
    return reply(request.query);
};

// then in the routes
server.route({
    method: 'GET',
    path: '/ping',
    config: {
        pre: [
             server.pre.myAwesomePre
        ],
        handler: (request, reply) => {
             return reply(request.pre.example);
        }
    }   
});

Pre-requirement Signature(Version 3.x.x and after)

// in you pre-handler file
const internals = module.exports = {};

internals.myAwesomePre = (request, reply) => {

    return reply('myAwesomePre');
}

internals.mySecondAwesomePre = (request, reply) => {
    
    return reply('mySecondAwesomePre');
}

// assume your pre-handler file name is my-Pre.js
// then in the routes
server.route({
    method: 'GET',
    path: '/ping',
    config: {
        pre: [
            server.pre.myPre.myAwesomePre,
            server.pre.myPre.mySecondAwesomePre
        ],
        handler: (request, reply) => {
            return reply('handler');
        }
    }
});

// if you need to get result from pre handler
request.pre.myAwesomePre
request.pre.mySecondAwesomePre

Cautions

  • Please do not name two different pre-handlers with same name, even in different files. Otherwise they won't be accessed correctly.

Current Tags

  • 3.1.0                                ...           latest (4 years ago)

6 Versions

  • 3.1.0                                ...           4 years ago
  • 3.0.0                                ...           4 years ago
  • 2.0.0                                ...           4 years ago
  • 1.0.4                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 7
Dependencies (6)
Dev Dependencies (12)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |