-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
134 lines (124 loc) · 3.42 KB
/
server.js
File metadata and controls
134 lines (124 loc) · 3.42 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
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
133
134
'use strict'
// This is an example HTTP server for the WMATA API wrapper.
const http = require('http')
let ENV
try { // look for local environment variable file first
ENV = require('./env')
} catch (LocalEnvFileNotFound) {
ENV = process.env
}
const Wmata = require('./captran-wmata-dev')
let wmata = new Wmata({ apiKey: ENV.WMATA_KEY, debugMode: true })
// const CapTran = require('./captran')
// var captran = new CapTran()
const RideOn = require('./captran-rideon')
let rideon = new RideOn({ apiKey: ENV.RIDEON_KEY, debugMode: true })
const Gbfs = require('./captran-gbfs')
let cabi = new Gbfs({
gbfsUrl: 'https://gbfs.capitalbikeshare.com/gbfs/gbfs.json'
})
//
// const Transit = require('transportation')
// let transit = new Transit()
//
// const wmataGtfsUrl = 'https://lrg.wmata.com/GTFS_data'
//
// // import GTFS data
// transit.importGTFS(wmataGtfsUrl, function (err) {
// // have a look at the Transit instance
// if (err) console.error(err)
// else console.log(transit)
// })
let server = http.createServer((request, response) => {
const respondWithJson = (object) => {
response.writeHead(200, {'Content-Type': 'application/json'})
response.end(JSON.stringify(object))
}
function respond404 () {
response.writeHead(404, {'Content-Type': 'text/html'})
response.end('Not found.')
}
let reqUrl = request.url.split('/')
let indexHtml = `Hello, world!
<script src="/primus/primus.js"></script>
<script>
// connect to current url
var primus = Primus.connect()
primus.on('open', function() {
console.log('connected!')
setTimeout(function() {
console.log('testing')
primus.write('routes')
}, 1000)
})
primus.on('data', function(data) {
console.log('data:', data)
})
</script>
`
switch (reqUrl[1]) {
case '':
response.writeHead(200, {'Content-Type': 'text/html'})
// response.end('Hello, world!')
response.end(indexHtml)
break
case 'ping':
respondWithJson({ 'body': 'pong' })
break
case 'busPositions':
case 'buses':
wmata.query({
api: 'busPositions'
}).then(respondWithJson, console.error)
break
case 'routes':
case 'busRoutes':
wmata.query({
api: 'routes'
}).then(respondWithJson, console.error)
break
case 'route':
wmata.query({
api: 'routeDetails',
RouteID: '7Y'
}).then(respondWithJson, console.error)
break
case 'lafeyette':
wmata.query({
api: 'stopPredictions',
StopID: '1001141'
}).then(respondWithJson, console.error)
break
case 'pentagon':
if (reqUrl[2] === 'stops')
wmata.query({
api: 'stops',
Lat: 38.8690011,
Lon: -77.0544217,
Radius: 500
}).then(respondWithJson, console.error)
else if (reqUrl[2] === 'buses')
wmata.query({
api: 'busPositions',
Lat: 38.8690011,
Lon: -77.0544217,
Radius: 500
}).then(respondWithJson, console.error)
else respond404()
break
case 'rideon':
rideon.query({
api: 'vehiclePositions'
}).then(respondWithJson, console.error)
break
case 'wh':
cabi.getStationsNear('en', 38.8977, -77.0365, 1)
.then(respondWithJson, console.error)
break
// case 'transit':
// respondWithJson(JSON.parse(transit))
// break
default: respond404()
}
})
module.exports = server