-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·87 lines (76 loc) · 2.43 KB
/
server.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
var express = require('express');
var bodyParser = require('body-parser');
var jsonld = require('jsonld');
var url = require('url');
var got = require('got');
var app = express();
const realtime = "http://datatank.stad.gent/4/mobiliteit/bezettingparkingsrealtime.json";
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
// create application/json parser
var jsonParser = bodyParser.json();
app.get('/parking', function (req, res) {
var json = req.query.json;
var parsedJson;
var errors;
var occupied;
var total;
var free;
if (typeof json !== 'undefined') {
parsedJson = JSON.parse(json);
errors = validateJsonld(parsedJson);
occupied = parsedJson['dtx:parkingSpaceOccupied'];
total = parsedJson['dtx:totalCapacity'];
free = total - occupied;
res.render('index',{ title : 'Home', data: parsedJson, freeSpace: free, errors : errors });
} else {
got(realtime).then(response => {
let data = JSON.parse(response.body);
res.render('empty', { title: 'Home', data: data});
});
}
});
function validateJsonld(json) {
var errors = {};
if (json["@context"] == null) {
errors.context = "No context was found";
}
if (json["@id"] == null) {
errors.id = "No id was found";
}
if (json["@type"] == null) {
errors.type = "No type was found";
}
if (json["dtx:parkingName"] == null) {
errors.name = "The parkingName was not found";
}
if (json["dtx:parkingSiteAddress"] == null) {
errors.address = "The parkingSiteAddress was not found";
}
if (json["dtx:parkingLocation"] == null) {
errors.location = "The parkingLocation was not found";
}
if (json["dtx:contactDetailsTelephoneNumber"] == null) {
errors.telephone = "The contactDetailsTelephoneNumber was not found";
}
if (json["dtx:parkingDescription"] == null) {
errors.description = "The parkingDescription was not found";
}
if (json["dtx:parkingSpaceOccupied"] == null) {
errors.spaceOccupied = "The parkingSpaceOccupied was not found";
}
if (json["dtx:totalCapacity"] == null) {
errors.capacity = "The totalCapacity was not found";
}
if (json["dtx:latitude"] == null) {
errors.latitude = "The latitude was not found";
}
if (json["dtx:longitude"] == null) {
errors.longitude = "The longitude was not found";
}
return errors;
}
app.listen(8080, function () {
console.log('Example app listening on port 8080!');
});