var path = require('path'); var fs = require('fs'); var cons = require('consolidate'); var model; exports.setModel = function(m) { model = m; }; exports.start = function(config) { var app = config.app; app.engine('html', cons.swig); // set .html as the default extension app.set('view engine', 'html'); app.set('views', path.join(__dirname, config.rootDir)); app.get('/slides/:id', function(req, res) { slideset = req.params.id; model.getSlides(slideset, function(err, slides) { if(err) { throw err; } var slidesAr = []; for(var slide in slides['slides']) { if(!slides['slides'].hasOwnProperty(slide)) { continue; } if(slides['slides'][slide]['type'] === 'image') { slidesAr.push({ image: '/' + slides['slides'][slide]['file'] }); } if(slides['slides'][slide]['head'].length > 0) { slidesAr[slidesAr.length - 1]['description'] = slides['slides'][slide]['head']; if(slides['slides'][slide]['text'].length > 0) { slidesAr[slidesAr.length - 1]['text'] = slides['slides'][slide]['text']; } } } var eventAr = []; for(var date in slides['events']) { if(!slides['events'].hasOwnProperty(date)) { continue; } eventAr.push({ date: date, events: [] }); for(var ev in slides['events'][date]) { if(!slides['events'][date].hasOwnProperty(ev)) { continue; } var eventObj = {}; for(var evF in slides['events'][date][ev]) { if(!slides['events'][date][ev].hasOwnProperty(evF)) { continue; } if(typeof slides['events'][date][ev][evF] == 'string') { eventObj[evF] = slides['events'][date][ev][evF]; } } eventAr[eventAr.length - 1]['events'].push(eventObj); } } res.render(path.join(slideset, config.templateFile), { slides: slidesAr, events: eventAr }); }); }); app.get('*', function(req, res){ model.getAllSlides(function(slidesets) { res.render(config.listTemplate, { slides: slidesets }); }); }); app.listen(config.port); };