Learnyounode http收集一个

我通过learnyounode教程学习node.js,并坚持在HTTP-COLLECT练习中。 我想让我的代码在没有第三方程序的帮助下工作。 它几乎可以正常工作,但由于某种原因,它不能接收来自learnyounode从中汲取数据的任何来源的第一块数据,所以我的代码是错误的。 这是我的代码到目前为止:

http = require("http"); var info = []; http.get(process.argv[2],function(res){ res.setEncoding('utf8'); res.on("data",function(input){ info.push(input); }); res.on("error",console.error); res.on("end",function(){ console.log(info.join("")); }); }); 

terminal输出:

 1. ACTUAL: "As busy as a fossicker mate built like a bluey. As dry as a sickie piece of piss mad as a beauty. As cunning as a trackies with get a dog up ya muster. Gutful of mate's rate piece of piss lets get some battler." 1. EXPECTED: "402" 2. ACTUAL: "We're going bottlo also come a holden. Grab us a bradman no dramas he hasn't got a ironman. She'll be right nipper no dramas she'll be right sheila. Mad as a pav also he hasn't got a barbie. " 2. EXPECTED: "As busy as a fossicker mate built like a bluey. As dry as a sickie piece of piss mad as a beauty. As cunning as a trackies with get a dog up ya muster. Gutful of mate's rate piece of piss lets get some battler." 3. ACTUAL: "" 3. EXPECTED: "We're going bottlo also come a holden. Grab us a bradman no dramas he hasn't got a ironman. She'll be right nipper no dramas she'll be right sheila. Mad as a pav also he hasn't got a barbie. " 4. ACTUAL: 4. EXPECTED: "" 

我看到这个post,所以它并没有真正帮助我,因为1)我试图使用http.get()中指定的练习的提示和2)我的问题似乎并不需要做代理问题。 任何提示/帮助将不胜感激,我不知道如何修复这里的代码。

噢,呃……再多了一些,我发现有什么不对的地方:第一个数字不是我丢失的数据,只是字符长度。 程序本身实际上并没有什么问题。 我通过添加console.log(info.join("").length);修复我的代码console.log(info.join("").length); 之前的console.log(info.join("")); 线。

 res.on("end",function(){ console.log(info.join("")); }); 

因为在打印string之前没有打印出所有字符的长度。 将“结束”事件中的语句replace为:

 datas = info.join("") console.log(datas.length); console.log(datas); 
 var http = require('http'); var bl = require('bl'); http.get(process.argv[2], function (response) { response.pipe(bl(function (err, data) { if (err) return console.error(err); data = data.toString(); console.log(data.length); console.log(data); })); });