Module

std/node/net.ts>Server#address

Deno standard library
Go to Latest
method Server.prototype.address
import { Server } from "https://dotland.deno.dev/std@0.116.0/node/net.ts";

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.

import { createRequire } from "https://deno.land/std@0.116.0/node/module.ts";
import { Socket } from "https://deno.land/std@0.116.0/node/net.ts";

const require = createRequire(import.meta.url);
const net = require("net");

const server = net.createServer((socket: Socket) => {
  socket.end("goodbye\n");
}).on("error", (err: Error) => {
  // 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().

Returns

AddressInfo | string | null