FI
firstandthird/hapi-method-events
Hapi plugin to execute a method when an event fires
hapi-method-events
A hapi plugin that makes it easy to create
response handlers for server events
Installation
npm install hapi-method-eventsUsage
const plugin = require('hapi-method-events');
const server = new hapi.Server({ });
// create a server event:
server.event('user.add');
// create one or more server methods to respond to the event:
server.method('userAddHandler', (string) => {
console.log(string);
});
server.method('anotherUserAddHandler', (data) => {
console.log(data.string2);
});
// register the plugin and specify server methods to call
// in the 'events' field
await server.register({
plugin,
options: {
events: [{
event: 'user.add',
// notice that the method is specified as a string
// and you can choose what value to pass to it
// when the event is emitted:
method: 'userAddHandler(data.string1)'
},
{
event: 'user.add',
method: 'anotherUserAddHandler(data)'
}]
}
});
// now call the event!
await server.events.emit('user.add', {
data: {
string1: 'Hello World!',
string2: 'Hello Again!'
}
});Options
- events (required)
A list of events and methods to call for those events. The events should be specified as string.
- verbose
In verbose mode, hapi-method-events will print out the events and command string being executed.
By default this is set to false.
On this page
Languages
JavaScript100.0%
MIT License
Created December 27, 2017
Updated February 25, 2021