-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (44 loc) · 1.28 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
var express = require('express');
var app = express();
var myMongo = require('./myMongo');
const GoogleImages = require('google-images');
const client = new GoogleImages(process.env.CSE_ID, process.env.API_KEY);
app.use(express.static('public'));
app.get("/", function (request, response) {
response.sendFile(__dirname + '/views/index.html');
});
app.get("/latest/imagesearch", (req, res) => {
myMongo.getTerms((err, data) => {
if(err) throw err;
res.send(data);
})
})
app.get("/imagesearch/:term" ,(req, res) => {
var term = req.params.term;
var offset = req.query.offset;
var date = new Date();
var termObj = {
term: term,
when: date
};
myMongo.addTerm(termObj, (err, data) => {
if(err) throw err;
console.log(data);
});
client.search(term, {page : offset}).then(images => {
var result = [];
for(let img of images){
var obj = {};
obj["url"] = img["url"];
obj["snippet"] = img["description"];
obj["thumbnail"] = img["thumbnail"]["url"];
obj["context"] = img["parentPage"];
result.push(obj);
}
res.send(result);
})
// res.end("Done!");
})
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});