-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·74 lines (64 loc) · 2.83 KB
/
app.js
File metadata and controls
executable file
·74 lines (64 loc) · 2.83 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
const polyline = require("@mapbox/polyline");
const fetch = require("node-fetch");
const express = require("express");
const app = express();
const hostname = "127.0.0.1";
const port = 3000;
const otpHost = 'localhost:8080';
let time = [];
let legInfo = [];
//Body Parser middleware
app.use(express.json());
//Post request that will handle returning the json of route info
app.post('/', (req, res) => {
console.log(req.body);
let url = urlCreator(req.body)
console.log(url);
fetch(url).then( async (response) => {
let body = await response.json();
res.send(jsonParsing(body.plan, body.plan.itineraries[0].legs));
});
});
//This fetch would be a seperate function called from the urlCreator with the created url
// fetch(
// "http://localhost:8080/otp/routers/default/plan?fromPlace=25.863925,-80.331163&toPlace=25.773868,-80.336200&time=6:54pm&date=2-12-2020&mode=TRANSIT,WALK&maxWalkDistance=500&arriveBy=false"
// )
// .then(res => res.json())
// .then(body => jsonParsing(body.plan, body.plan.itineraries[0].legs))
// .catch(err => console.log(err));
//Function to create the URL to make the call to the OTP API
function urlCreator(reqBody) {
fromPlace = reqBody.fromPlace;
toPlace = reqBody.toPlace;
startTime = reqBody.startTime;
startDate = reqBody.startDate;
return url = 'http://' + otpHost + '/otp/routers/default/plan?fromPlace=' + fromPlace + '&toPlace=' + toPlace + '&time=' + startTime + '&date=' + startDate + '&mode=TRANSIT,WALK&maxWalkDistance=500&arriveBy=false';
}
//Function that will parse the api call and return the important stuff
function jsonParsing(jsonData, jsonLegData) {
time.push({
walkingTime : jsonData.itineraries[0].walkTime,
transitTime : jsonData.itineraries[0].transitTime,
waitingTime : jsonData.itineraries[0].waitingTime,
start : jsonData.itineraries[0].startTime,
end : jsonData.itineraries[0].endTime,
transfers : jsonData.itineraries[0].transfers,
});
for (j=0; j < jsonLegData.length; j++) {
legInfo.push({ currentLeg:j + 1,
transitMode:jsonLegData[j].mode,
legDuration : (jsonLegData[j].endTime - jsonLegData[j].startTime) / 1000,
departurePlace : jsonLegData[j].from.name,
departureTime : jsonLegData[j].from.departure,
arrivalPlace : jsonLegData[j].to.name,
arrivalTime : jsonLegData[j].to.arrival,
legPolyline : decodeGeometry(jsonLegData[j].legGeometry.points)});
}
return {time, legInfo};
}
function decodeGeometry(encoded) {
//returns an array of long and lat each element corresponding to a point
let decoded = polyline.decode(encoded);
return decoded;
}
app.listen(port, () => console.log(`App listening on port ${port}!`))