Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Node.JS, by doing require('net'), do you not do require('event')?

The code I'm looking at does not have "require('event')" anywhere, and yet I see this code

server.on('error', function (e) {
  if (e.code == 'EADDRINUSE') {
    console.log('Address in use, retrying...');
    setTimeout(function () {
      //server.close();
      server.listen(port);//PORT, HOST);
    }, 1000);
  }
  else{ .......

That uses "on".

And looking at this line

var net = require('net')

and this line

var server = net.createServer();

makes me think that doing require('net') already includes doing require('event').

Is this right?

like image 229
itsmarziparzi Avatar asked Sep 26 '22 01:09

itsmarziparzi


1 Answers

The documentation makes it quite clear that net.Server is an EventEmitter, so EventEmitter's properties and methods (including .on()) are inherited.

like image 60
mscdex Avatar answered Sep 28 '22 13:09

mscdex