diff --git a/lib/controllers.js b/lib/controllers.js new file mode 100644 index 0000000..5fd9d1e --- /dev/null +++ b/lib/controllers.js @@ -0,0 +1,38 @@ +/** + * Module dependencies. + */ + +var express = require('express'); +var fs = require('fs'); + +module.exports = function(parent, options){ + var verbose = options.verbose; + + fs.readdirSync(__dirname + '/../posts').forEach(function(name){ + + // index.js in controller, with function as pages (views.py for django) + var obj = require('./../posts/' + name + '/index'); + + // urls.js, just like django urls.py + var urls = require('./../posts/' + name + '/urls'); + 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 + '/../posts/' + name + '/views'); + + // generate routes based + // on the exported methods + + verbose && console.log('\t' + name + ':'); + for (var key in urls) { + app.post(key, obj[urls[key]]); + console.log('\t\t' + key + ' -> ' + name + '.' + urls[key]); + } + console.log(); + + // mount the app + parent.use(app); + }); +}; diff --git a/lib/boot.js b/lib/posts.js similarity index 54% rename from lib/boot.js rename to lib/posts.js index 81c59ff..d9b0642 100644 --- a/lib/boot.js +++ b/lib/posts.js @@ -34,32 +34,4 @@ module.exports = function(parent, options){ // mount the app parent.use(app); }); - - fs.readdirSync(__dirname + '/../posts').forEach(function(name){ - - // index.js in controller, with function as pages (views.py for django) - var obj = require('./../posts/' + name + '/index'); - - // urls.js, just like django urls.py - var urls = require('./../posts/' + name + '/urls'); - 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 + '/../posts/' + name + '/views'); - - // generate routes based - // on the exported methods - - verbose && console.log('\t' + name + ':'); - for (var key in urls) { - app.post(key, obj[urls[key]]); - console.log('\t\t' + key + ' -> ' + name + '.' + urls[key]); - } - console.log(); - - // mount the app - parent.use(app); - }); }; diff --git a/server.js b/server.js index 12d5c9e..8ce4bce 100644 --- a/server.js +++ b/server.js @@ -35,13 +35,18 @@ app.use(function(req, res, next) { res.locals.alertCookie = false; } else { res.locals.alertCookie = true; - res.cookie('alreadyCame', true); + res.cookie('alreadyCame', true, {maxAge: 604800000}); // One week in ms } next(); }); // Load controllers -require('./lib/boot')(app, { verbose: !module.parent }); +console.log("Loading controllers :"); +require('./lib/controllers')(app, { verbose: !module.parent }); + +// Load post to log data from user study +console.log("Loading posts :"); +require('./lib/posts')(app, { verbose: !module.parent }); app.use('/static', express.static('static'));