{"id":13227,"library":"get-server-address","title":"Get Server Address","description":"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.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/jameslnewell/get-server-address","tags":["javascript","typescript"],"install":[{"cmd":"npm install get-server-address","lang":"bash","label":"npm"},{"cmd":"yarn add get-server-address","lang":"bash","label":"yarn"},{"cmd":"pnpm add get-server-address","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package uses named exports. CommonJS users should destructure the require call.","wrong":"const getServerAddress = require('get-server-address');","symbol":"getServerAddress","correct":"import { getServerAddress } from 'get-server-address';"},{"note":"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.","wrong":"import { Server } from 'get-server-address';","symbol":"getServerAddress (type)","correct":"import type { Server } from 'http';\nimport { getServerAddress } from 'get-server-address';"}],"quickstart":{"code":"import { createServer } from 'http';\nimport { getServerAddress } from 'get-server-address';\n\nconst server = createServer();\n\nserver.listen(3000, '::', () => {\n  const address = getServerAddress(server);\n  console.log(`Server listening at ${address}`);\n\n  // Example of how to close the server gracefully\n  // setTimeout(() => {\n  //   console.log('Closing server...');\n  //   server.close(() => {\n  //     console.log('Server closed.');\n  //   });\n  // }, 5000);\n});\n\n// Handle potential errors\nserver.on('error', (err) => {\n  console.error('Server error:', err);\n});","lang":"typescript","description":"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."},"warnings":[{"fix":"Only call `getServerAddress` inside the `server.listen()` callback or after confirming `server.listening` is true.","message":"Calling `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure the server is actively listening before calling `getServerAddress`. Place the call within the `server.listen()` callback.","cause":"The `server.address()` method was called on a server that is not currently listening, thus returning `null`.","error":"TypeError: Cannot read properties of null (reading 'address')"},{"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.","cause":"`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.","error":"TypeError: Cannot read properties of undefined (reading 'port')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}