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
|
var express = require('express');
var router = express.Router();
var Mockpoints = require('../models/mockpoints');
var Points = require('../models/points');
var fs = require("fs");
var GeoJsonGeometriesLookup = require('geojson-geometries-lookup');
var torontoNeighbourhoords = JSON.parse(fs.readFileSync(__dirname + '/../../data/Neighbourhoods.geojson', "utf8"));
const connections = [];
const glookup = new GeoJsonGeometriesLookup(torontoNeighbourhoords);
/* GET home page. */
router.get('/', function(req, res, next) {
// for now just redirect to /map
res.redirect('/map');
});
/* GET Map page. */
router.get('/map', function(req,res) {
// load the map with all the points
// Mockpoints.get_points(function(err, points){
Points.get_points(function(err, points){
res.render('map', {
lat : 43.665234,
lng : -79.383370,
points: points
});
});
});
router.post('/mobilerequest', function(req, res) {
var point = req.body;
if (!point.coordinates.length) {
var errMssg = "Coordinates field must not be empty in a request.";
return res.status(422).send({ error: errMssg });
}
if (!glookup.hasContainers(point)) {
var errMssg = "Sorry, we currently only support locations strictly within the City of Toronto.";
return res.status(422).send({ error: errMssg });
}
// here we are guaranteed to have a valid point in Toronto
Points.save_request(point, function(err, result) {
if (err) {
console.log(`err inserting mobile request into db: ${err}`);
res.status(500).send({ error: "Sorry, something failed on our end." });
} else {
// send event to all connections
for(var i = 0; i < connections.length; i++) {
connections[i].sseSend(point);
}
console.log(result);
res.status(201).send({'status': 'success'});
}
});
});
router.get('/stream', function(req, res){
// set up server side event (communication line between front end and server)
res.sseSetup();
// send an event to the front end to open the connection
res.sseSend('ok');
// push connection to connection array
connections.push(res);
});
module.exports = router;
|