-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
109 lines (84 loc) · 2.99 KB
/
app.js
File metadata and controls
109 lines (84 loc) · 2.99 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
var express = require('express');
var http = require('http');
var app = express();
var map_util = require('./maputils/index.js');
app.use(express.static(__dirname + "/public"));
app.use("/packages", express.static(__dirname + "/bower_components"));
app.use(express.bodyParser());
app.use("views", express.static(__dirname + "/views"));
app.use(app.router);
app.use(function(err, req, res, next){
res.status(500);
res.render("error.html", {error: err});
});
app.engine('html', require('ejs').renderFile);
/*app.get('/', function(req, res){
res.render('login.html');
});
app.post('/login', function(req, res){
if(req.body.username !== 'richmond804' && req.body.password !== 'this is for code for america'){
res.render('error.html');
}else{
res.redirect('/form');
}
});*/
app.get('/', function(req, res){
res.render('index.html');
});
app.post('/find_address', function(req, res, next){
var app_res = res;
var app_req = req;
var app_next = next;
var myLocation, nearestBusStop, nearestBusStopAddress;
var routes_coordinates = [];
var school = req.body.school;
if(req.body.address.match(/[ /](richmond|Richmond),[ /](va|VA)/i)) {
var address = req.body.address;
}else{
var address = req.body.address + ' ,Richmond, VA';
}
var clientHours = req.body.hours;
console.log(clientHours);
//we need to geocode the user's address.
var geoAddressData= '';
var geocodeAPI = "http://www.datasciencetoolkit.org/maps/api/geocode/json?sensor=false&address=" + encodeURIComponent(address);
var geoAddress = http.get(geocodeAPI, function(res){
res.on('data', function(chunk){
geoAddressData += chunk;
});
res.on('end', function(){
geoAddressData = JSON.parse(geoAddressData);
myLocation = [geoAddressData.results[0].geometry.location.lat,
geoAddressData.results[0].geometry.location.lng];
map_util.findNearestBusStop(geoAddressData, clientHours, school, function(err, data){
if(err){
var error = err;
console.log(err);
app_next(error);
}else if(data.length == 0){
app_res.render('no_results.html');
}else{
console.log(data[0]);
nearestBusStop = map_util.normalizeLatLng(data[0].loc);
nearestBusStopAddress = data[0].address;
//then get the route
//but first let's see if we get the morning or afternoon route
console.log("the hours is " + clientHours);
var route_num = data[0].route_num;
map_util.getRoute(route_num, function(err, data){
console.log(data);
routes_coordinates = data;
for(var i = 0; i < routes_coordinates.length; i++){
routes_coordinates[i].loc = map_util.normalizeLatLng(routes_coordinates[i].loc);
}
app_res.render('results.html', {myBusStop: nearestBusStop, nearestBusStopAddress: nearestBusStopAddress, myLocation: myLocation, routes_coordinates: routes_coordinates});
});
}
});
});
}).on('error', function(e){
console.log(e);
app_next(new Error("Could not connect to geocoding service!"));
});
});
app.listen(10817);