~~ 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的更多相关文章

  1. nodeschool.io 4

    ~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...

  2. nodeschool.io 3

    ~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...

  3. nodeschool.io 2

    ~~  BABY STEPS  ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...

  4. nodeschool.io 10

    ~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...

  5. nodeschool.io 9

    ~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...

  6. nodeschool.io 7

    ~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...

  7. nodeschool.io 6

    ~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...

  8. nodeschool.io 5

    ~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...

  9. NODESCHOOL

    来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...

随机推荐

  1. Cacti中文版在Centos上的安装

    最近老有人问Cacti中文版在哪下载啊怎么安装啊,我在这里一遍给大家讲解了:Cacti中文版在Centos上的安装 1.基本安装 cacti是运作在apache+php+mysql+net-snmp工 ...

  2. 编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,最后再在主类E 的main方法中创建Print的对象并赋值,运行方法

    package lianxi; public interface InterfaceA { void printCapitalLetter(); } package lianxi; public in ...

  3. glyphicons-halflings-regular.ttf 404

    这个是用bootstrap框架时我遇到的问题,个人解决过程如下: ① 这个资源不是我手动引用的,是bootstrap.min.css文件间接调用的. ② 默认的路径是css文件路径是project/c ...

  4. CANopen笔记2

    PDO 过程数据对象用于在节点之间传送过程数据,如I/O模块I/O状态读取和设定,模拟量采集和模拟量输出等等,协议考虑从机硬件限制最多支持4组PDO,每组包含一个RPDO和一个TPDO.The Gol ...

  5. mysql 实现行号的方法——如何获取当前记录所在行号

    SELECT aaa,(@rowNum:=@rowNum+1) AS rowNoFROM tb_bbb,(SELECT (@rowNum :=0) ) bORDER BY tb_bbb.liushui ...

  6. 汇编语言指令与debug命令符

    •MOV与ADD指令 汇编指令 控制CPU完成的操作 形式化语法描述 mov ax, 18 将18送入AX (AX)=18 mov   ah, 78 将78送入AH (AH)=78 add ax, 8 ...

  7. Spring管理bean的生命周期

    1: bean的创建:   如果我们默认的scope配置为Singleton的话, bean的创建实在Spring容器创建的时候创建: 如果scope的配置为Prototype的话,bena的创建是在 ...

  8. iOS - Swift 基本语法

    前言 Swift 全面支持 Unicode 符号. Swift 中的定义和实现是在同一个单元中的,通常一个 Swift 源代码单文件是以 ".Swift" 结尾的. Swift 不 ...

  9. Java源码初学_LinkedList

    一.LinkedList的内部数据结构 LinkedList底层是一个链表的数据结构,采用的是双向链表,基本的Node数据结构代码如下: private static class Node<E& ...

  10. hdu 3117 Fibonacci Numbers

    这道题其实也是水题来的,求Fibonacci数的前4位和后4位,在n==40这里分界开.后4位不难求,因为n达到了10^18的规模,所以只能用矩阵快速幂来求了,但在输出后4位的时候一定要注意前导0的处 ...