I'm new to both Node and Hapi.js, but not to programming.
I've made a few simple Hapi.js servers, just for testing out Joi and other plugins.
I'm now trying to understand how best to package up chunks of functionality in a Hapi way, so we can re-use them in future projects and potentially share them with the world!
Are there any good resources for learning this available?
EDIT: I've found a few good resources, but non which go into deep details about the plugin system.
Essentially, a plugin is an object with a register property that returns a function with the signature function (server, options, next) . Further, the register function has an attributes object that contains meta data about your plugin to provide some extra data for hapi.
Creating a Server A very basic hapi server looks like the following: 'use strict'; const Hapi = require('@hapi/hapi'); const init = async () => { const server = Hapi. server({ port: 3000, host: 'localhost' }); await server. start(); console.
A server composer for hapi. Glue provides configuration based composition of hapi's Server object. Specifically it wraps. server = Hapi.server(Options) server.register(Plugins, Options)
I've made a sample project which groups functionality into plugins which might be what you're looking for:
https://github.com/johnbrett/hapi-level-sample
If you're looking at sharing functionality between plugins, look at the usage of plugin.expose, plugin.depend.
If you have any questions on it, you can raise an issue on the github project.
The latest hapijs.com website has a good intro as well: http://hapijs.com/tutorials/plugins
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With