-
Notifications
You must be signed in to change notification settings - Fork 56
/
app.js
189 lines (173 loc) · 4.15 KB
/
app.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var express = require('express');
var fs = require('fs');
var Tabletop = require('tabletop');
var _ = require('underscore');
var cons = require('consolidate');
var app = express();
app.locals._ = _;
var getData = require('./getData.js');
var Report;
getData(function(err,results){
Report = results;
});
function checkData(){
if (_.isEmpty(Report)) {
getData(function(err,results){
Report = results;
});
} else {
// The time when we got the data
created = new Date(Report['created']);
// How many seconds ago we got the data
seconds = Math.floor((new Date() - created) / 1000);
console.log('Data is ' + seconds + ' seconds old...')
if (seconds > 20) {
// More than 5 minutes ago
console.log('\nData out of date, getting new data...');
getData(function(err,results){
Report = results;
console.log('We\'ve got data!');
});
}
}
}
app.engine('html', cons.ejs);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(express.static('public'));
// =====
// Routes
// =====
app.get(['/','/category/:id','/story/:id'], function(req, res, next){
if (_.isEmpty(Report)) {
res.render('error/loading', {
title: 'Loading',
partials: {
header: 'partials/header',
footer: 'partials/footer'
}
});
checkData();
} else {
next();
}
});
app.get('/', function(req, res){
res.render('index', {
title: 'Introduction',
type: 'introduction',
url: req.originalUrl,
data: Report,
partials: {
header: 'partials/header',
footer: 'partials/footer'
}
});
});
app.get('/category/:id', function (req, res) {
if (req.params.id == '2014-at-a-glance') {
res.render('timeline', {
title: 'Timeline',
type: 'timeline',
url: req.originalUrl,
requested: req.params.id,
data: Report,
partials: {
header: 'partials/header',
footer: 'partials/footer'
}
});
} else if (req.params.id == 'supporters') {
res.render('supporters', {
title: 'Supporters',
type: 'supporters',
url: req.originalUrl,
requested: req.params.id,
data: Report,
partials: {
header: 'partials/header',
footer: 'partials/footer'
}
});
} else if (req.params.id == 'financials') {
res.render('financials', {
title: 'Financials',
type: 'financials',
url: req.originalUrl,
requested: req.params.id,
data: Report,
partials: {
header: 'partials/header',
footer: 'partials/footer',
charts: 'partials/charts'
}
});
} else if (req.params.id == 'what-you-can-do') {
res.render('closer', {
title: 'What You Can Do',
type: 'closer',
url: req.originalUrl,
requested: req.params.id,
data: Report,
partials: {
header: 'partials/header',
footer: 'partials/footer',
charts: 'partials/charts'
}
});
} else {
res.render('category', {
requested: req.params.id,
type: 'category',
url: req.originalUrl,
data: Report,
partials: {
header: 'partials/header',
footer: 'partials/footer'
}
});
}
});
app.get('/story/:id', function (req, res) {
res.render('story', {
requested: req.params.id,
type: 'story',
url: req.originalUrl,
data: Report,
partials: {
header: 'partials/header',
footer: 'partials/footer'
}
});
});
// Get a random story, refresh it every X seconds
app.get('/random', function (req, res) {
res.render('random', {
type: 'random',
url: req.originalUrl,
data: Report,
partials: {
header: 'partials/header',
footer: 'partials/footer'
}
});
});
app.get('/update', function (req, res) {
checkData();
res.send('Updating data...');
});
// =====
// Error handling
// =====
// Catch 404
app.use(function(req, res, next) {
res.status(404).render('error/404', {
url: req.originalUrl,
});
});
// Catch 500
app.use(function(error, req, res, next) {
res.status(500).render('error/500', {});
});
app.listen(process.env.PORT || 3000);
console.log('Express server listening on port 3000');