var express = require('express'); var fs = require('fs'); var log = require('log'); module.exports = function(app) { log.debug("Loading controllers :"); fs.readdirSync(__dirname + '/../controllers').forEach(function(name) { // views.js in controller, with function as pages (views.py for django) var obj = require('./../controllers/' + name + '/views'); // urls.js, just like django urls.py var urls = require('./../controllers/' + name + '/urls'); name = obj.name || name; // allow specifying the view engine if (obj.engine) app.set('view engine', obj.engine); log.debug(' ' + name + ':'); for (let url of urls) { app.get(url.url, ((url) => function(req, res, next) { var path = obj[url.view](req, res, function(view) { let viewPath = __dirname + '/../controllers/' + name + '/views/' + view; res.render(viewPath, res.locals, function(err, out) { if (err !== null) { log.pugerror(err); } res.send(out); }); }, next); })(url)); log.debug(' ' + url.url + ' -> ' + name + '.' + url.view); } log.debug(); // mount the app // parent.use(app); }); }