3.2.0 • Published 10 days ago

hapi-auto-route v3.2.0

Weekly downloads
81
License
MIT
Repository
github
Last release
10 days ago

hapi-auto-route

NPM Github action

hapi-auto-route is a hapi plugin that lets you load route objects automatically by specifying the root directory/directories containing the routes. And allow routes path to be prefixed.

Maintainer: Sitraka Ratsimba

Installation

For Hapi >= v17:

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 Path = require('path');
const Hapi = require('@hapi/hapi');

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

const init = async () => {
    await server.register({
      plugin: require('hapi-auto-route'),
      options: {
        routes_dir: Path.join(__dirname, 'routes')
      }
     });
    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.

You can also provide an array of absolute paths if you want to auto-import from multiple base directories.

API

  • routes_dir: absolute path(s) to routes directory/directories. required
  • 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.

3.2.0

10 days ago

3.1.0

10 days ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.4.4

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago