-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
105 lines (80 loc) · 2.72 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
// app entry point
const axios = require('axios'),
express = require('express'),
{ MongoClient } = require('mongodb');
const app = express(),
baseUrl = process.env.BASE_URL,
collectionName = 'img-find',
dataKeyMap = {
contextLink: 'context',
link: 'url',
thumbnailLink: 'thumbnail',
title: 'snippet',
},
dbUrl = process.env.DB_URL,
endUrl = process.env.GOOGLE_URL_END,
queryKeyMap = {
num: 'num',
offset: 'start',
q: 'q',
rights: 'rights',
size: 'imgSize',
type: 'fileType',
},
storeKeys = ['term', 'when'];
let db, collection, listener;
app.use(express.static('public'));
app.get('/', (req, res) => res.sendFile(__dirname + '/views/index.html'));
app.get('/queryKeyMap', (req, res) => res.json(queryKeyMap));
app.get('/search', async (req, res) => {
const query = req.query;
let newSearch,
url = baseUrl;
if (query.hasOwnProperty('offset')) {
query.offset = query.hasOwnProperty('num') ?
+query.offset * +query.num :
+query.offset * 10;
}
for (let key in query) {
if (url.slice(-1) !== '?') url += '&';
if (queryKeyMap.hasOwnProperty(key)) url += `${queryKeyMap[key]}=${query[key]}`;
}
url += url.slice(-1) === '&' ? endUrl : '&' + endUrl;
try {
const response = await axios.get(url);
const json = response.data.items.map( el => {
let item = {};
for (let key in dataKeyMap) {
el.hasOwnProperty(key) ?
item[dataKeyMap[key]] = el[key] :
item[dataKeyMap[key]] = el.image[key];
}
return item;
});
newSearch = { [storeKeys[0]]: query.q, [storeKeys[1]]: new Date().toISOString() };
collection.insert(newSearch);
res.set({ 'Content-Type': 'application/json' });
res.send(JSON.stringify(json, null, ' '));
}
catch (err) { console.log(err), res.end(err) }
});
app.get('/latest', async (req, res) => {
try {
const count = await collection.count();
const num = req.query.hasOwnProperty('num') && typeof +req.query.num === 'number' ?
Math.min(+req.query.num, count):
Math.min(+req.query.num, 10);
const latestSearches = await collection.find().limit(num).sort({ when: -1 }).toArray();
res.set({ 'Content-Type': 'application/json' });
res.send(JSON.stringify(latestSearches, storeKeys, ' '));
}
catch(err) { console.log(err), res.end(err) }
});
MongoClient.connect(dbUrl, (err, database) => {
if (err) throw err;
db = database;
collection = db.collection(collectionName);
listener = app.listen(process.env.PORT, () => {
console.log('Your app is listening on port ' + listener.address().port);
});
});