0%

learnyounode-Lesson 7

课程目标:

1
2
3
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).

My Solution:

1
2
3
4
5
6
7
const http = require("http");
const url = process.argv[2];
http.get(url, (request, response) => {
response.on("data", data => {
console.log(data.toString());
});
});

Official Solution:

1
2
3
4
5
6
7
8
9
const http = require("http");
const url = process.argv[2];
http
.get(url, (request, response) => {
response.setEncoding("utf8");
response.on("data", console.log);
response.on("error", console.error);
})
.on("error", console.error);

课程总结:

本节课主要讲的是 node 中 http 模块的使用

  1. http.get()方法的使用。
  2. http.get()回调函数的两个参数是request,response.都是 Node Stream object 类型。
  3. Node Stream 可以用on方法触发 Node 事件(eg.dataenderror…)
  4. responserequeset默认是 Node buffer 类型。可以直接调用setEncoding方法指定类型,也可以使用toString()等方法转换类型。
  5. 官方解决方法中处理返回错误是最佳实践。
  6. 除了http模块https模块也需要了解。