nodeschool.io 8
~~ HTTP COLLECT ~~
Write a program that performs an HTTP GET request to a URL provided to
you as the first command-line argument. Collect all data from the
server (not just the first "data" event) and then write two lines to
the console (stdout).
The first line you write should just be an integer representing the
number of characters received from the server and the second line
should contain the complete String of characters sent by the server.
----------------------------------------------------------------------
HINTS:
There are two approaches you can take to this problem:
1) Collect data across multiple "data" events and append the results
together prior to printing the output. Use the "end" event to
determine when the stream is finished and you can write the output.
2) Use a third-party package to abstract the difficulties involved in
collecting an entire stream of data. Two different packages provide a
useful API for solving this problem (there are likely more!):
`bl` (Buffer List) and `concat-stream`; take your pick!
http://npm.im/bl
http://npm.im/concat-stream
To install a Node package, use the Node Package Manager `npm`. Simply
type:
npm install bl
And it will download and install the latest version of the package
into a subdirectory named `node_modules`. Any package in this
subdirectory under your main program file can be loaded with the
`require` syntax without being prefixed by './':
var bl = require('bl')
Node will first look in the core modules and then in the
`node_modules` directory where the package is located.
If you don't have an Internet connection, simply make a `node_modules`
directory and copy the entire directory for the package you want to
use from inside the learnyounode installation directory:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_modules\bl
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_modules\con
cat-stream
Both `bl` and `concat-stream` can have a stream piped in to them
and they will collect the data for you. Once the stream has ended, a
callback will be fired with the data:
response.pipe(bl(function (err, data) { ... }))
Note that you will probably need to `data.toString()` to convert from
a Buffer.
----------------------------------------------------------------------
httpCollect.js
const bl = require("bl");
var http = require('http');
http.get(process.argv[2], function(res) {
res.pipe(bl(function(err,data){
console.log(data.length);
console.log(data.toString());
}));
});
nodeschool.io 8的更多相关文章
- nodeschool.io 4
~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...
- nodeschool.io 3
~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...
- nodeschool.io 2
~~ BABY STEPS ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...
- nodeschool.io 10
~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...
- nodeschool.io 9
~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...
- nodeschool.io 7
~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...
- nodeschool.io 6
~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...
- nodeschool.io 5
~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...
- NODESCHOOL
来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...
随机推荐
- 高仿bootstrap的layout效果(一)
公司研发一个新的cms,为了减少以后的修改和尽可能大程度的满足客户对cms的灵活需求,我的经理安排我去做一个与bootstrap的layout差不多的效果,这叫什么,锻炼的时候来了,加油,这个急不得一 ...
- JBOSS批量扫描
exploit-db提供出了EXP,如下: /* * JBoss JMXInvokerServlet Remote Command Execution * JMXInvoker.java v0.3 - ...
- html之a标签
屏蔽跳转 1.href属性删除 2.href="javascript:void(0);" void是一个操作符,void(0)返回undefined,地址不发生跳转. 3.s ...
- ubuntu14.04换一个更快的源
mirrors.yun-idc.com,这个源可比ubuntu自带的源快多了,我的source.list文件内容如下: deb http://mirrors.yun-idc.com/ubuntu/ t ...
- django(一)搭建开发环境
本学习系列均使用centos7操作系统,基于python3进行操作.centos7下的python3安装配置http://www.cnblogs.com/Guido-admirers/p/625941 ...
- 关于版本号:alpha、beta、rc、stable
定义好版本号,对于产品的版本发布与持续更新很重要: 但是对于版本怎么定义,规则如何确定,却是千差万别.具体应用,可以结合自己目前的实际情况命名: 很多软件在正式发布前都会发布一些预览版或者测试版,一般 ...
- php wamp 配置虚拟主机
apeach 配置: 还有是:E:\wamp\bin\apache\Apache2.4.4\conf 目录下有个 http.conf文件中,有一个需要取消注释, # Virtual hostsInc ...
- 转 cocos2dx内存优化 (之二)
一.cocos2dx之如何优化内存使用(高级篇) 本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=93 一.内存优化原则 为了优化应用内存,你应该知道 ...
- Scrum Meeting---Ten(2015-11-5)
今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 分类页设计 4h 商品详单设计 4h 胡亚坤 首页设计 2h 滚动广告栏设计 2h 刘猛 服务器测试 ...
- mysql概要(十四)索引(补充:外键级联操作)
[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ][ ON UPDATE { NO ACTION | CASCADE | SE ...