-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
109 lines (73 loc) · 2.67 KB
/
app.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
var http = require("http");
var https = require("https")
const PORT=9000;
http.createServer(function(request, response){
if (request.method == "POST") {
var body = "";
request.on("data", function(data) {
body += data;
// Too much POST data, kill the connection!
if (body.length > 1e6) {
request.connection.destroy();
}
});
var ids = [];
request.on("end", function() {
ids = JSON.parse(body).map(function(item, index, array) {
console.log("REQUESTING " + item.key);
var share_req = https.request({
hostname: "share.osf.io",
method: "GET",
port: 443,
path: "/api/v2/search/agents/" + item.key
}, function(resp) {
var item_details = "";
resp.on("data", function(chunk) {
if (chunk) {
item_details += chunk;
}
});
resp.on("end", function() {
if (!item_details) {
console.log("something bad here");
console.log(item_details);
return;
}
console.log(item_details);
var details = JSON.parse(item_details);
if (!details._source) {
details._source = {
name: "",
id: item.key,
sources: [],
location: "",
identifiers: [],
type: "",
types: []
};
}
ids[index] = details._source;
ids[index].number = item.doc_count;
if (item.awards) {
ids[index].awards = item.awards;
}
if (ids.some(function(el, index, array) { return el == null; })) {
return;
}
response.end(JSON.stringify(ids));
});
})
share_req.on("error", function(e) {
console.log("got error: " + e.message);
});
share_req.end();
return null;
});
});
} else {
console.log(request);
response.end("It Works!! Path Hit: " + request.url);
}
}).listen(PORT, function(){
console.log("Server listening on: http://localhost:%s", PORT);
});