@flexshopper/hapi-handlers
v3.0.0
Published
Standard NPM package with Flexshopper coding standards and guidelines.
Downloads
8
Readme
@flexshopper/hapi-handlers
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' }
},