hapi-route-hierarchy
Hapi plugin that automatically sets up your routes based on your directory hierarchy.
What is this?
The purpose of this plugin is to allow intuitive route setup by basing routes on your directory structure. For example, imagine you have a Hapi project with this folder structure:
my_hapi_project
│ server.js
│
└───routes
│ login.js
│
└───users
│ group_a.js
│ group_b.js
Assume that server.js
simply starts the Hapi server, after registering the plugin like this:
server
All the files under /routes
include the data for all the routes. So for example, /routes/login.js
might look like this:
moduleexports = method: 'GET' path: '/login' { return h; };
This will result in a route at /login
, which is rather boring, but what if we put something similar in /routes/users/group_a.js
:
moduleexports = method: 'GET' path: '/group_a' { return h; };
This would result in a route at /users/group_a
.
In this way you can easily keep your routes organized by ensuring they always match your directory structure.
Plugin Options
root
-
REQUIRED
-
Type:
string
-
Description: The directory to search for files with route data. This can accept 2 types of paths:
- Relative path from
process.cwd()
- Absolute path
- Relative path from
glob_pattern
- REQUIRED
- Type:
string
- Description: The glob pattern to use to determine which files should be loaded as route files.
glob_options
- Type:
object
- Description: Options that will be passed to the
glob
NPM module. Here is a full list of available options.
Route files
The route files can define route data in 3 different ways:
Object
moduleexports = method: 'GET' path: '/some_path' { // Do something... };
Array
moduleexports = method: 'GET' path: '/some_path' { // Do something... } method: 'POST' path: '/some_other_path' { // Do something... } ;
Function
Note that this variation allows you to access the Hapi server object
module{ return method: 'GET' path: '/some_path' { let some_fancy_plugin = requestserverplugins'some_fancy_plugin'; h; } ;};
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.