Skip to content

Commit

Permalink
Only attempt to load routes that are directories
Browse files Browse the repository at this point in the history
Fixes #1534
  • Loading branch information
taylortom committed Feb 21, 2017
1 parent 7f709ec commit 7cc7303
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions lib/router.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -28,11 +25,29 @@ 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) {
if(e.code === 'MODULE_NOT_FOUND') {
logger.log('error', 'Cannot load routes/' + item + ', no index.js found');
}
else {
logger.log('error', e);
}
}
});
});
};

Expand Down

0 comments on commit 7cc7303

Please sign in to comment.