net_server.address()

server.address()

Returns the bound address, the address family name and port of the server as reported by the operating system. Useful to find which port was assigned when giving getting an OS-assigned address. Returns an object with three properties, e.g. { port: 12346, family: 'IPv4', address: '127.0.0.1' }

Example:

var server = net.createServer((socket) => {
  socket.end('goodbye\n');
}).on('error', (err) => {
  // handle errors here
  throw err;
});

// grab a random port.
server.listen(() => {
  address = server.address();
  console.log('opened server on %j', address);
});

Don't call server.address() until the 'listening' event has been emitted.

doc_Nodejs
2016-04-30 04:40:30
Comments
Leave a Comment

Please login to continue.