在node.js中,“request.on”什么是这个“.on” [英] In node.js "request.on" what is it this ".on"
问题描述
我是node.js和java脚本的新手,我无法找到这个.on关键字的含义。当我用另一个单词代码更改它失败时。
I'm new in node.js and java script, i cant find meaning of this ".on" keyword. when i change it with another word code failed.
var req = http.get("http://www.google.com", function(res) {
console.log("Got response: " + res.statusCode);
res.on('data', function (chunk) {
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
推荐答案
on 方法将事件绑定到对象。
这是一种表达意图的方法如果发生了某些事情(数据已发送或您的情况发生错误),则执行作为参数添加的函数。这种编程风格称为事件驱动编程。
你可能想在维基百科中查找它
It is a way to express your intent if there is something happening (data sent or error in your case) , then execute the function added as a parameter. This style of programming is called Event-driven programming. You might want to look it up in the Wikipedia
在node.js中,有一个名为 EventEmitter 的类,它为您提供基本事件所需的所有代码,如果您决定在自己的事件中使用它们代码(我强烈建议在node.js的情况下)。 node.js EventEmitter 的文档为此处
In node.js, there is a class called EventEmitter that provides you with all the code that you need for basic events if you decide to use them in your own code (which I would strongly recommend in the case of node.js). Docs for node.js EventEmitter are here
这篇关于在node.js中,“request.on”什么是这个“.on”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!