Localhost vs 127.0.0.1 vs ::1: Diagnose Connection Differences
Compare hostname resolution with IPv4 and IPv6 listeners using a small Node server, and diagnose why one loopback address works while another refuses connections.
localhost is a hostname, 127.0.0.1 is an IPv4 loopback address, and ::1 is an IPv6 loopback address. They refer to the current device, but the server’s listening address, the system’s name resolution, and the client’s connection strategy can differ. The same port does not guarantee that all three connect.
Separate names, addresses, and listening scope
| Value | Purpose | Browser URL example |
|---|---|---|
localhost | A name resolved by the local system | http://localhost:8765/ |
127.0.0.1 | Explicit IPv4 loopback | http://127.0.0.1:8765/ |
::1 | Explicit IPv6 loopback | http://[::1]:8765/ |
0.0.0.0 | An IPv4 wildcard address for server binding | Clients use the server’s actual address |
Brackets distinguish the colons in an IPv6 address from the port separator. http://::1:8765 is not a valid URL.
Run a small comparison
With Node.js installed, create local-http.mjs:
import { createServer } from 'node:http';
const bindAddress = process.argv[2] || '127.0.0.1';
const server = createServer((_request, response) => {
response.setHeader('Content-Type', 'text/plain; charset=utf-8');
response.end('local demo\n');
});
server.listen(8765, bindAddress, () => console.log(server.address()));
In the first terminal:
node local-http.mjs 127.0.0.1
Keep it running. In a second terminal:
curl http://127.0.0.1:8765/
curl http://localhost:8765/
curl -g "http://[::1]:8765/"
On Windows PowerShell, use curl.exe if curl resolves to an alias.
The first request should return local demo. When the server binds only to IPv4 loopback, the IPv6 request normally fails. The hostname request depends on resolution and whether the client tries another returned address. Do not assume a fixed address preference across operating systems.
Stop the server with Ctrl+C, run node local-http.mjs ::1, and compare again. IPv6 must be enabled; an address-unavailable error during startup is a separate system-support issue.
Inspect resolution and the actual listener
node -e "require('node:dns').lookup('localhost', { all: true }, console.log)"
The output may contain IPv4, IPv6, or both. Node’s dns.lookup() uses the operating system’s name-resolution facilities; see the Node DNS reference. Resolution does not prove a service is listening.
Select the listener check for your OS:
# macOS
lsof -nP -iTCP:8765 -sTCP:LISTEN
# Linux
ss -ltnp 'sport = :8765'
# Windows PowerShell
Get-NetTCPConnection -LocalPort 8765 -State Listen
Choose the next action from the result
| Observation | Next action |
|---|---|
| IPv4 works; IPv6 refuses | Check for an IPv4-only listener; use the explicit IPv4 address while correcting configuration |
| IPv6 works; IPv4 refuses | Check the IPv6 binding and whether a separate IPv4 listener is needed |
| All requests fail | Check startup logs, the running process, and its actual port |
| Computer works; phone fails | Check the LAN address and binding using the phone access guide |
| App fails inside Docker | Loopback points to the container itself; check Docker networking |
To serve multiple address families, configure and verify the intended interfaces explicitly. Whether an IPv6 wildcard listener also accepts IPv4 depends on the system and server options.
Changing the address also changes the browser origin
http://localhost:8765 and http://127.0.0.1:8765 have different hosts and therefore different origins. Review login state, browser storage, CORS allowlists, and OAuth callback URLs when switching. With HTTPS, the certificate must cover the hostname or IP you actually use. See MDN’s origin definition and the localhost HTTPS guide.
Use the port reference to identify the service and the URL parser to inspect an address. A successfully parsed URL still needs a real connection check.