Skip to content

Instantly share code, notes, and snippets.

@mikefrey
Created March 11, 2016 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikefrey/66cce57d1fdd78a87b32 to your computer and use it in GitHub Desktop.
Save mikefrey/66cce57d1fdd78a87b32 to your computer and use it in GitHub Desktop.
Route Loader plugin for hapi.js
/*
Loads and registers routes automatically.
USAGE
{
register: require('../app/plugins/route-loader'),
options: {
pattern: 'app/routes/*.js'
}
}
*/
'use strict'
const glob = require('glob')
const _ = require('lodash')
const Path = require('path')
const cwd = process.cwd()
function getRoutes(pattern) {
let files = glob.sync(pattern)
return files.reduce((memo, file) => {
file = Path.join(cwd, file)
let actions = _.values(require(file))
return [].concat(memo, actions)
}, [])
}
exports.register = function(server, options, next) {
server.register([require('vision'), require('inert'), require('./api-route')], err => {
if (err) throw err
server.views(options.views)
server.route(getRoutes(options.pattern))
})
next()
}
exports.register.attributes = {
name: 'route-loader',
dependencies: 'api-route',
version: '1.0.0'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment