Last active
July 14, 2017 09:43
-
-
Save ORESoftware/9d27e3ed392e2eefb123 to your computer and use it in GitHub Desktop.
StackExchange API Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by amills001c on 9/15/15. | |
*/ | |
var url = 'http://api.stackexchange.com/2.2/answers?order=desc&sort=activity&site=stackoverflow'; | |
var http = require("http"); | |
var zlib = require("zlib"); | |
function getGzipped(url, callback) { | |
// buffer to store the streamed decompression | |
var buffer = []; | |
http.get(url, function (res) { | |
// pipe the response into the gunzip to decompress | |
var gunzip = zlib.createGunzip(); | |
res.pipe(gunzip); | |
gunzip.on('data', function (data) { | |
// decompression chunk ready, add it to the buffer | |
buffer.push(data.toString()) | |
}).on("end", function () { | |
// response and decompression complete, join the buffer and return | |
callback(null, buffer.join("")); | |
}).on("error", function (e) { | |
callback(e); | |
}) | |
}).on('error', function (e) { | |
callback(e) | |
}); | |
} | |
getGzipped(url, function (err, data) { | |
if(err){ | |
console.error(err); | |
} | |
else{ | |
console.log(data); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment