firstandthird / hapi-method-loader

Hapi method loader
MIT License
0 stars 1 forks source link
hapi-plugin

hapi-method-loader Build Status

A plugin that automatically loads hapi server methods for you. Never type server.method(....) again!

Installation

npm install hapi-method-loader

Usage

server.register({
  register: require('hapi-method-loader'),
  options: {}
});

Will cause hapi to scan the methods directory and import all the files it finds there as server methods.

Method Files

Each method should be a file in the methods directory. Sub directories may be used for nested methods. File name will dictate method name.

Example Method File:

const Joi = require('@hapi/joi');
module.exports = {
  method: function(name) {
    // 'this' will be bound to the server:
    this.log(`Hello ${name}!`);
  },
  options: {
    cache: {
      expiresIn: 60 * 60 * 1000
    }
  },
  schema: Joi.object({
    name: Joi.string().required()
  }),
  description: 'Greets the user by name'
};

Example Directory Layout:

-methods/
  -hello.js
  -world.js
  |-data/
    |-dump.js
    |-db/
      |- fetch.js
      |- put.js

Will result in the following server methods:

Plugin Options

The following options can be passed when the plugin is registered with hapi: