const express = require('express'); const fs = require('fs'); const log = require('log'); module.exports = function(app, controllersDir = __dirname + '/../controllers') { log.debug("Loading controllers :"); fs.readdirSync(controllersDir).forEach(function(name) { // views.js in controller, with function as pages (views.py for django) let obj = require(controllersDir + '/' + name + '/views'); // urls.js, just like django urls.py let urls = require(controllersDir + '/' + 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) { let path = obj[url.view](req, res, function(template) { let templatePath = controllersDir + '/' + name + '/templates/' + template; res.render(templatePath, 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); }); }