~~ 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. nl2br

    PHP中   在字符串所有新行之前插入 HTML 换行标记 说明 string nl2br ( string $string [, bool $is_xhtml = true ] ) 在字符串 str ...

  2. strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes学习小结

    一.strip_tags 从字符串中去除 HTML 和 PHP 标记 string strip_tags ( string $str [, string $allowable_tags ] ) str ...

  3. EI表达式和JSTL

    JSP九大内置对象     WEB域内置对象(存值取值,传值数据用的)    setAttribute/getAttribute;    page  基本不用/ pageContext 只在本页面有效 ...

  4. C#打开指定路径文件对话框

    private string OpenFileDlog(string DeafultDir) { OpenFileDialog Ofd = new OpenFileDialog(); Ofd.AddE ...

  5. HTML5媒体

    1.视频格式 格式 文件 描述 AVI .avi AVI (Audio Video Interleave) 格式是由微软开发的.所有运行 Windows 的计算机都支持 AVI 格式.它是因特网上很常 ...

  6. DbUtils使用时抛出Cannot get a connection

    java.sql.SQLException: Cannot get a connection, pool error Timeout waiting for idle object Caused by ...

  7. asynchronous vs non-blocking

    http://stackoverflow.com/questions/2625493/asynchronous-vs-non-blocking In many circumstances they a ...

  8. linux下inotify的使用

    有时候我们需要检测某个目录下文件或者子目录的改动状况,如添加.删除.以及更新等,Linux系统上提供了inotify来完成这个功能.inotify是在版本2.6.13的内核中首次出现,现在的发行本应该 ...

  9. SQL疑难杂症【1】解决SQL2008 RESTORE 失败问题

    有时候从服务器或者其它电脑上面备份的数据库文件在还原到本地的时候会出现以下错误:  这种情况通常是备份文件之前的逻辑名称跟当前的名称对应不上,我们可以通过以下语句查看备份文件的逻辑名称: 知道备份文件 ...

  10. 用一张图片制作skybox图片 (如何制作360全景图、立方体)

    我转发的帖子在这里 http://dong2008hong.blog.163.com/blog/static/4696882720140294039205/ 原帖不知道地址 我这里简单整理了下 去掉了 ...