Autoloads hapi routes.
Just type yarn add hapi-auto-route
or npm i -S hapi-auto-route
.
Suppose your directory looks like this:
node_modules/
routes/
home.js
server.js
package.json
// routes/home.js
module.exports = {
method: 'GET',
path: '/',
handler: (request, reply) => reply('Hello');
}
// server.js
'use strict';
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 3000, labels: ['web'] });
server.register(require('hapi-auto-route'), (error) => {
if(error) throw error;
server.start();
});
Now, you can start the server and see Hello
at http://localhost:3000
.
dir
: the absolute path to the route directories. Defaults toprocess.cwd() + '/routes'
.pattern
: glob pattern used to find route files. Defaults to/**/!(_)*.js
.prefix
: Use directory tree as prefix. Defaults totrue
.
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.
This project is licensed under the MIT License - see the LICENSE.txt file for details.