-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (90 loc) · 2.97 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
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
// DEPENDENCIES
const express = require("express")
const csv = require('csv-parser')
const fs = require('fs')
const path = require('path');
const moment = require('moment')
// CONFIGURATION
const port = 1025
const csvFilename = path.resolve(__dirname, 'all.csv')
const lastUpdatedFile = path.resolve(__dirname, 'last-updated.txt')
const pageTitle = 'Corona Abstrichtest'
// Set up Express
const app = express()
// Use Pug for rendering
app.set('view engine', 'pug')
// Make Bootstrap CSS and JavaScript available
app.use(express.static('node_modules/bootstrap/dist'))
app.use('/js', express.static('node_modules/@popperjs/core/dist/umd'))
app.use('/js', express.static('node_modules/jquery/dist'))
// Determines the common template variables that are provided for each route.
var getTemplateVariables = function (variables = {}) {
const commonVariables = {
'lastUpdated': lastUpdatedDate,
lastModifiedAbsolute: absoluteModifiedDate,
lastModifiedRelative: moment(absoluteModifiedDate).locale('de').fromNow(),
title: variables.title ? pageTitle + ': ' + variables.title : pageTitle,
}
return Object.assign({}, variables, commonVariables)
}
app.get('/', function (req, res) {
res.render('unable-to-parse', getTemplateVariables())
})
app.get('/suche', function (req, res) {
const regularExpression = /([^\/]+)/i
const match = regularExpression.exec(req.query.code)
const code = match ? match[1] : ""
if (code.length == 0) {
res.redirect('/')
return
}
const rows = csvRows.filter(function(row) {
return row['Code'].toLowerCase().includes(code.toLowerCase())
})
res.render('search-results', getTemplateVariables({
code: code,
rows: rows,
title: 'Suchergebnisse',
}))
})
app.get('/reload', function (req, res) {
reloadCSVData()
res.redirect('/?reloaded')
});
app.get('/alle-daten', function (req, res) {
res.render('all-data', getTemplateVariables({
rows: csvRows,
activeModule: 'all-data',
title: 'Alle Daten',
}))
});
app.get('/hintergrund', function (req, res) {
res.render('about', getTemplateVariables({
activeModule: 'about',
title: 'Hintergrund',
}))
});
// Current CSV data
var csvRows = [];
var lastUpdatedDate;
var absoluteModifiedDate;
var reloadCSVData = function() {
const parsedRows = []
fs.createReadStream(csvFilename)
.pipe(csv())
.on('data', (data) => parsedRows.push(data))
.on('end', () => {
csvRows = parsedRows
// Read the 'last updated' date
lastUpdatedDate = fs.readFileSync(lastUpdatedFile, { encoding: 'utf8', flag: 'r' });
// Check when the CSV file was last modified, since this is the date at which the Cronjob last run.
var stats = fs.statSync(csvFilename)
absoluteModifiedDate = stats.mtime
console.log('Data was last updated at', absoluteModifiedDate)
console.log('CSV data reloaded successfully')
});
};
app.listen(port, () => {
reloadCSVData()
console.log(`Covid test result app listening at http://localhost:${port}`)
})