Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only attempt to load routes that are directories #1535

Merged
merged 3 commits into from
Mar 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 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,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}`);
}
});
});
};

Expand Down