Skip to content

Instantly share code, notes, and snippets.

@devinivy
Last active March 2, 2018 14:05
Show Gist options
  • Save devinivy/12f4f27107c9f3e9a7ec3348900acd8e to your computer and use it in GitHub Desktop.
Save devinivy/12f4f27107c9f3e9a7ec3348900acd8e to your computer and use it in GitHub Desktop.
recursive require-dir for hapi routes
'use strict';
const tree = require('require-dir')(null, {
recurse: true // Consider also adding filter() to ignore e.g. helper directories
});
const flatten = (routes, obj) => {
if (Array.isArray(obj) || (obj.path && obj.method)) {
return routes.concat(obj);
}
return routes.concat(
Object.keys(obj)
.map((filename) => obj[filename])
.reduce(flatten, [])
);
};
module.exports = flatten([], tree);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment