scenic
v1.0.1
Published
Programmatically setup routes and resources for HapiJS server
Downloads
3
Readme
Scenic
A very hapi route auto-loader
Usage
Easily manage your HapiJS routes and resources by storing your basic route configurations in a designated routes folder.
Examples
- Basic Example - Just a basic example usage
- Advanced Example - Something a little more realistic, this is an example of how you can use Scenic with other plugins to load pretty much everything programmatically, such as the routes, controllers, configuration file and all other HapiJS plugins (via Confidence)
Under The Hood
There really isn't anything too fancy here. All this does is recursively iterate through the folder containing your routes, and load any of the exported configurations into Hapi as resources, but before doing so, the path to the file containing the resources will be appended to the path
value in the resources themselves (relative to the routes folder)
Example
Lets say I create a file at `routes/hello.js, with the following content:
module.exports = [{
method: 'GET',
path: '/world',
handler: function( request, reply ){
return reply('Hello!')
}
},{
method: 'POST',
path: '/world',
handler: function( request, reply ){
return reply('Thank you for your details')
}
}]
This will create two resources:
- GET /hello/world
- POST /hello/world
Route Folder
By default, Scenic will look for a folder called /routes
in the root of you project, but you can change this by defining routeDir
in the plugin options when loading the plugin. As long as the value is a valid path to an existing folder, it should work fine.
Root Resource
Since the filename containing the resources is prepended to the route path within the path files, you will need to define which route file you want to use as your root resource, this will skip the modification of the path value in the resources. To do this, just add a rootResource
value to the plugin options, and specify the file you wish to use (without the .js
extension)
Debugging
If you want to know whats going on behind the scenes, then add a debug
value to the options when loading Scenic, or in the configuration file. The value should be numeric:
- 0 - No output (Same as null)
- 1 - Errors only
- 2 - Errors and information
- 3 - Errors, information and debugging data
Note: If you install Chalk, then the output will be colorized.
Todo
- Allow users to specify more than one
rootResource
file, skipping path modifications for all contained resources - Write unit tests (yeah yeah yeah, I know)