Skip to content

Instantly share code, notes, and snippets.

@dengjonathan
Last active August 29, 2016 00:00
Show Gist options
  • Save dengjonathan/35805918f100ed6493a225f689b33f5d to your computer and use it in GitHub Desktop.
Save dengjonathan/35805918f100ed6493a225f689b33f5d to your computer and use it in GitHub Desktop.
An example of piping in output from an HTTP request into another function
var http = require('http');
var bl = require('bl');
// file path is from enviroment variable passed in through CLI
var urls = process.argv.slice(2);
var results = [];
var returned = 0;
function httpGet(index) {
http.get(urls[index], function(response) {
response.pipe(bl(function(err, data) {
if (err) {
console.error(err);
}
results[index] = data.toString();
returned++;
if (returned === 3) {
results.forEach(function(each) {
console.log(each);
});
}
})).on('error', function(err) {
console.error("There was an error submitting the http request.");
});
});
}
for (var i = 0; i < urls.length; i++) {
httpGet(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment