hapi-route-auto-reg

Automatic registration for hapi routes

Usage no npm install needed!

<script type="module">
  import hapiRouteAutoReg from 'https://cdn.skypack.dev/hapi-route-auto-reg';
</script>

README

Hapi route auto-registration

Build Status NPM version Dependencies

Automatically scans the directory and registers the routes it finds within

installation:

npm install hapi-route-auto-reg

usage:

var hapi = require("hapi");

var server = Hapi.createServer('127.0.0.1', 3000, {});

server.pack.register([
  {
    plugin: require("hapi-route-auto-reg"),
    options: {
      directory: './path/to/routes'
    }
  }], function(err){
    if(err) {
      throw err;
    }
    
    server.start(function(){
      server.log('server started...');
    });
});

Scans the given directory for .js files which export a method .routes(plugin) and invokes them.

The routes files should look like this:

module.exports.routes = function(plugin){
    plugin.route([
        {
            method: 'GET',
            path: '/my/test/route',
            config: {
                handler: function(request, reply) {
                    ...
                }
            }
        }
    ]);
}