forked from Flugmango/sc16-ephemeral-lbs-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (119 loc) · 3.5 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
var express = require('express');
var bodyParser = require("body-parser");
var Validator = require('jsonschema').Validator;
var zoneModel = require('./models/zonemodel');
var msgModel = require('./models/msgmodel');
var schemata = require('./schemata');
/* Change accordingly. */
var webport = 8080;
/* Initialize the JSON validator */
var validator = new Validator();
/*
* We set up the server to serve static files from /public,
* in case it should deliver static files.
*/
var server = express();
server.use(express.static('./public'));
/* This adds CORS-string into the header of each response. */
server.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
next();
});
/* This configures body-parser. */
server.use(bodyParser.urlencoded({
extended: false
}));
server.use(bodyParser.json());
server.get('/api/zones', function(req, res) {
zoneModel.getZones(function(err, data) {
if (err) {
res.status(err.status).send(err);
} else {
res.json(data);
}
})
});
server.get('/api/zones/:zoneid', function(req, res) {
zoneModel.getZoneById(req.params.zoneid, function(err, data) {
if (err) {
res.status(err.status).send(err);
} else {
res.json(data);
}
})
});
server.post('/api/addzone', function(req, res) {
let vresult = validator.validate(req.body, schemata.zone);
if (!vresult.valid) {
res.status(404).send({
status: 404,
error: 'Validation error:' + vresult.errors
});
return;
}
zoneModel.addZone(req.body, function(err, msg) {
if (err) {
res.status(err.status).send({ err });
} else {
res.status(201).send(msg);
}
})
});
server.get('/api/zones-search', function(req, res) {
if (!req.query.q) {
res.status(404).send({ status: 404, error: "Query parameter 'q' missing." });
return;
}
let search_string = req.query.q.toLowerCase();
zoneModel.searchZones(search_string, function(err, data) {
if (err) {
res.status(err.status).send(err);
} else {
res.json(data);
}
})
});
server.get('/api/zones/:zoneid/dailyactivity', function(req, res) {
zoneModel.getDailyActivity(req.params.zoneid, function(err, data) {
if (err) {
res.status(err.status).send(err);
} else {
res.json(data);
}
})
});
server.get('/api/messages', function(req, res) {
if (!req.query.zone) {
res.status(404).send({ status: 404, error: "Zone parameter missing." })
} else {
msgModel.getMessages(req.query.zone, function(err, data) {
if (err) {
res.status(err.status).send(err);
} else {
res.json(data);
}
})
}
});
server.post('/api/addmessages', function(req, res) {
let vresult = validator.validate(req.body, schemata.messages);
if (!vresult.valid) {
res.status(404).send({
status: 404,
error: 'Validation error:' + vresult.errors
});
return;
}
msgModel.addMessages(req.body.Messages, function(err, msg) {
if (err) {
res.status(err.status).send({ err });
} else {
res.status(201).send(msg);
}
})
});
/* We start the server from the specified port. */
server.listen(webport, function() {
console.log('Smart Cities SS16 server now running on port ' + webport);
});