Last active
December 9, 2015 01:01
-
-
Save scottoffen/4f891b1089b3be9a7174 to your computer and use it in GitHub Desktop.
Autoload Express Routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = require('config/config'); | |
var debug = require('debug')(global.app.name + ':autoloader'); | |
var path = require('path'); | |
var fs = require('fs'); | |
var mask = /\.routes?\.js/i; | |
var _ = require('lodash'); | |
/*********************************************************************************** | |
* Autoloader entry point * | |
**********************************************************************************/ | |
module.exports = function (app) | |
{ | |
var contents = recurseFolder(global.app.paths.base); | |
loadRoutes(app, contents); | |
} | |
/**********************************************************************************/ | |
/*********************************************************************************** | |
* Recurse the folder structure and return a contents object * | |
**********************************************************************************/ | |
function recurseFolder (folder) | |
{ | |
var contents = { files : [], folders : [] }; | |
fs.readdirSync(folder).forEach(function (item) | |
{ | |
item = { name : item, path : path.join(folder, item) }; | |
var stats = fs.statSync(item.path); | |
if (stats.isDirectory()) | |
{ | |
item["contents"] = recurseFolder(item.path); | |
contents.folders.push(item); | |
} | |
else if ((stats.isFile()) && (mask.test(item.name))) | |
{ | |
contents.files.push(item); | |
} | |
}); | |
return contents; | |
}; | |
/**********************************************************************************/ | |
/*********************************************************************************** | |
* Select to load routes breadth first or depth first * | |
**********************************************************************************/ | |
function loadRoutes (app, contents) | |
{ | |
var bfirst = config.autoloader && config.autoloader.breadthFirst; | |
if (bfirst) | |
{ | |
loadRouteFiles(app, contents.files); | |
loadRouteFolders(app, contents.folders); | |
} | |
else | |
{ | |
loadRouteFolders(app, contents.folders); | |
loadRouteFiles(app, contents.files); | |
} | |
} | |
/**********************************************************************************/ | |
/*********************************************************************************** | |
* Load route folder * | |
**********************************************************************************/ | |
function loadRouteFolders (app, folders) | |
{ | |
_.each(folders, function (folder) | |
{ | |
loadRoutes(app, folder.contents, parseRoutePath(folder.name)); | |
}); | |
} | |
/**********************************************************************************/ | |
/*********************************************************************************** | |
* Load route folder * | |
**********************************************************************************/ | |
function loadRouteFiles (app, files) | |
{ | |
_.each(files, function (file) | |
{ | |
var route = parseRoutePath(file.name); | |
app.use(route, require(file.path)); | |
debug('added route : ' + route); | |
}); | |
} | |
/**********************************************************************************/ | |
/*********************************************************************************** | |
* Parse route from file name * | |
**********************************************************************************/ | |
function parseRoutePath (item) | |
{ | |
item = item.replace(/^index\./, ''); | |
item = item.replace(/\.?routes?\.js$/i, ''); | |
if (item.indexOf('.') > 0) | |
{ | |
item = item.replace(/\./g, '/'); | |
} | |
return '/' + item; | |
} | |
/**********************************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment