Member-only story
Server-Side Development with Hapi.js — Plugins
3 min readFeb 24, 2021
Hapi.js is a small Node framework for developing back end web apps.
In this article, we’ll look at how to create back end apps with Hapi.js.
Create a Plugin
We can create plugins and use them with Hapi.
For example, we can write:
index.js
const Hapi = require('@hapi/hapi');const init = async () => {
const server = Hapi.server({
port: 3000,
host: '0.0.0.0',
}); await server.register(require('./myPlugin')); server.route({
path: '/',
method: 'GET',
handler(request, h) {
return h.response('hello')
},
});
await server.start();
console.log('Server running on %s', server.info.uri);
};process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});init();
myPlugin.js
const myPlugin = {
name: 'myPlugin',
version: '1.0.0',
register: async function (server, options) {
server.route({
method: 'GET',
path: '/test',
handler: function (request, h) {
return 'hello, world';
}
});
}
};module.exports = myPlugin