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);
});
});
nodeschool.io 7的更多相关文章
- 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 8
~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...
- 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 ...
随机推荐
- Python3基础 print 输出hello world
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- 如何解决Angular 2 的templateUrl和styleUrl的路径问题?
参考地址:https://github.com/kittencup/angular2-ama-cn/issues/18 前言: templateUrl表示的是组件在浏览器中运行时依赖的模板地址,所以在 ...
- SQL查看表锁定,死锁解锁
select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran ...
- 【转载】简述Linux的启动过程
原文:简述Linux的启动过程 本文将简单介绍一下Linux的启动过程,希望对那些安装Linux的过程中遇到了问题的朋友有些帮助 声明:本人没用过UEFI模式和GPT分区格式,所有关于这两部分的内容都 ...
- linux终端下为什么用命令打开软件后,要关闭软件才能继续下一条命令?
用终端打开chromium浏览器(命令:chromium-browser)的时候发现打开浏览器之后无法继续在终端输入命令,只能关闭浏览器或者在终端按下Ctrl+c,此时系统将退出浏览器并可以继续在终端 ...
- awk 以HWI开头,并且:相邻两行的第一个字段完全相同;
## 思路:以HWI开头,并且:相邻两行的第一个字段完全相同:awk 'BEGIN{ last_col_1="xxxxxx"; last_row="bbbbbbbbbbb ...
- 【转】雪崩光电二极管(APD)偏置电源及其电流监测
摘要:本文提供的参考设计用于实现APD偏置电源及其电流监测.基于MAX15031 DC-DC转换器,该电路能够将2.7V至11V范围的输入电压经过DC-DC电源转换器后得到一个70V.4mA电源. 下 ...
- SpringMVC 模拟登陆
新建BackgroundController类: package cn.bdqn.mvc.controller; import org.springframework.stereotype.Contr ...
- 手机页面中的meta标签
以前看书的时候,觉得meta标签就只有一个charset对于我来说是有用的.前段时间有个学弟让我写个手机版的网页,我才知道原来meta标签有那么多学问. meta指元素可提供有关页面的元信息(meta ...
- 转 Cocos网络篇[3.2](3) ——Socket连接(1)
Cocos网络篇[3.2](3) ——Socket连接(1) 2015-03-05 22:24:13 标签:network http socket cocos [唠叨] 在客户端游戏开发中,使用HTT ...