server.address()


如果在 IP socket 上监听,则返回操作系统报告的绑定的 address、地址 family 名称、以及服务器 port(用于查找在获取操作系统分配的地址时分配的端口):{ port: 12346, family: 'IPv4', address: '127.0.0.1' }

对于在管道或 Unix 域套接字上监听的 server,该名称将返回为字符串。

const server = net.createServer((socket) => {
  socket.end('再见\n');
}).on('error', (err) => {
  // 处理错误
  throw err;
});

// 获取任意未使用的端口。
server.listen(() => {
  console.log('打开服务器', server.address());
});

'listening' 事件被触发之前、或在调用 server.close() 之后, server.address() 返回 null

Returns the bound address, the address family name, and port of the server as reported by the operating system if listening on an IP socket (useful to find which port was assigned when getting an OS-assigned address): { port: 12346, family: 'IPv4', address: '127.0.0.1' }.

For a server listening on a pipe or Unix domain socket, the name is returned as a string.

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

// Grab an arbitrary unused port.
server.listen(() => {
  console.log('opened server on', server.address());
});

server.address() returns null before the 'listening' event has been emitted or after calling server.close().