-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·120 lines (100 loc) · 3.48 KB
/
server.js
File metadata and controls
executable file
·120 lines (100 loc) · 3.48 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
/*--------------- Express Server Setup ----------------*/
require('dotenv').config();
var path = require('path');
var express = require('express');
var app = express();
var PORT = process.env.PORT || 8080
var fs = require('fs');
var https = require('https');
var http = require('http');
/* ----- Password Protection ------ */
// const auth = require('./auth');
// app.use(auth);
const googleMapsClient = require('@google/maps').createClient({
key: process.env.GOOGLE_API_KEY
});
const {google} = require('googleapis');
const customsearch = google.customsearch('v1');
const IMAGE_API_INCREMENT = 10;
const IMAGE_API_MAX = 30;
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// using webpack-dev-server and middleware in development environment
if(process.env.NODE_ENV !== 'production') {
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var webpack = require('webpack');
var config = require('./webpack.config');
var compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }));
app.use(webpackHotMiddleware(compiler));
}
app.use(express.static(path.join(__dirname, 'dist')));
app.get('/', function(request, response) {
response.sendFile(__dirname + '/dist/index.html')
});
app.listen(PORT, function(error) {
if (error) {
console.error(error);
} else {
console.info("==> 🌎 Listening on port %s. Visit http://localhost:%s/ in your browser.", PORT, PORT);
}
});
/*--------------- ^^^ Express Server Setup ^^^ ----------------*/
/* --------------- TODO: Call Remote APIs ---------------- */
app.get('/getGoogleKey', function(request, response){
const data = process.env.GOOGLE_API_KEY;
// TODO: error handling
response.json(data);
});
app.post('/getPlaceInfo', function(request, response){
console.log(request);
googleMapsClient.place({
placeid: request.body
}, function(err, data) {
if (!err) {
console.log(data);
//response.json(data);
}
});
});
app.post('/getPlacesData', async function(request, response){
googleMapsClient.places({
location: request.body.location,
radius: 2000,
query: 'point of interest'
}, async function(err, data) {
if (!err) {
const places = data.json.results.map(item => item.name);
const placeIDs = data.json.results.map(item => item.place_id);
const placeInfo = await Promise.all(places.map(async function (placeName, index) {
const newUrls= await imageSearch(placeName, request.body.city, 1, []);
const place = new Object();
place.name = placeName;
place.id = placeIDs[index];
place.images = newUrls;
return place;
}));
response.json(placeInfo);
}
});
});
async function imageSearch(query, city, index, urls) {
if(index > IMAGE_API_MAX) {
return [...urls];
} else {
const data = await customsearch.cse.list({
cx: '002529004652784734871:cadocxkhd9g',
q: query + " " + city,
auth: "AIzaSyAi2TGFCA7tVP2rHrDMG4FugLc6WJk3i54",
searchType: "image",
imgColorType: "color",
rights: "cc_publicdomain, cc_noncommercial",
imgType: "photo",
start: index
});
const newUrls = await imageSearch(query, city, index + IMAGE_API_INCREMENT, data.data.items.map(item => item.link));
return [...urls, ...newUrls]
}
}