-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
151 lines (129 loc) · 4.29 KB
/
app.js
File metadata and controls
151 lines (129 loc) · 4.29 KB
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
const express = require('express')
const favicon = require('serve-favicon')
const request = require('request')
let app = express()
app.set('view engine', 'pug')
app.set('views', __dirname + '/views')
// redirect http->https
app.enable('trust proxy')
app.use ((req, res, next) => {
if (req.secure || (app.get('env') === 'development')) {
return next()
} else {
res.redirect('https://' + req.headers.host + req.url)
}
})
app.use(express.static('static'))
app.use(favicon(__dirname + '/static/favicons/favicon.ico'))
const availableDatasets = {
'world-2' : "World (nations)",
'se-4': "Sweden (counties)",
'se-7': "Sweden (municipalities)",
'no-4': "Norway (counties)",
'no-7': "Norway (municipalities)",
'fi-8': "Finland (municipalities)",
'us-4': "United States (states)",
'gl-7': "Greenland (municipalities)",
'ch-8': "Switzerland (municipalities)",
'dk-7': "Denmark (municipalities)",
}
app.get('/demo', function(req, res, next) {
if ((req.query.dataset) && (req.query.dataset in availableDatasets)) {
var dataset = req.query.dataset
} else {
var dataset = Object.keys(availableDatasets)[0]
}
if (req.query.date){
var date = new Date(req.query.date).toISOString()
} else {
var date = new Date().toISOString()
}
date = date.split("T")[0]
if (app.get('env') === 'development') {
var apiUrl = `http://localhost:3000/v2/${dataset}/info/${date}`
} else {
var apiUrl = `http://api.thenmap.net/v2/${dataset}/info/${date}`
}
request(apiUrl, function (error, response, body) {
if (error) {
console.log(error)
return next(error)
}
let datasetInfo = JSON.parse(body)
if (req.query.language && (datasetInfo.languages.includes(req.query.language))) {
var activeLanguage = req.query.language
} else {
var activeLanguage = datasetInfo.defaultLanguage
}
let projection = req.query.projection || datasetInfo.recommendedProjections[0]
if (!datasetInfo.recommendedProjections.includes(projection)){
if (!req.query.allow_all){
projection = datasetInfo.recommendedProjections[0]
}
}
res.render('demo',{
availableDatasets: availableDatasets,
activeDataset: dataset,
datasetInfo: datasetInfo,
date: date,
width: req.query.width || "900",
height: req.query.height || "900",
activeDatakey: req.query.dataKey || "",
activeLanguage: activeLanguage,
requestedProjection: projection,
env: app.get('env'),
allow_all: req.query.allow_all || "",
queryString: [
"dataKey="+(req.query.dataKey || ""),
"dataset="+dataset,
"date="+date,
"language="+activeLanguage,
"svg_proj="+projection,
"svg_width="+(req.query.width || "900"),
"svg_height="+(req.query.height || "900")].join("&"),
})
})
})
app.get('/clean', function(req, res) {
res.render('clean',{
activeDataset: req.query.dataset,
date: req.query.date,
width: req.query.svg_width || "900",
height: req.query.svg_height || "900",
activeDatakey: req.query.dataKey || "",
activeLanguage: req.query.language,
requestedProjection: req.query.svg_proj,
})
})
app.get('/svg', function(req, res) {
// DEPRACATED: The API now delivers SVG directly, so this can be removed
let queryString = [
"language="+req.query.language,
"svg_proj="+req.query.svg_proj,
"svg_width="+(req.query.width || "900"),
"svg_height="+(req.query.height || "900")].join("&")
if (app.get('env') === 'development') {
var apiUrl = `http://localhost:3000/v2/${req.query.dataset}/svg/${req.query.date}?${queryString}`
} else {
var apiUrl = `http://api.thenmap.net/v2/${req.query.dataset}/svg/${req.query.date}?${queryString}`
}
request(apiUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.setHeader('Content-Type', 'image/svg+xml')
res.end(body)
}
})
})
app.get('/', function(req, res) {
res.render('index')
})
// Errors
app.use(function(req, res, next) {
res.status(404).send('Sorry, can\'t find that!')
})
//Start server
let server = app.listen(process.env.PORT || 3001, function() {
let host = server.address().address
let port = server.address().port
console.log('Thenmap site listening at http://%s:%s in %s mode', host, port, app.get('env'))
})