Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make a Hapi.JS plugin/module?

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.

  • https://github.com/hapijs/hapi/issues/1489 - Getting Started Tutorial Contest
  • https://github.com/otodockal/hapi-tutorial - Entry in above contest, with a section on plugins
like image 428
a_c_m Avatar asked Apr 10 '14 10:04

a_c_m


People also ask

What is plugin in Hapi JS?

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.

How do I make a HAPI server?

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.

What is Hapi glue?

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)


1 Answers

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

like image 73
John Avatar answered Oct 08 '22 23:10

John