-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleServer.js
62 lines (52 loc) · 1.59 KB
/
simpleServer.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
//Lets require/import the HTTP module
var http = require('http');
var fs = require('fs');
var url = require("url");
//var dispatcher = require('httpdispatcher');
//Lets define a port we want to listen to
const PORT=8088;
fs.readFile('results.json', 'utf8', function (err, data) {
if (err) throw err;
allResults = JSON.parse(data);
});
fs.readFile('filtered.json', 'utf8', function (err, data) {
if (err) throw err;
filteredResults = JSON.parse(data);
});
fs.readFile('final.json', 'utf8', function (err, data) {
if (err) throw err;
finalResults = JSON.parse(data);
});
//Create a server
var server = http.createServer(function(req,res){
var urlParams = url.parse(req.url, true).query;
var relevantIds = urlParams.relevantIds;
var unrelevantIds = urlParams.unrelevantIds;
var results = allResults;
if(relevantIds){
results = filteredResults;
}
else if(unrelevantIds){
results = finalResults;
}
if(urlParams.id){
for(var i=0; i<results.items.length; i++){
if(results.items[i].id == urlParams.id){
//TODO: get rid of that
results = results.items[i];
break;
}
}
}
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
res.write(JSON.stringify(results, null, 3));
res.end();
});
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});