-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
37 lines (31 loc) · 1013 Bytes
/
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
var express = require('express')
var app = express()
var itemsjs;
const request = require('request');
const data_url = process.env.DATA_URL || 'https://raw.githubusercontent.com/itemsapi/itemsapi-example-data/master/items/movies-processed.json';
var PORT = process.env.PORT || 3000;
console.log('Importing JSON data..');
request(data_url, {json: true}, (err, res) => {
console.log('Imported data.');
itemsjs = require('itemsjs')(res.body);
})
app.get('/search', function (req, res) {
let filters = req.query.filters ? JSON.parse(req.query.filters) : undefined;
function waitForIndex() {
if (itemsjs) {
const result = itemsjs.search({
per_page: req.query.per_page || 10,
page: req.query.page || 1,
filters: filters
})
res.json(result)
} else {
console.log("index still not ready-wait")
setTimeout(waitForIndex, 100)
}
}
waitForIndex();
})
app.listen(PORT, function () {
console.log('Example app listening on port %s!', PORT);
})