2015-05-05 11:56:35 +02:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var express = require('express');
|
|
|
|
var fs = require('fs');
|
2015-07-07 09:27:03 +02:00
|
|
|
var Log = require('./NodeLog.js');
|
2015-05-05 11:56:35 +02:00
|
|
|
|
2015-07-07 09:27:03 +02:00
|
|
|
module.exports = function(parent){
|
2015-05-19 18:09:46 +02:00
|
|
|
|
2015-07-07 09:27:03 +02:00
|
|
|
Log.debug("Loading controllers :");
|
2015-06-25 17:21:19 +02:00
|
|
|
|
2015-05-19 18:09:46 +02:00
|
|
|
fs.readdirSync(__dirname + '/../posts').forEach(function(name){
|
2015-05-05 11:56:35 +02:00
|
|
|
|
|
|
|
// index.js in controller, with function as pages (views.py for django)
|
2015-05-19 18:09:46 +02:00
|
|
|
var obj = require('./../posts/' + name + '/index');
|
2015-05-05 11:56:35 +02:00
|
|
|
|
|
|
|
// urls.js, just like django urls.py
|
2015-05-19 18:09:46 +02:00
|
|
|
var urls = require('./../posts/' + name + '/urls');
|
2015-07-01 10:14:15 +02:00
|
|
|
name = obj.name || name;
|
2015-05-05 11:56:35 +02:00
|
|
|
var app = express();
|
|
|
|
|
|
|
|
// allow specifying the view engine
|
|
|
|
if (obj.engine) app.set('view engine', obj.engine);
|
2015-05-19 18:09:46 +02:00
|
|
|
app.set('views', __dirname + '/../posts/' + name + '/views');
|
2015-05-05 11:56:35 +02:00
|
|
|
|
|
|
|
// generate routes based
|
|
|
|
// on the exported methods
|
|
|
|
|
2015-07-07 09:27:03 +02:00
|
|
|
Log.debug(' ' + name + ':');
|
2015-07-01 10:14:15 +02:00
|
|
|
|
2015-05-05 11:56:35 +02:00
|
|
|
for (var key in urls) {
|
2015-07-23 17:19:53 +02:00
|
|
|
app.post(key, obj[urls[key]]);
|
2015-07-01 10:14:15 +02:00
|
|
|
|
2015-07-23 17:19:53 +02:00
|
|
|
Log.debug(' ' + key + ' -> ' + name + '.' + urls[key]);
|
2015-05-05 11:56:35 +02:00
|
|
|
}
|
2015-07-01 10:14:15 +02:00
|
|
|
|
2015-07-07 09:27:03 +02:00
|
|
|
Log.debug();
|
2015-05-05 11:56:35 +02:00
|
|
|
|
|
|
|
// mount the app
|
|
|
|
parent.use(app);
|
|
|
|
});
|
|
|
|
};
|