/
app.js
154 lines (129 loc) · 4.72 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
var express = require('express');
var ejs = require('ejs');
var app = express();
var path = require('path');
var api = require('./services/APIService');
const Cookies = require('cookies');
const compression = require('compression');
var dotenv = require('dotenv').config();
var allData = require('./public/data/allData');
// app.get('/', function (req, res) {
// res.sendFile(__dirname + '/public/index.html');
// });
app.use(compression());
app.set('view engine', 'ejs');
app.set("views", "express")
app.use(function(req, res, next) {
const trailingSlashRegExp = new RegExp("\\/(\\?.+)?$");
if(req.url.length > 1 && req.url.match(trailingSlashRegExp))
res.redirect(301, req.url.replace(trailingSlashRegExp,"$1"));
else
next();
});
// Temporary forcing all visitors to be redirected to /students
// except those who visited /beta
// app.get('/beta', function (req, res , next) {
// const cookies = new Cookies( req, res );
// const dn = new Date();
// cookies.set("access","all",{expires:new Date(dn.setDate(dn.getDate()+1))}); // 1 day expiration
// res.redirect('/')
// });
//
// app.get('/flush', function (req, res , next) {
// const cookies = new Cookies( req, res );
// cookies.set("access"); // deleting the cookie
// res.redirect('/');
// });
//
// app.use(function(req, res, next) {
// const cookies = new Cookies( req, res );
// if(cookies.get("access")==="all"){
// next();
// } else {
// if(req.url==='/'){
// res.redirect("/students")
// } else {
// next();
// }
// }
// });
app.get('/', function(req, res, next) {
res.redirect(301,"/quiz");
// res.render('index', { standalone: false, embed: false, brand: process.env.SITE_BRAND || 'ge2017', step: '', phrase: '', quiz: false });
});
app.get('/students/', function(req, res, next) {
res.render('index', { standalone: true, embed: false, brand: process.env.SITE_BRAND || 'ge2017', step: 'postcode-compare', phrase: '', quiz: false });
});
//This doesn't work yet - redirect??? - Igor: this doesn't work, because there is no "req.params.step" in URL
app.get('/student/', function(req, res, next) {
res.redirect(301,"/students");
//res.render('/index', { standalone: true, embed: false, brand: process.env.SITE_BRAND || 'ge2017', step: req.params.step, phrase: '', quiz: false });
});
app.get('/embed/:step', function(req, res, next) {
res.render('index', { standalone: false, embed: true, brand: process.env.SITE_BRAND || 'ge2017', step: req.params.step, phrase: '', quiz: false });
});
app.get('/policy', function(req, res, next) {
res.render('index', { standalone: true, embed: false, brand: process.env.SITE_BRAND || 'ge2017', step: 'policy', phrase: '', quiz: false });
});
app.get('/shared/:party?/:percentage?', function(req, res, next) {
if(req.params.party && req.params.party.includes("-and-")) {
req.params.party = req.params.party.split("-and-").join(" and ");
req.params.equally = "equally ";
} else {
req.params.equally = "";
}
res.render('index', { standalone: true, embed: false, brand: process.env.SITE_BRAND || 'ge2017', step: 'quiz', phrase: '', quiz: true, params: req.params });
})
app.get('/quiz', function(req, res, next) {
res.render('index', { standalone: true, embed: false, brand: process.env.SITE_BRAND || 'ge2017', step: 'quiz', phrase: '', quiz: true });
});
app.get('/quiz/questions', function(req, res, next) {
res.redirect(301,"/quiz");
});
//Temporary for dev testing
app.get('/results', function(req, res, next) {
res.redirect(301,"/quiz");
});
app.get('/phrase/iWantTo/', function(req, res, next) {
res.render('index', { standalone: true, embed: false, brand: process.env.SITE_BRAND || 'ge2017', step: '', phrase: 'iWantTo', quiz: true });
});
//Teporary because AddThis was sending people to the wrong place!
app.get('/ge2017.com', function(req, res, next) {
res.redirect(301,"/students");
});
app.get('/api/postcode/:postcode', function(req, res, next) {
api.getContenders(req.params.postcode)
.then(function(result) {
res.json({test: 'test', result: result});
})
});
app.get('/api/constituency/:id', function(req, res, next) {
// api.
api.getContenders(req.params.id)
.then(function(result) {
res.json({test: 'test', result: result});
})
});
/*
This is temporary to make sure we do not see the
ugly 404 page if we accidentally refresh the page
*/
app.get('/:page/*', function (req, res , next) {
if(
req.params.page==="dashboards"
||
req.params.page==="steps"
||
req.params.page==="phrase"
){
res.redirect("/",301);
} else {
next();
}
});
app.use(express.static(path.join(__dirname, 'public')));
const port = process.env.PORT || 1234;
app.listen(port, function () {
console.log('Listening on port '+port+'!');
});
module.exports = app;