3d-interface/lib/controllers.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2015-05-05 11:56:35 +02:00
/**
* Module dependencies.
*/
var express = require('express');
var fs = require('fs');
module.exports = function(parent, options){
var verbose = options.verbose;
verbose && console.log("Loading controllers :");
fs.readdirSync(__dirname + '/../controllers').forEach(function(name){
2015-05-19 11:03:53 +02:00
// index.js in controller, with function as pages (views.py for django)
var obj = require('./../controllers/' + name + '/index');
2015-05-19 11:03:53 +02:00
// urls.js, just like django urls.py
var urls = require('./../controllers/' + name + '/urls');
2015-05-19 11:03:53 +02:00
var name = obj.name || name;
var app = express();
// allow specifying the view engine
if (obj.engine) app.set('view engine', obj.engine);
app.set('views', __dirname + '/../controllers/' + name + '/views');
2015-05-19 11:03:53 +02:00
// generate routes based
// on the exported methods
verbose && console.log('\t' + name + ':');
for (var key in urls) {
app.get(key, obj[urls[key]]);
verbose && console.log('\t\t' + key + ' -> ' + name + '.' + urls[key]);
2015-05-19 11:03:53 +02:00
}
verbose && console.log();
2015-05-19 11:03:53 +02:00
// mount the app
parent.use(app);
});
2015-05-05 11:56:35 +02:00
};