-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootloader.js
60 lines (54 loc) · 1.29 KB
/
bootloader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Modules dependencies.
*/
var fs = require('fs');
var path = require('path');
var express = require('express');
var url = require('url');
var engine = require('ejs-locals');
/**
* Application loader
*/
(function () {
/**
* Constructor.
* @param {express} app.
*/
function Bootloader(app) {
this.loadControllers(app);
this.loadApiControllers(app);
}
/**
* load module controllers
* @param {express} module
*/
Bootloader.prototype.loadControllers = function (app) {
var controllerFolder = __dirname + '/app/controllers/';
loadControllerFromFolder(controllerFolder, app);
};
/**
* load module api controllers
* @param {express} app
*/
Bootloader.prototype.loadApiControllers = function (app) {
var apiFolder = __dirname + '/app/api/';
loadControllerFromFolder(apiFolder, app);
};
/**
* Dynamically load controller from folder.
* @param {String} folderpath
* @param {express} app
*/
var loadControllerFromFolder = function(folderpath, app) {
fs.readdir(folderpath, function (err, files) {
if (err) { throw err; }
files.forEach(function (file) {
if (path.extname(file) === '.js') {
var Controller = require(folderpath + file);
var controller = new Controller(app);
}
});
});
};
module.exports = Bootloader;
})();