-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
80 lines (72 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
'use strict';
var request = require('request');
var http = require('http');
http.createServer(function (serverRequest, serverResponse) {
var url = require('url');
var url_parts = url.parse(serverRequest.url, true);
var query = url_parts.query;
var pathName = url_parts.pathname;
var mode = query.mode;
var finalUrl = "";
if (pathName == "/places") {
finalUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
finalUrl += "location=" + query.location + "&";
finalUrl += "radius=" + query.radius + "&";
finalUrl += "type=" + query.type + "&";
finalUrl += "keyword=" + query.keyword + "&";
finalUrl += "key=AIzaSyCezipVJkYSdRmEtwdg37OEgW7_fODwvSU";
}
else if (pathName == "/geocode") {
finalUrl = "https://maps.googleapis.com/maps/api/geocode/json?";
finalUrl += "address=" + query.location + "&";
finalUrl += "key=AIzaSyCezipVJkYSdRmEtwdg37OEgW7_fODwvSU";
}
else if (pathName == "/nextpage") {
finalUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
finalUrl += "pagetoken=" + query.nextPageToken + "&";
finalUrl += "key=AIzaSyCezipVJkYSdRmEtwdg37OEgW7_fODwvSU";
}
else if (pathName == "/details") {
finalUrl = "https://maps.googleapis.com/maps/api/place/details/json?";
finalUrl += "placeid=" + query.placeId + "&";
finalUrl += "key=AIzaSyCezipVJkYSdRmEtwdg37OEgW7_fODwvSU";
}
else if (pathName == "/yelpmatch") {
finalUrl = "https://api.yelp.com/v3/businesses/matches/best?";
finalUrl += "name=" + query.name + "&";
finalUrl += "city=" + query.city + "&";
finalUrl += "state=" + query.state + "&";
finalUrl += "country=" + query.country + "&";
finalUrl += "address1=" + query.address1 + "&";
finalUrl += "postal_code=" + query.postalCode;
}
else if (pathName == "/yelpreviews") {
finalUrl = "https://api.yelp.com/v3/businesses/";
finalUrl += query.id + "/reviews";
}
Run(serverRequest, serverResponse, finalUrl, pathName);
}).listen(process.env.PORT || 3000);
function Run(serverRequest, serverResponse, finalUrl, pathName) {
var headers = { 'User-Agent': 'request' };
if (pathName == "/yelpmatch" || pathName == "/yelpreviews") {
headers = { 'Authorization': 'Bearer QkGxQZuSrClCGs2pREeNigSVT0f5OuWY9PuVoLfJyiD4OtCWB1wEWjeJ2HKVlAv65oXaozuboCBSQtO7vFHIWjCBO9mxgYF8il3M52cuFZZKMYTsm1wg3XIMp3CzWnYx' };
}
var options = {
url: finalUrl,
json: true,
headers: headers
}
request.get(options, function (error, apiResponse, apiData) {
serverResponse.setHeader('Access-Control-Allow-Origin', '*');
serverResponse.setHeader('Content-Type', 'application/json');
if (error) {
serverResponse.end(JSON.stringify({ 'Error': error }));
}
else if (apiResponse.statusCode !== 200) {
serverResponse.end(JSON.stringify({ 'Error': apiResponse.statusCode }));
}
else {
serverResponse.end(JSON.stringify(apiData));
}
});
}