get-server-address is a concise and focused utility for Node.js environments that provides a standardized way to retrieve the listening address of an `http.Server` instance. It abstracts away the intricacies of directly interacting with `server.address()`, which can return different types (string for Unix sockets, or an object for `AddressInfo` with IPv4/IPv6 details). The package aims to return a human-readable URL string, correctly handling IPv6 addresses by wrapping them in brackets (e.g., `http://[::1]:3000`). Currently at version 1.0.1, it maintains a stable API, with releases likely occurring only for critical bug fixes or compatibility updates with new Node.js versions. It differentiates itself by offering a simple, consistent API for a common server-side task, focusing solely on the address retrieval rather than server creation or management.
npm install get-server-addressVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create a simple HTTP server, start listening on a port, and then use `getServerAddress` to retrieve and log the full listening URL. It also includes basic error handling.
Only call `getServerAddress` inside the `server.listen()` callback or after confirming `server.listening` is true.
Ensure the server object passed to `getServerAddress` conforms to the `net.Server.address()` return types. Custom servers might need to expose a compatible `address()` method.
Apply string manipulation (e.g., `replace('[', '').replace(']', '')`) if a bracket-less IPv6 address is needed, but be aware this is generally not URL-safe.Ensure the server is actively listening before calling `getServerAddress`. Place the call within the `server.listen()` callback.
Verify that your server is configured to listen on a network port, not a Unix domain socket, if you expect 'port'. If listening on a Unix domain socket, `getServerAddress` will return the socket path as a string.
No dependency data recorded yet.