hapi-plugin-router

Routing focused at the plugin level

Usage no npm install needed!

<script type="module">
  import hapiPluginRouter from 'https://cdn.skypack.dev/hapi-plugin-router';
</script>

README

Hapi Plugin Router

Build Status npm version Code Climate

Similar to the hapi-router, this plugin's focus is to provide routing within the scope of a plugin. The hapi-router was nice when you wanted to globally include from the project's root, or custom folder the routes.

The goal of this plugin is to amplify the features of glob based routing and provide a mechanism to use a plugin scoped routing registration. Allowing routes to be contained in custom places, and more importantly registering the routes when the plugin is being initialized.

Installation and Setup

npm install hapi-plugin-router --save

Withing a plugin, you can register routes by utilizing a glob. The glob will be scoped to the plugin's current directory. Meaning, the routes should be files/folders within the plugin' folder

// Your plugin's register function, this assumes there is a folder named routes which contains one or many files.  Each file
export.register = (server, options, next) => {
    const router = server.plugins['hapi-plugin-router'];
    /**
        This will assume the base directory of the plugin that is calling the setup function.  In other words, it knows the current directory this call is in and will by default set it to that directory.
    **/
    router.setup('routes/**/*.js');
    return next();
}

API

The setup function registers the routes based upon the discovery of files indicated by the glob

.setup(globPattern, [options])

  • globPattern - String - the glob pattern. See Glob Primer if you're unfamiliar with what a glob pattern is.
  • options - Object

Options

  • cwd - An override to any directory that should be the base directory of your glob's discovery. By default it will be the calling function's directory.