Menu Close

Server-Side Development with Hapi.js — Plugins

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

We have the myPlugin object with the register method to add the routes we want in the plugin.

We specify the name and version of the plugin to set the metadata.

Then in index.js , we have:

await server.register(require('./myPlugin'));

to register the plugin we added.

We can pass in some options by writing:

index.js

const Hapi = require('@hapi/hapi');

const init = async () => {
  const server = Hapi.server({
    port: 3000,
    host: '0.0.0.0',
  });

  await server.register({
    plugin: require('./myPlugin'),
    options: {
      name: 'Bob'
    }
  });

  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 options.name;
      }
    });
  }
};

module.exports = myPlugin

We get the option.name to get the name option in myPlugin.js .

Then to register the plugin, we write:

await server.register({
  plugin: require('./myPlugin'),
  options: {
    name: 'Bob'
  }
});

to register it with the plugins property to require the plugin file.

options lets us pass in the options.

We can also set a route prefix for the plugin.

For instance, 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'),{
    routes: {
      prefix: '/plugins'
    }
  });

  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

We create the plugin the same way as in the first plugin example.

Then in index.js , we write:

await server.register(require('./myPlugin'), {
  routes: {
    prefix: '/plugins'
  }
});

to set the route prefix.

Now when we go to the /plugins/test route, we see hello world as the response.

Conclusion

We can create plugins to modularize our Hapi app code.

Posted in Hapi