-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
132 lines (120 loc) · 3.88 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var express = require('express');
var app = express();
var request = require('request');
var resultsArr = [];
var mongodb = require('mongodb');
var path = require('path');
var mongo_url = 'mongodb://eric8ah:[email protected]:53775/search-history';
var MongoClient = mongodb.MongoClient;
var string;
var historyArr = [];
var historyObj;
function printResults(values) {
for (var i=0;i<values.length;i++) {
var name = values[i].name;
var image = values[i].contentUrl;
var webURL = values[i].webSearchUrl;
var imageResult = {name, image, webURL};
resultsArr.push(imageResult);
}
}
var insertSearchTerm = function(db, callback) {
db.collection('history').insertOne(
{
"term": string,
"timestamp": new Date(Date.now()).toISOString()
}, function(err, result) {
if (err) throw new Error(err);
console.log('Inserted a record of search term \"' + string + '\"' );
callback();
});
};
var getSearchHistory = function(db, callback) {
var cursor = db.collection('history').find({}, {term: 1, timestamp: 1}).limit(10);
cursor.each(function(err, doc) {
if (err) {
return err;
} else if (doc !== null) {
console.dir(doc);
console.log('doc was found?');
historyObj = { "term": doc.term, "timestamp": doc.timestamp };
historyArr.push(historyObj);
console.log(historyArr);
} else {
console.log('doc was not found?');
callback();
}
});
};
app.get('/', function(req, res) {
var readme = path.join(__dirname, 'README.md');
res.sendFile(readme, function(err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
else {
console.log('File was sent');
}
});
});
app.get('/api/imagesearch/:query',function(req, res) {
MongoClient.connect(mongo_url, function(err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', mongo_url);
resultsArr = [];
var options = {};
var offsetVal;
string = req.params.query;
offsetVal = req.query.offset;
if (offsetVal !== undefined) {
options = { method: 'GET',
url: 'https://api.cognitive.microsoft.com/bing/v5.0/images/search',
qs: { q: string, count: '10', offset: offsetVal},
headers:
{ 'postman-token': '409cff25-9099-a85f-4d72-777283d089cd',
'cache-control': 'no-cache',
'ocp-apim-subscription-key': '55ce1008e1094a11bf1480a3e648f087' } };
} else {
options = { method: 'GET',
url: 'https://api.cognitive.microsoft.com/bing/v5.0/images/search',
qs: { q: string, count: '10'},
headers:
{ 'postman-token': '409cff25-9099-a85f-4d72-777283d089cd',
'cache-control': 'no-cache',
'ocp-apim-subscription-key': '55ce1008e1094a11bf1480a3e648f087' } };
}
console.log(options);
request(options, function (error, response, body) {
if (error) throw new Error(error);
var newBody = JSON.parse(body);
var values = newBody['value'];
printResults(values);
console.log(resultsArr);
res.send(resultsArr);
insertSearchTerm(db, function() {
});
db.close();
});
}
});
});
app.get('/api/latest/imagesearch', function(req, res) {
MongoClient.connect(mongo_url, function(err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', mongo_url);
getSearchHistory(db, function() {
res.send(historyArr);
console.log(historyArr);
});
db.close();
}
});
});
app.listen(process.env.PORT || 8080, function () {
console.log('Server has been started');
});