diff --git a/lib/router.js b/lib/router.js index 02243f1d94..c7bc8f2213 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1,14 +1,11 @@ // LICENCE https://github.com/adaptlearning/adapt_authoring/blob/master/LICENSE -/** - * Module used to set up routes - * - * @module lib/routes - */ - +var _ = require('underscore'); +var async = require('async'); var fs = require('fs'); var path = require('path'); + var configuration = require('./configuration'); -var _ = require('underscore'); +var logger = require('./logger'); /* * CONSTANTS @@ -28,11 +25,25 @@ function Router (options) { Router.prototype.init = function (app) { var basePath = this.options.path; - var routes = fs.readdirSync(basePath); var server = app.server; - routes.forEach (function (routePath) { - var route = require(path.join(basePath, routePath)); - server.use(route); + fs.readdir(basePath, function(error, contents) { + if(error) { + logger.log('error', error); + return; + } + async.each(contents, function(item, cb) { + var itemPath = path.join(basePath, item); + if(!fs.statSync(itemPath).isDirectory()) { + return; + } + try { + var route = require(itemPath); + server.use(route); + } catch(e) { + var detail = (e.code === 'MODULE_NOT_FOUND') ? ', no index.js found' : ` (${e})`; + logger.log('error', `Cannot load routes/${item}${detail}`); + } + }); }); };