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); })); });
- 如何检查一个集合是否存在于Mongodb本地nodejs驱动程序中?
- 在Node.js中安全且正确地monkeypatching require函数
- NodeJS / express / connect-redis:Redis没有错误; 会议缺失
- 如何将Node.js环境variables注入到客户端JavaScript?
- 非阻塞STDIN检查节点?
- 为什么Node.js服务器在脚本完成后不能退出?
- 如何在新标签中查看图像,而不是使用Multer在NodeJS中下载
- node.js,setTimeoutcallback方法和“this”
- 无法加载资源:服务器在angular.js中的状态为404(未find)