Skip to content

Instantly share code, notes, and snippets.

@mezerotm
Created June 29, 2017 20:03
Show Gist options
  • Save mezerotm/ed14ab3507579417080889cc2cb2febe to your computer and use it in GitHub Desktop.
Save mezerotm/ed14ab3507579417080889cc2cb2febe to your computer and use it in GitHub Desktop.
Recursively applies routes to restify using [restify-router](https://www.npmjs.com/package/restify-router). Create folders and place index.js at each level. for example `/routes/users/index.js` will appear as `localhost:3000/users/`
function recursivelyApplyRoutes(route){
(function recurse(dir){
let absoluteRoutesPath = process.cwd() + route + (dir? dir:'');
fs.readdirSync(absoluteRoutesPath).forEach((file) =>{
let absoluteCurrentFilePath = absoluteRoutesPath + '/' + file;
let isDirectory = fs.statSync(absoluteCurrentFilePath).isDirectory();
if(isDirectory) recurse((dir? dir:'') + '/' + file);
if(file.indexOf('index.js') !== -1) require(absoluteCurrentFilePath).applyRoutes(server, dir);
});
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment