This repository has been archived by the owner on Apr 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 440
/
server.js
85 lines (72 loc) · 1.96 KB
/
server.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
85
var express = require('express'),
swig = require('../../index'),
http = require('http'),
app = express(),
people;
// NOTE: It is preferred to use consolidate.js
// However, we can't do that in this example, because the example uses
// The uninstalled version of swig for testing purposes
// Please see the documentation for proper use with Express
swig._cache = {};
swig.express3 = function (path, options, fn) {
swig._read(path, options, function (err, str) {
if (err) {
return fn(err);
}
try {
options.filename = path;
var tmpl = swig.compile(str, options);
fn(null, tmpl(options));
} catch (error) {
fn(error);
}
});
};
swig._read = function (path, options, fn) {
var str = swig._cache[path];
// cached (only if cached is a string and not a compiled template function)
if (options.cache && str && typeof str === 'string') {
return fn(null, str);
}
// read
require('fs').readFile(path, 'utf8', function (err, str) {
if (err) {
return fn(err);
}
if (options.cache) {
swig._cache[path] = str;
}
fn(null, str);
});
};
app.engine('html', swig.express3);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('view options', { layout: false });
app.set('view cache', false);
swig.init({
root: __dirname + '/views',
allowErrors: true
});
app.get('/', function (req, res) {
res.render('index', {});
});
app.get('/people', function (req, res) {
res.render('people', { people: people });
});
app.get('/people/:id', function (req, res) {
res.render('person', { person: people[req.params.id] });
});
app.get('/variable', function (req, res) {
res.render('variable', { extendFile : 'outer.html' });
});
app.get('/*', function (req, res) {
res.render(req.params[0], {});
});
people = [
{ name: 'Paul', age: 28 },
{ name: 'Jane', age: 26 },
{ name: 'Jimmy', age: 45 }
];
app.listen(1337);
console.log('Application Started on http://localhost:1337/');