2.2.0 • Published 5 years ago

hapi-plus-routes v2.2.0

Weekly downloads
15
License
MIT
Repository
github
Last release
5 years ago

hapi-plus-routes

Hapi plugin. Automatically add routes applying defaults from directory to Hapi API. Works with Hapi 17+.

Installation

yarn add hapi-plus-routes

or

npm i -S hapi-plus-routes

Example

Where registering Hapi plugins add:

const Routes = require('hapi-plus-routes');
const routeOptions = {
  routes: './routes/**/*.js',
  defaultRoute: {
    path: '/users',
    options: {
      plugins: {
        policies: ['preResponsePolicy'],
      },
    },
  },
};

server.register({
  plugin: Routes,
  options: routeOptions,
});

For complete example check Hapi starter.

Error handling

You may specify a custom error handler which will catch exceptions thrown in your routes and do something with them. A 500 internal server error will be returned.

const Routes = require('hapi-plus-routes');
const routeOptions = {
  routes: './routes/**/*.js',
  errorHandler: (request, error) => {
    // Do something with the error
    request.log([request.method, request.path, 'error'], error);
  },
};

server.register({
  plugin: Routes,
  options: routeOptions,
});

Path prefix

You may specify a path prefix for all routes by supplying the prefix option.

const routeOptions = {
  routes: './routes/**/*.js',
  prefix: '/some-prefix',
};
2.2.0

5 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.2

6 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

8 years ago

1.0.0

8 years ago