@flexshopper/hapi-pres
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) => {
});
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);
};
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)
const internals = module.exports = {};
internals.myAwesomePre = (request, reply) => {
return reply('myAwesomePre');
}
internals.mySecondAwesomePre = (request, reply) => {
return reply('mySecondAwesomePre');
}
server.route({
method: 'GET',
path: '/ping',
config: {
pre: [
server.pre.myPre.myAwesomePre,
server.pre.myPre.mySecondAwesomePre
],
handler: (request, reply) => {
return reply('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.