-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
110 lines (98 loc) · 3.1 KB
/
server.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
'use strict';
//RESTful api call for lyft to get new OAuth
var superagent = require('superagent');
//RESTful api call for uber & lyft & biketown
var express = require('express'),
requestProxy = require('express-request-proxy'),
port = process.env.PORT || 3000,
app = express();
//RESTful api call for uber
function proxyUber(request, response) {
console.log('Routing Uber request for ');
console.log('the request is ', request.params);
console.log('the response is ', response.params);
(requestProxy({
url: 'https://api.uber.com/v1/estimates/time',
query: {
start_latitude: request.params[0].split('/')[0],
start_longitude: request.params[0].split('/')[1]
},
headers: {
Authorization: 'Token ' + process.env.UBER_TOKEN
}
}))(request, response);
};
//RESTful api call for lyft if valid OAuth token
function proxyLyft(request, response) {
var bearerToken;
// console.log('Routing Lyft request for ');
console.log('the requst is ', request.params);
// console.log('the response is ', response.params);
process.env.TOKENWHATEVER='hello';
console.log(process.env.BEARER_TOKEN);
(requestProxy({
url: 'https://api.lyft.com/v1/eta',
query: {
lat: request.params[0].split('/')[0],
lng: request.params[0].split('/')[1]
},
headers: {
authorization: 'Bearer ' + request.params[0].split('/')[2]
}
}))(request, response);
}
//RESTful api call for new lyft OAuth token
function proxyLyftGetAuth(request, response) {
console.log(request.params);
//get new OAuth token
superagent
.post('https://api.lyft.com/oauth/token')
.send({grant_type: 'client_credentials'})
.set('authorization', 'Basic ' + process.env.LYFT_TOKEN)
.set('content-type', 'application/json')
.end(function(err, res){
if (err) {
console.log('error');
} else {
console.log('success');
var token = res.body.access_token;
//new api request
superagent
.get('https://api.lyft.com/v1/eta')
.query({lat:request.params[0].split('/')[0]})
.query({lng:request.params[0].split('/')[1]})
.set('authorization','Bearer ' + token)
.end(function(err, resp) {
if (err) {
console.log('error');
response.end();
} else {
console.log('further success');
console.log(resp);
response.json(resp);
};
}
);
};
});
};
//RESTful api call for biketown
function proxyBikeTown(request, response) {
console.log(request.params);
(requestProxy({
url: 'http://biketownpdx.socialbicycles.com/opendata/station_information.json'
}))(request, response);
};
//routes
app.get('/uber/*', proxyUber);
app.get('/lyft/*', proxyLyft);
app.post('/tokenlyft/*', proxyLyftGetAuth);
app.get('/biketown/*', proxyBikeTown);
app.use(express.static('./'));
app.get('*', function(request, response) {
console.log('New request:', request.url);
response.sendFile('index.html', {root: '.'});
});
app.listen(port, function() {
console.log('Server started on port ' + port + '!');
});