-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (115 loc) · 3.07 KB
/
index.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
133
134
135
136
var http = require("http");
var express = require("express");
var mongo = require("mongodb").MongoClient;
var db;
app = express();
app.get("/", displayInstructions);
app.get("/api/imagesearch/*", doImageSearch);
app.get("/api/latest/imagesearch", doStoredSearches);
mongo.connect("mongodb://localhost/image-search", function(err, connection)
{
db = connection;
app.listen(8080);
console.log("Listening to port 8080");
});
function doError(response, message, err)
{
var obj = { error_message: message };
if (err)
obj.err = err;
response.setHeader("Content-Type", "text/json");
response(JSON.stringify(obj));
}
function updateStoredSearches(searchTerm)
{
db.collection("stored_searches", function(err, collection)
{
if (err)
{
doError(response, "Error trying to store search term");
return;
}
collection.save({ term: searchTerm, when: new Date().toJSON() });
});
}
function doImageSearch(request, response)
{
response.setHeader("Content-Type", "text/json");
// Get offset param
var offset = request.query.offset;
if (offset == undefined)
{
doError(response, "Missing offset parameter");
return;
}
offset = parseInt(offset, 10);
if (offset == undefined)
{
doError(response, "offset parameter is not an integer");
return;
}
// Extract search term
var searchTerm =
decodeURIComponent(request.url.substring(17, request.url.indexOf("?"))).
trim();
updateStoredSearches(searchTerm);
db.collection("cat_info", function(err, collection)
{
if (err)
{
doError(response, "Error occurred while getting collection", err);
return;
}
// Perform search with support for paging
collection.find({ snippet: { $regex: searchTerm, $options: "$i" } },
{ fields: { _id: 0 }, limit: 10, skip: offset },
function(err, items)
{
if (err)
{
doError(response, "Error occurred while searching collection",
err);
return;
}
items.toArray(function(err, array)
{
if (err)
{
doError(response, "Error occurred while collecting found " +
"documents", err);
return;
}
response.send(JSON.stringify(array));
});
});
});
}
function doStoredSearches(request, response)
{
response.setHeader("Content-Type", "text/json");
db.collection("stored_searches", function(err, collection)
{
collection.find({}, { fields: { _id: 0 } }, function(err, items)
{
items.toArray(function(err, array)
{
response.send(JSON.stringify(array));
});
});
});
}
function displayInstructions(request, response)
{
var out = "<html><head><title>Image Search Abstraction Layer</title>" +
"</head><body>" +
"<h1>Image Search Abstraction Layer</h1>" +
"<h3>To perform a search:</h3><p>" +
"<code>https://image-search-anischyros.c9users.io/api/imagesearch/" +
"<strong><em><search term></em></strong>/?offset=" +
"<strong><em><skip count></em></strong></code></p>" +
"<h3>To see the most recent search terms:</h3><p>" +
"<code>https://image-search-anischyros.c9users.io/api/latest/" +
"imagesearch</code></p>"+
"<p>Results are returned in JSON format.</p></body></html>";
response.send(out);
}