62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
|
var express = require('express');
|
||
|
var fs = require('fs');
|
||
|
var log = require('log.js')
|
||
|
|
||
|
module.exports = function(parent){
|
||
|
|
||
|
log.debug("Loading get :");
|
||
|
|
||
|
fs.readdirSync(__dirname + '/../routes').forEach(function(name){
|
||
|
|
||
|
// index.js in controller, with function as pages (views.py for django)
|
||
|
var obj = require('./../routes/' + name + '/index');
|
||
|
|
||
|
// urls.js, just like django urls.py
|
||
|
var urls = require('./../routes/' + name + '/urls');
|
||
|
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 + '/../routes/' + name + '/views');
|
||
|
|
||
|
// generate routes based
|
||
|
// on the exported methods
|
||
|
|
||
|
log.debug(' ' + name + ':');
|
||
|
|
||
|
for (var key in urls) {
|
||
|
|
||
|
app.get(key, ((key) => function(req, res, next) {
|
||
|
|
||
|
var path = obj[urls[key]](req, res, function(view) {
|
||
|
res.render(
|
||
|
__dirname +
|
||
|
'/../routes/' +
|
||
|
name + '/views/' +
|
||
|
view,
|
||
|
res.locals,
|
||
|
function(err, out) {
|
||
|
if (err !== null) {
|
||
|
log.pugerror(err);
|
||
|
}
|
||
|
res.send(out);
|
||
|
}
|
||
|
);
|
||
|
|
||
|
}, next);
|
||
|
|
||
|
})(key));
|
||
|
|
||
|
log.debug(' ' + key + ' -> ' + name + '.' + urls[key]);
|
||
|
|
||
|
}
|
||
|
|
||
|
log.debug();
|
||
|
|
||
|
// mount the app
|
||
|
parent.use(app);
|
||
|
});
|
||
|
|
||
|
};
|