Autoloads hapi routes
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
.nvmrc
.travis.yml
CHANGELOG.md
LICENSE
README.md
index.js
package.json
yarn.lock

README.md

hapi-auto-route

Semver Dependency Status devDependency Status

NPM

hapi-auto-route is a hapi plugin that lets you load route objects automatically from a directory.

Installation

For Hapi v17.x.x:

npm i -S hapi-auto-route

For Hapi v16.x.x:

npm i -S hapi-auto-route@1.1.0

Code Example

Suppose your directory looks like this:

node_modules/
routes/
  home.js
server.js
package.json
// routes/home.js
'use strict';

module.exports = {
    method: 'GET',
    path: '/',
    handler: (request, h) => 'Hello';
}
// server.js
'use strict';

const Hapi = require('hapi');

const server = Hapi.Server({
  port: 3000,
  host: 'localhost'
});

const init = async () => {
    await server.register(require('hapi-auto-route'));
    await server.start();
    console.log(`Server is running at: ${server.info.uri}`);
};

process.on('unhandledRejection', (error) => {
  console.log(error);
  process.exit();
});

init();

Now, you can start the server and see Hello at http://localhost:3000.

API

  • routes_dir: directory where routes are located. routes_dir must be relatives from process.cwd(). Defaults to 'routes'.
  • pattern: glob pattern used to find route files. Defaults to **/!(_)*.js.
  • use_prefix: Use directory tree as prefix. Defaults to false.

Contributing

If you find a bug in the source code or a mistake in the documentation, you can help us by submitting an issue or a pull request with a fix.

Licence

This project is licensed under the MIT License - see the LICENSE file for details.