在Node.js中,什么是“启用”的? [英] In Node.js, what's "on"?

查看:76
本文介绍了在Node.js中,什么是“启用”的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方文档中,有一些示例代码:

In official doc, there is some sample code:

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

除了一部分,我可以理解: res.on 中?它和 addListener 有什么区别?

I can understand it except one part: what's on in res.on? What's the difference between it and addListener?

推荐答案

据我所知知道,它与 addListener 没什么不同。这里有有关此事件的一些文档: http://nodejs.org/ docs / latest / api / events.html#emitter.on on addListener 均记录在相同的标题。它们具有相同的效果;

As far as I'm aware, it's no different than addListener. There is some documentation on the event here: http://nodejs.org/docs/latest/api/events.html#emitter.on Both on and addListener are documented under the same heading. They have the same effect;

server.on('connection', function(stream) {
    console.log('someone connected!');
});

server.addListener('connection', function(stream) {
    console.log('someone connected!');
});

这篇关于在Node.js中,什么是“启用”的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆