简体   繁体   中英

In Node.js, what's “on”?

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);
  });
});

I can understand it except one part: what's on in res.on ? What's the difference between it and 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!');
});

onaddListener都是同一函数的别名。

事件文档暗示它们是同一功能的两个别名。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM