-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
84 lines (63 loc) · 2.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
var adaro = require('adaro'),
rules = require('./specialization'),
specializer = require('karka').create(rules),
dust = require('dustjs-linkedin'),
engine = 'dust',
fs = require('fs'),
path = require('path'),
express = require('express'),
app = express(),
cookieParser = require('cookie-parser'),
session = require('express-session'),
extend = require('util-extend');
app.use(cookieParser('keyboard cat'));
app.use(session({
secret: 'keyboard cat',
"cookie": {
"path": "/",
"httpOnly": true,
"maxAge": null
}
}));
app.use(express.static(__dirname + '/public'));
app.set('views','public/templates');
app.set('view engine', engine);
//Step 1: Decorate the engine that you feed into
//express app to first do the specialization map generation
function specialize(engine) {
return function(name, context, callback) {
//Step 2: Generate the specialization map on each render
//for that context
context._specialization = specializer.resolveAll(context);
engine.apply(null, arguments);
};
}
app.engine('dust', specialize(adaro[engine]({cache:false})));
//app.engine('dust', specialize(consolidate[engine]));
//Step 3: Use the hook in the rendering engine to switch
// the templates that are present in the map
dust.onLoad = function(name, context, cb) {
var specialization = context.get('_specialization');
fs.readFile(constructFilePath( specialization && specialization[name] || name), 'utf8', cb);
};
app.listen(8000, function (err) {
console.log('[%s] Listening on http://localhost:%d', app.settings.env, 8000);
});
app.get('/', function(req, res){
res.render('index', req.session && req.session.custom);
});
app.get('/setCustom', function(req, res) {
if(!req.session.custom) {
req.session.custom = {};
}
extend(req.session.custom, req.query);
res.redirect('/');
});
function constructFilePath(name) {
var p;
if (name.indexOf(app.get('views')) === -1) {
p = path.join(app.get('views'), name);
}
return p + '.' + engine;
}