Plugins in hapi js

In this post I will be going over some hapi js plugin examples. A plugin in hapi like most other frameworks is just an object that is formated a certain way. There just needs to be a register property and a name at a minimum, but there are a few more properties that are also of concern.

1 - Basic Hapi plugin example

In this section I will be going over some very basic hello world style plugin examples, it is also worth noting that in this section I am using hapi 17.9.0, in older and newer versions of hapi these code examples might break.

So a very basic hapi plugin example can just be an object. This way of using plugins can be used as a good starting point when it comes to the process of breaking things down into smaller more manageable, and reusable blocks of code.

The object must have a register property and a name property. The name can just be a string, and the register must be a async function that will do whatever needs to happen for the plugin, such as setting up a path which will be what is going on in this plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
let Hapi = require('@hapi/hapi');
let pluginRoot = {
name: 'pluginRoot',
register: async function (server, options) {
server.route({
method: 'GET',
path: '/',
handler: function (request, h) {
return 'hello world this is ' + options.mess;
}
});
}
};
The server argument will be a reference to the server object that is created with Hapi.server in the main script that uses the plugin. The options object is what will also be given when the plugin is used in the main script as well with server.register.
let init = async() => {
let server = Hapi.server({
port: 3000,
host: 'localhost'
});
await server.register({
plugin: pluginRoot,
options: {
mess: 'foobar'
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
init();

1.1 - Same basic example as external js

So in this sub section I will be going over what is the same example before, only now things are broken down into two completely different files.

In a module.js file I just have the following.

1
2
3
4
5
6
7
8
9
10
11
12
exports.pluginRoot = {
name: 'pluginRoot',
register: async function (server, options) {
server.route({
method: 'GET',
path: '/',
handler: function (request, h) {
return 'hello world this is ' + options.mess;
}
});
}
};

An then I use it in the main script just like before only now I use require to load it in.

1
2
3
4
5
6
7
8
9
10
11
12
13
let Hapi = require('@hapi/hapi');
let init = async() => {
let server = Hapi.server({port: 3000,host: 'localhost'});
await server.register({
plugin: require('./module').pluginRoot,
options: {
mess: 'foobar'
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
init();