https://github.com/aaronleocooper/hapi-namespaced-routes
A Hapi plugin to load routes with a namespace
https://github.com/aaronleocooper/hapi-namespaced-routes
api hapi hapi-plugin namespace plugin rest-api routes
Last synced: 11 months ago
JSON representation
A Hapi plugin to load routes with a namespace
- Host: GitHub
- URL: https://github.com/aaronleocooper/hapi-namespaced-routes
- Owner: AaronLeoCooper
- License: mit
- Created: 2017-11-07T05:36:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-09T07:25:19.000Z (over 8 years ago)
- Last Synced: 2025-03-07T19:06:29.210Z (over 1 year ago)
- Topics: api, hapi, hapi-plugin, namespace, plugin, rest-api, routes
- Language: JavaScript
- Size: 86.9 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hapi Namepsaced Routes
[](https://badge.fury.io/js/hapi-namespaced-routes)
[](https://travis-ci.org/AaronLeoCooper/hapi-namespaced-routes)
[](https://coveralls.io/github/AaronLeoCooper/hapi-namespaced-routes?branch=master)
A Hapi plugin to enable writing routes with a namespace (prefix) with
a minimal API.
## Usage
Install the module:
```bash
npm install hapi-namespaced-routes
```
Then register it in your Hapi app:
```javascript
const server = new Hapi.Server()
server.register(require('hapi-namespaced-routes'))
```
And finally, add your app routes anywhere you have access to your app
`server` instance:
```javascript
server.methods.namespacedRoute({
namespace: '/posts',
routes: [
{
method: 'GET',
path: '',
handler: postsHandler
},
{
method: 'GET',
path: '/comments',
handler: commentsHandler
}
]
})
/**
* Generates these routes:
* /posts
* /posts/comments
*/
```
## How it works
The logic in this plugin is deliberately simple: its sole purpose is to prefix
routes with a namespace in their path. Once all passed routes have been
prefixed, they are passed to Hapi's built-in `server.route` method, so anything
you add to route objects will also be passed through unaffected.
## Todos
In the interests of keeping this module small I probably won't add a lot of
extra features. That said, I'm open to ideas!
These are the currently planned additions sometime in the near future:
- [ ] Ability to inject routes at registration time
- [ ] "Global" namespace option (for auto-wrapping all passed in routes)
## Alternatives
One alternative package I've seen that does a similar job to this plugin is
dsernst's [hapi-namespace](https://github.com/dsernst/hapi-namespace).
It offers an alternative API— opting to use a wrapper function to accept the
namespace and routes as parameters, and it doesn't explicitly require Hapi
since it doesn't use Hapi's plugin system, so that might be a more favourable
option depending on your needs.
There's probably a few other options out there too in the wild. Feel free to
bring any others to my attention and I'll add those here too!
## Tests
This plugin has unit tests that can be executed by cloning this repo to your
machine and running `npm test` after `npm install`. At the time of writing,
passing tests and test coverge are at 100%.