Gergely
Gergely

Reputation: 7555

Callback function documentation in NodeJS, especially net.createServer

The NodeJS documentation writes that net.createServer has an optional argument connectionListener

net.createServer([options][, connectionListener])

The documentation does not tell that it has an argument, the incoming connection's socket, it turns out only from the example code:

const net = require('net');
const server = net.createServer((c) => {
  // 'connection' listener
  console.log('client connected');
  c.on('end', () => {
    console.log('client disconnected');
  });
  c.write('hello\r\n');
  c.pipe(c);
});
server.on('error', (err) => {
  throw err;
});
server.listen(8124, () => {
  console.log('server bound');
});

Where is it documented in the NodeJS documentation whether a callback function has any arguments and what are those?

Upvotes: 0

Views: 357

Answers (1)

slebetman
slebetman

Reputation: 114024

It is documented as the parameter passed to the 'connection' event:

  • connectionListener Automatically set as a listener for the 'connection' event.

And if you check what the connection event is you will get this:

Event: 'connection'

Added in: v0.1.90

  • <net.Socket> The connection object

Emitted when a new connection is made. socket is an instance of net.Socket.

So the parameter passed to a function that handles the connection event is a net.Socket object.

Upvotes: 1

Related Questions