Last active
September 19, 2018 21:25
-
-
Save Anna-Myzukina/9ed61b1135a761438d27fb5dad584f4d to your computer and use it in GitHub Desktop.
async-you
This file contains hidden or 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
//Exercise 1 | |
/*write a program that first reads the | |
contents of a file. | |
The path will be provided as the first command-line argument to your | |
program (i.e. process.argv[2]). | |
The file will contain a single URL. Using http.get, create a GET | |
request to this URL and console.log the response body. | |
*/ | |
var fs = require("fs"), | |
fp = process.argv[2], | |
http = require('http'), | |
async = require('async'); | |
async.waterfall([function(cb) | |
{ | |
fs.readFile(fp,function(err,data) | |
{ | |
if(err) return console.log(error); | |
cb(null,data.toString()); | |
}); | |
},function(data) | |
{ | |
http.get(data,function(res) | |
{ | |
var body = ""; | |
res.on('data',function(chunk) | |
{ | |
body +=chunk.toString(); | |
}); | |
res.on('end',function() | |
{ | |
console.log(body); | |
}); | |
}); | |
}]); |
This file contains hidden or 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
//Exercise 2 | |
/*Write a program that will receive two URLs as the first and second | |
command-line arguments. | |
Using http.get, create a GET request to these URLs and pass the response | |
body to the callback. | |
Pass in an object of task functions, using the property names requestOne | |
and requestTwo, to async.series. | |
console.log the results in the callback for series when all the task | |
functions have completed. | |
*/ | |
var async = require('async'), | |
url = [process.argv[2], process.argv[3]], | |
http = require('http'); | |
async.series({ | |
requestOne: function (done) { | |
http.get(url[0], function (res) { | |
res.on('data', function (chunk) { | |
done(null, chunk.toString()) | |
}); | |
}); | |
}, | |
requestTwo: function (done) { | |
http.get(url[1], function (res) { | |
res.on('data', function (chunk) { | |
done(null, chunk.toString()); | |
}); | |
}); | |
} | |
}, function (err, results) { | |
console.log(results); | |
}); |
This file contains hidden or 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
//Exercise 3 | |
/*Create a program that will receive two URLs as the first and second | |
command-line arguments. | |
Then using http.get, create two GET requests, one to each URL, and | |
console.log any errors. | |
*/ | |
var async = require('async'), | |
http = require('http'); | |
var urls = process.argv.slice(1, 3); | |
async.each(urls, function (item, done) { | |
http.get(item).on('error', function (e) { | |
done(e); | |
}); | |
}, | |
function (err) { | |
if (err) console.log(err); | |
}); |
This file contains hidden or 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
//Exercise 4 | |
/*Write a program that will receive two command-line arguments to two URLs. | |
Using http.get create two GET requests to these URLs. | |
You will need to use async.map, then console.log the results array. | |
*/ | |
var async = require('async'), | |
http = require('http'); | |
var urls = process.argv.slice(2, 4); | |
async.map(urls, function(url, done) { | |
http.get(url, function(res) { | |
var body = ''; | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end', function() { | |
return done(null, body); | |
}); | |
}); | |
}, | |
function(err, results) { | |
if (err) { | |
return console.log(err); | |
} | |
console.log(results); | |
}); |
This file contains hidden or 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
//Exercise 5 | |
/*In this problem, you will need to co-ordinate a few async operations. | |
Use async.series for this and pass in an Object. One of the task functions | |
will need to use async.times to send POST requests using http.request. The | |
other will then do the GET request. | |
You can read more about async.times here: | |
(https://github.com/caolan/async#times) | |
*/ | |
var http = require('http'), | |
qs = require('querystring'), | |
async = require('async'), | |
hostname = process.argv[2], | |
port = process.argv[3], | |
url = 'http://' + hostname + ':' + port; | |
async.series({ | |
post: function(done){ | |
async.times(5, function(n, next){ | |
_addUser(++n, function(err){ | |
next(err); | |
}); | |
}, function next(err){ | |
if (err) return done(err); | |
done(null, 'saved'); | |
}); | |
}, | |
get: function(done){ | |
http.get(url + '/users', function(res){ | |
var body = ""; | |
res.on('data', function(chunk){ | |
body += chunk.toString(); | |
}); | |
res.on('end', function(){ | |
done(null, body); | |
}); | |
}).on('error', done); | |
} | |
}, function done(err, result){ | |
if (err) return console.log(err); | |
console.log(result.get); | |
}); | |
function _addUser(user_id, next){ | |
var postdata = JSON.stringify({'user_id': user_id}), | |
opts = { | |
hostname: hostname, | |
port: port, | |
path: '/users/create', | |
method: 'POST', | |
headers: { | |
'Content-Length': postdata.length | |
} | |
}; | |
var req = http.request(opts, function(res){ | |
res.on('data', function(chunk){}) | |
res.on('end', function(){ | |
next(); | |
}); | |
}); | |
req.on('error', function(err){ | |
next(err); | |
}); | |
req.write(postdata); | |
req.end(); | |
} |
This file contains hidden or 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
//Exercise 6 | |
/* | |
Write a program that will receive a URL as the first command line | |
argument. | |
To this URL, for each of the values in the following array, send a GET | |
request using http.get with a query parameter named number set at the | |
proper value: | |
['one', 'two', 'three'] | |
Each time, convert the response body to Number and add it to the previous | |
value. console.log the final reduced value. | |
## Hints | |
Use async.reduce: | |
(https://github.com/caolan/async#reduce) | |
*/ | |
var http = require('http'), | |
async = require('async'); | |
var url = process.argv[2], | |
number = ['one', 'two', 'three']; | |
async.reduce(number, 0, function(memo, item, callback) { | |
http.get(url + "?number=" + item, function(res) { | |
var body = ''; | |
res.on('data', function(block) { | |
body += block; | |
}); | |
res.on('end', function() { | |
callback(null, +body + memo); | |
}); | |
}).on('error', callback); | |
}, function(err, result) { | |
if (err) return console.error(err); | |
console.log(result); | |
}); |
This file contains hidden or 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
//Exercise 7 | |
/* | |
Write a program that will receive a single command line argument to a URL. | |
Using async.whilst and http.get, send GET requests to this URL until the | |
response body contains the string "meerkat". | |
console.log the amount of GET requests needed to retrieve the "meerkat" | |
string. | |
## Hints | |
String.prototype.trim() is your friend. | |
You can get documentation on async.whilst() here: | |
(https://github.com/caolan/async#whilst) | |
*/ | |
var http = require('http'), | |
async = require('async'), | |
url = process.argv[2], | |
responseString = '', | |
count = 0; | |
async.whilst( | |
function () { | |
return responseString != 'meerkat'; | |
}, | |
getRequest, | |
function (err) { | |
if (err) console.log(err); | |
console.log(count); | |
}); | |
function getRequest(done) { | |
body = ''; | |
count++; | |
http.get(url, function (res) { | |
res.on('data', function (block) { | |
body += block; | |
}); | |
res.on('end', function () { | |
responseString = body; | |
done(null, count); | |
}); | |
}).on('error', done); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment