-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (50 loc) · 1.47 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
const express = require('express');
const app = express();
const path = require('path');
const device = require('express-device');
const https = require('https');
const xss = require('xss');
let items = [];
const itemsPath = "https://raw.githubusercontent.com/Connect-To-Care/items/master/items.json";
const exphbs = require('express-handlebars');
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
app.use(device.capture());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
// Jank but Rushil wanted to do this
if (req.device.type === 'phone') {
res.render('index_mobile', {
items,
layout: false
});
} else {
res.render('index', {
items,
layout: false
});
}
});
setTimeout(() => update(), 1000 * 60 * 60 * 2);
const update = () => {
https.get(itemsPath, function (res) {
let body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
items = JSON.parse(body).map(item => {
return {
name: xss(item.name),
img: xss(item.img)
}
});
console.log("Got " + items.length + " items.")
});
}).on('error', function (e) {
console.log("Got an error while grabbing files: ", e);
process.exit(1);
});
};
update();
app.listen(3001);