Nodejs 这段代码不是特别懂。。

Nodejs 这段代码不是特别懂。。

http.get({ host: ‘shapeshed.com’ }, function(res) {
console.log(“Got a response from shapeshed.com”);
}).on(‘error’, function(e) {
console.log(“There was an error from shapeshed.com”);
});

).on(‘error’, function(e)

一个点,一个on,代表啥意思。。 function(e) 这回调函数在这里有麻用 = =


13 回复

当然可以!让我们来详细解析一下这段 Node.js 代码。

代码解析

http.get({
    host: 'shapeshed.com'
}, function(res) {
    console.log("Got a response from shapeshed.com");
}).on('error', function(e) {
    console.log("There was an error from shapeshed.com");
});

1. http.get 方法

http.get 是 Node.js 中的一个方法,用于发起一个 HTTP GET 请求。它接受两个参数:

  • 第一个参数:一个配置对象,包含请求的选项(如 host, port, path 等)。
  • 第二个参数:一个回调函数,当接收到服务器响应时会被调用。

2. 回调函数 (res)

在这个例子中,回调函数接收一个名为 res 的参数,表示服务器的响应。通常情况下,res 是一个包含了响应头、响应体等信息的对象。

3. .on('error', function(e))

.on 方法用于监听事件。在这个例子中,我们监听了一个名为 'error' 的事件。如果请求过程中发生错误,这个事件就会被触发,并且传递一个错误对象 e 给回调函数。

示例解释

假设我们想从 shapeshed.com 获取一些数据。我们可以使用 http.get 方法发起请求,当服务器响应时,我们会打印一条消息;如果请求过程中出错,则会打印另一条错误消息。

代码执行流程

  1. 发起一个 HTTP GET 请求到 shapeshed.com
  2. 当服务器响应时,执行回调函数,打印 “Got a response from shapeshed.com”。
  3. 如果请求过程中出现任何错误,执行错误处理函数,打印 “There was an error from shapeshed.com”。

完整示例

const http = require('http');

// 发起 HTTP GET 请求
http.get({
    host: 'shapeshed.com'
}, function(res) {
    // 打印响应消息
    console.log("Got a response from shapeshed.com");
    
    // 打印响应状态码
    console.log(`STATUS: ${res.statusCode}`);
    // 打印响应头部
    res.on('data', function(chunk) {
        console.log(`BODY: ${chunk}`);
    });
}).on('error', function(e) {
    // 打印错误消息
    console.log("There was an error from shapeshed.com", e);
});

总结

通过这段代码,我们可以理解如何发起 HTTP GET 请求并处理响应和错误。http.get 方法用于发起请求,回调函数处理响应,而 .on('error') 用于处理错误。希望这些解释能帮助你更好地理解这段代码。


监听 error 时间,收到事件后,执行匿名函数function(e){} e 是错误事件的参数

.表示调用对象的方法,onaddListener的别名,用于注册事件监听器的,eventEmitter.addListener('err', function (e) {})就是监听eventEmitter(事件发生器,这里就是http.get()方法返回的对象)的error事件,事件发生后你传递的匿名函数function (e) {}会被调用,并且会得到一个参数e

这么看: http.get().on(…).on(…),说明 http.get() 这个函数返回的对象有 on() 这个方法可以调,同时,on() 方法也返回对象本身,所以可以继续 on()

专业名词应该叫 链式调用。

分开写就是:

var a = http.get(…);

a.on(‘error’, func_err);

function func_err(e) { }

哦 thx!

那这个on是哪个对象的方法哦?

噢,不过分开写就不是异步了吧。。

是否异步跟是否分开写没关系。。。

把对象的每一个方法最后都return this就可以无限链下去,比如

var a={
    b:'bbb',
    c:function(){
    	//doSomething
    	return this;
    },
    d:function(){
    	//doAnotherThing
    	return this;
    }
}

这样就可以a.c().d().c().d()…etc 当然你举的例子是function,不过function也是对象,一样也可以有属性和方法,function是比较有趣的变量~~

http.get().on(…).on(…) : 函数式语言的标准用法。

这段代码使用了 Node.js 的 http 模块来发起一个 HTTP GET 请求。让我们逐行解析一下:

http.get({ host: 'shapeshed.com' }, function(res) {
    console.log("Got a response from shapeshed.com");
}).on('error', function(e) {
    console.log("There was an error from shapeshed.com");
});

解析

  1. http.get 方法

    • http.gethttp.request 的简写版本,用于发起 HTTP GET 请求。
    • 它接受两个参数:第一个参数是配置对象(包含主机名等),第二个参数是回调函数,用于处理响应数据。
  2. 回调函数

    • function(res) 是请求成功后的回调函数。
    • 当服务器返回响应时,该函数会被调用,并且 res 参数是一个包含了响应数据的流对象。
  3. .on('error', function(e))

    • 这是在请求过程中添加事件监听器的方法。
    • 'error' 是一个事件名称,表示请求过程中出现了错误。
    • function(e) 是当发生错误时被调用的回调函数,e 是错误对象。

示例代码解释

http.get({ host: 'shapeshed.com' }, function(res) {
    // 请求成功后,打印出 "Got a response from shapeshed.com"
    console.log("Got a response from shapeshed.com");
}).on('error', function(e) {
    // 如果请求过程中出现错误,打印出错误信息
    console.log("There was an error from shapeshed.com", e);
});
  • console.log("Got a response from shapeshed.com");

    • 当服务器成功返回响应时,会在控制台输出一条消息,表示接收到响应。
  • console.log("There was an error from shapeshed.com", e);

    • 如果请求过程中发生错误,会在控制台输出一条错误消息,并将错误对象 e 打印出来。

希望这样解释能让你更好地理解这段代码的作用和用途。如果你有任何其他问题,欢迎继续提问!

回到顶部