Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/firstandthird/hapi-route-loader
Automatically load routes from a directory
https://github.com/firstandthird/hapi-route-loader
hapi-plugin hapi-v17 needs-readme
Last synced: 3 days ago
JSON representation
Automatically load routes from a directory
- Host: GitHub
- URL: https://github.com/firstandthird/hapi-route-loader
- Owner: firstandthird
- License: mit
- Created: 2015-10-02T20:12:41.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T18:16:32.000Z (about 2 years ago)
- Last Synced: 2025-01-13T13:38:07.423Z (9 days ago)
- Topics: hapi-plugin, hapi-v17, needs-readme
- Language: JavaScript
- Size: 500 KB
- Stars: 0
- Watchers: 6
- Forks: 2
- Open Issues: 14
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
## hapi-route-loader [![Build Status](https://travis-ci.org/firstandthird/hapi-route-loader.svg?branch=master)](https://travis-ci.org/firstandthird/hapi-route-loader)
## What It Does
A hapi plugin that automatically loads routes from a
directory for your hapi server
(never type "server.route(...)" again!)## Installation
In your main hapi directory do:
```console
npm install hapi-route-loader
```## Directory Layout
You define routes in js files inside a nested directory structure in a
'routes' directory in your server project (file format will be
explained below):
```
/routes
├── api
│ ├── foo.js/
│ ├── bar.js/
│ ├── baz.js/
│ ├── baz.js/
│ ├── bat/
│ ├── bif.js/
├── admin
│ ├── foo.js/
│ ├── bar.js/
├── login.js
├── logout.js
```This will register the following routes with your server:
```
/api/foo
/api/bar
/api/baz
/api/bat/bif
/admin/foo
/admin/bar
/login
/logout
```## File Format
Files contain the options passed to server.route
### Example 1:
/routes/api/myRoute.js:
```javascript
exports.test = {
method: 'GET',
handler(request, h) {
return 'Hello World
';
}
};
```Creates a GET route handler at `/api/myRoute`
### Example 2:
/routes/api/myRoute.js:
```javascript
exports.test = {
path: 'callMe',
method: 'POST',
handler(request, h) {
return { value: request.query.value };
}
};
```Since the 'path' field is specified this creates a POST route at `/api/CallMe` instead of `/api/myRoute`
To register the plugin just do:
```javascript
const routeLoader = require('hapi-route-loader');
const server = new hapi.Server({ port: 8000 });
await server.register({ plugin: routeLoader.plugin, options });```
and this will then load the routes from the base directory## Options (pass these to the plugin)
- __path__
the path to the directory containing your route files, default is the `/routes`
directory in the current working directory (`process.cwd()`). For example, setting
options.path to '/myRoutes' will look in `/myRoutes` instead.
- __base__the root path for all routes, by default this is '/' but you can override
this. For example if 'base' is `/api` and you have a route
specified at `/foo.js` it will be registered as `/api/foo` instead of `/foo`
- __prefix__works like base, except prefix will always be at the beginning of the path
- __verbose__prints out verbose information about each route as it is registered, default is false
- __autoLoad__The route-loading function is exported by the library. By default
hapi-route-loader will load and register all routes when you register the plugin.
Set autoLoad to false to skip this and manually use the exported function.
- __routeConfig__You can specify a base routeConfig for all routes, for example:
```javascript
// this will be assigned to all routes
const pre1 = (request, reply) => {
return 'global!';
};{
routeConfig: {
pre: [
{ method: pre1, assign: 'm1' },
]
}
}
```
will result in each route having the indicated pre method