-
Notifications
You must be signed in to change notification settings - Fork 0
/
program-exercise9.js
89 lines (71 loc) · 2.06 KB
/
program-exercise9.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//exercise 8 of 13 - learnyounode
var http = require('http')
var http1 = require('http')
var http2 = require('http')
var http3 = require('http')
var bl = require('bl')
var bl1 = require('bl')
var bl2 = require('bl')
var bl3 = require('bl')
var responseContent = []
var responseIndex = 0
/*
http1.get(process.argv[2], function (res){
res.setEncoding("utf8")
// res.on('data', function(err, data) {
// if (err) return console.log('error:@@@@@@@@@' + err)
// data = data.toString()
// console.log( data.length)
// console.log(data)
// })
res.pipe(bl1(function (err, data){
//console.log("display:" + data.toString())
if (err)
console.log('error:@@@@@@@@@' + err)
responseContent[responseIndex++] = data.toString()
if (responseIndex == 3) printContents()
//console.log(data.toString())
}))
}).on('error' , console.log)
http2.get(process.argv[3], function (res){
res.setEncoding("utf8")
res.pipe(bl2(function (err, data){
if (err)
console.log('error:@@@@@@@@@' + err)
responseContent[responseIndex++] = data.toString()
if (responseIndex == 3) printContents()
//console.log(data.toString())
}))
}).on('error' , console.log)
http3.get(process.argv[4], function (res){
res.setEncoding("utf8")
res.pipe(bl3(function (err, data){
if (err)
console.log('error:@@@@@@@@@' + err)
responseContent[responseIndex++] = data.toString()
if (responseIndex == 3) printContents()
//console.log(data.toString())
}))
}).on('error' , console.log)
*/
function httpGet(i) {
//console.log("inside httpGet : " + i )
http.get(process.argv[2+i] , function ( res) {
res.setEncoding("utf8")
res.pipe(bl(function (err, data){
if (err)
return console.log('error:@@@@@@@@@' + err)
responseContent[responseIndex] = data.toString()
responseIndex++
if (responseIndex == 3) printContents()
}))
}).on('error', console.log)
}
function printContents() {
//responseContent.forEach( function (s) { console.log(s)})
for (i = 0 ; i< responseIndex ; i++) { console.log (responseContent[i]) }
}
for (var i =0 ; i < 3; i++)
{
httpGet(i)
}