Localhost and Development Server URLs
Understand how localhost URLs work, the difference between localhost and 127.0.0.1, IPv6 loopback addresses, common development server ports, and network binding considerations.
Detailed Explanation
Localhost URLs
localhost is a hostname that resolves to the loopback network interface, allowing a computer to communicate with itself. It is fundamental to local development.
Localhost Resolution
localhost → 127.0.0.1 (IPv4) or ::1 (IPv6)
127.0.0.1 → Direct IPv4 loopback
[::1] → Direct IPv6 loopback
0.0.0.0 → All network interfaces (bind address, not a valid URL host)
Common Development URLs
http://localhost:3000 → React / Next.js / Express
http://localhost:4200 → Angular CLI
http://localhost:5173 → Vite
http://localhost:8080 → Spring Boot / Tomcat / Go
http://localhost:8000 → Django / PHP artisan
http://127.0.0.1:5432 → PostgreSQL
http://127.0.0.1:6379 → Redis
http://localhost:27017 → MongoDB
localhost vs 127.0.0.1
| Aspect | localhost |
127.0.0.1 |
|---|---|---|
| Type | Hostname | IP address |
| Resolution | DNS lookup (usually /etc/hosts) | Direct, no DNS |
| IPv6 | May resolve to ::1 |
IPv4 only |
| Cookies | Domain: localhost |
Domain: 127.0.0.1 |
| Speed | Slightly slower (DNS) | Slightly faster |
| CORS | Treated as different origins if mixed | — |
IPv6 Loopback
IPv6 loopback ::1 must be enclosed in brackets in URLs:
http://[::1]:3000/api/health
Binding: 0.0.0.0 vs 127.0.0.1
When starting a development server, the bind address matters:
127.0.0.1— Only accessible from the same machine0.0.0.0— Accessible from any network interface (other machines on the LAN can connect)
# Only local access
next dev --hostname 127.0.0.1
# Accessible from network (e.g., testing on phone)
next dev --hostname 0.0.0.0
HTTPS on Localhost
For local HTTPS development, tools like mkcert create locally-trusted certificates:
mkcert localhost 127.0.0.1 ::1
# Creates localhost.pem and localhost-key.pem
Use Case
Understanding localhost URLs is fundamental for web developers during local development and testing. Knowing the difference between localhost and 127.0.0.1 helps debug CORS issues, cookie problems, and network binding configuration. Docker networking, port forwarding, and reverse proxy setups all require a solid understanding of loopback addressing.