forked from PatrickJS/Reddit-Insight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myLibrary.js
36 lines (31 loc) · 1016 Bytes
/
myLibrary.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
var http = require('http');
var https = require('https');
/**
* getJSON: REST get request returning JSON object(s)
* @param options: http options object
* @param callback: callback to pass the results JSON object(s) back
*/
exports.getJSON = function(options, onResult, self)
{
// console.log("rest::getJSON", JSON.stringify(options), onResult);
var prot = options.port == 443 ? https : http;
var req = prot.request(options, function(res)
{
// console.log('response received:' + res);
var output = '';
console.log(options.host + ':' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function (chunk) {
// console.log('chunk: ', chunk);
output += chunk;
});
res.on('end', function() {
var obj = JSON.parse(output);
onResult(res.statusCode, obj, self);
});
});
req.on('error', function(err) {
//res.send('error: ' + err.message);
});
req.end();
};