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 ...
随机推荐
- nl2br
PHP中 在字符串所有新行之前插入 HTML 换行标记 说明 string nl2br ( string $string [, bool $is_xhtml = true ] ) 在字符串 str ...
- Linux运维命令之一
释放内存:syncecho 3 > /proc/sys/vm/drop_caches Linux查看Dell服务器型号命令:dmidecode | grep "Product Name ...
- UVA 1366 九 Martian Mining
Martian Mining Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- HTTP和FTP的区别
原文地址: http://blog.csdn.net/xiaoxiangzhu660810/article/details/8291656 一.字面上来看 HTTP是Hyper Text Tr ...
- Log4j2在WEB项目中配置
最近决定在新WEB项目中使用新的日志系统Log4j2. 官方介绍和学习文档网址为http://logging.apache.org/log4j/2.x/ 首先在WEB项目中引入以下几个jar包: ① ...
- How To PLAY_SOUND in Oracle Forms
Play_sound is used to play audio files in Oracle Forms, Play_Sound plays the sound object in the spe ...
- 【转】The decoupling capacitor…is it really necessary?
Before working as an applications engineer, I worked as an IC test development engineer here at TI. ...
- ubuntu下解压zip文件乱码
安装unar 即可解决问题 sudo apt get install unar
- sencha touch之store
store相当于数据库的表,是模型实例的集合.(是不是可以看成结构体数组) 可以对模型实例进行新建.更新.读取.删除.排序和过滤等操作. 创建store 由于store是表,必须要有模型或者定义字段, ...
- 坐标随鼠标移动 jquery简易版
<html> <span style="position:absolute" id="xy_test"></span> &l ...