nodeschool.io 7

~~ HTTP CLIENT ~~

Write a program that performs an HTTP GET request to a URL provided to
you as the first command-line argument. Write the String contents of
each "data" event from the response to a new line on the console
(stdout).

----------------------------------------------------------------------
HINTS:

For this exercise you will need to use the `http` core module.

Documentation on the `http` module can be found by pointing your
browser here:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\http
.html

The `http.get()` method is a shortcut for simple GET requests, use it
to simplify your solution. The first argument to `http.get()` can be
the URL you want to GET, provide a callback as the second argument.

Unlike other callback functions, this one has the signature:

function (response) { ... }

Where the `response` object is a Node Stream object. You can treat Node
Streams as objects that emit events, the three events that are of most
interest are: "data", "error" and "end". You listen to an event like
so:

stream.on("data", function (data) { ... })

The "data" is emitted when a chunk of data is available and can be
processed. The size of the chunk depends upon the underlying data
source.

The `response` object / Stream that you get from `http.get()` also has
a `setEncoding()` method. If you call this method with "utf8", the
"data" events will emit Strings rather than the standard Node `Buffer`
objects which you have to explicitly convert to Strings.

----------------------------------------------------------------------

httpClient.js(nodejs api里面找了找,啊哈哈哈)

var http = require('http');
http.get(process.argv[2], function(res) {
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log(chunk);
  });
});

 

转载于:https://www.cnblogs.com/della/p/3430220.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值