Registry / http-networking / get-server-address

get-server-address

JSON →
library1.0.1jsnpmunverified

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-address
INSTALL
IMPORT
SIG · GET-SERVER-ADDRESS
G
get-server-address
http-networkingjavascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

getServerAddress
import { getServerAddress } from 'get-server-address';
const getServerAddress = require('get-server-address');
The package uses named exports. CommonJS users should destructure the require call.
getServerAddress (type)
import type { Server } from 'http'; import { getServerAddress } from 'get-server-address';
import { Server } from 'get-server-address';
The function expects an instance of Node.js `http.Server`. Its type definition is part of the package but the Server type itself comes from Node.js's 'http' module.

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.

import { createServer } from 'http'; import { getServerAddress } from 'get-server-address'; const server = createServer(); server.listen(3000, '::', () => { const address = getServerAddress(server); console.log(`Server listening at ${address}`); // Example of how to close the server gracefully // setTimeout(() => { // console.log('Closing server...'); // server.close(() => { // console.log('Server closed.'); // }); // }, 5000); }); // Handle potential errors server.on('error', (err) => { console.error('Server error:', err); });
Debug
Known issues
gotchaCalling `getServerAddress` before the server has started listening will result in an error or `null`/`undefined` as `server.address()` will not yet return a meaningful value. Ensure `server.listen()`'s callback has been executed.
fix
Only call `getServerAddress` inside the `server.listen()` callback or after confirming `server.listening` is true.
affects: >=1.0.0
gotchaThe package assumes a standard `http.Server` or a compatible object. If a custom server implementation does not expose a `server.address()` method that returns an `AddressInfo` object or a string (for Unix sockets), `getServerAddress` may fail or return an unexpected value.
fix
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.
affects: >=1.0.0
gotchaThe package explicitly formats IPv6 addresses with brackets. If you require a different format for IPv6 addresses, you will need to process the output string further.
fix
Apply string manipulation (e.g., `replace('[', '').replace(']', '')`) if a bracket-less IPv6 address is needed, but be aware this is generally not URL-safe.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of null (reading 'address')
The `server.address()` method was called on a server that is not currently listening, thus returning `null`.
fix
Ensure the server is actively listening before calling `getServerAddress`. Place the call within the `server.listen()` callback.
TypeError: Cannot read properties of undefined (reading 'port')
`server.address()` returned an object without a 'port' property, likely because it returned a string (e.g., for a Unix domain socket) or an invalid AddressInfo object.
fix
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.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
get-server-address — npm install get-server-address · libregistry