-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.js
47 lines (37 loc) · 1.45 KB
/
9.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
/*
JUGGLING ASYNC
Fazer um HTTP GET request a um URL (unix stream)
Colectar toda a informacao do servidor, e escreve 2 linhas na consola
1-Integer com o numero de caracteres, recebidos do servidor
2-COntem a string de caracteres enviada pelo servidor
*
Para 3 URLS
*
E necessario fazer queue dos resultados, para ver quantos URLs retornaram os seus conteudos
Counting callbacks is one of the fundamental ways of managing async in Node.
Rather than doing it yourself, you may find it more convenient to rely on a third-party library such as [async](http://npm.im/async) or [after](http://npm.im/after). But for this exercise, try and do it without any external helper library.
*/
var http = require('http') // http core module
var bl = require('bl') // Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too
var array_response = []
var counter = 0;
function geturl(url, index){ //funcao para descarregar o conteudo
http.get(url, function (response) { callback(response, index); })
}
function callback(response,index){
response.setEncoding('utf8'); //funcao callback
response.pipe(bl(function(err, data){ // conversao de HEX->UTF8
array_response[index] = data.toString();
}))
response.on("end", function(){
counter ++;
if (counter == 3) {
for (var i=0; i<array_response.length; i++ ) {
console.log(array_response[i])
}
}
})
}
geturl(process.argv[2], 0);
geturl(process.argv[3], 1);
geturl(process.argv[4], 2);