Parse URLs with Custom Port Numbers
Learn how port numbers work in URLs, which ports are default for each protocol, when to explicitly specify a port, and how different tools and APIs handle port extraction.
Detailed Explanation
Port Numbers in URLs
A port number identifies a specific process or service on a networked host. In URLs, the port appears after the hostname, separated by a colon: hostname:port.
Default Ports
Each protocol has a default port that is used when no port is specified:
| Protocol | Default Port | Example |
|---|---|---|
| HTTP | 80 | http://example.com (port 80 implied) |
| HTTPS | 443 | https://example.com (port 443 implied) |
| FTP | 21 | ftp://files.example.com |
| SSH | 22 | ssh://server.example.com |
| MySQL | 3306 | mysql://db.example.com |
| PostgreSQL | 5432 | postgres://db.example.com |
| Redis | 6379 | redis://cache.example.com |
| MongoDB | 27017 | mongodb://db.example.com |
When the URL API Reports the Port
The browser URL API only populates url.port when a non-default port is explicitly specified:
new URL("https://example.com").port; // "" (empty — default 443)
new URL("https://example.com:443").port; // "" (empty — matches default)
new URL("https://example.com:8080").port; // "8080"
new URL("http://example.com:3000").port; // "3000"
Common Development Ports
- 3000 — React (Create React App), Next.js, Express default
- 4200 — Angular CLI default
- 5173 — Vite default
- 5432 — PostgreSQL
- 8080 — Common alternative HTTP, Tomcat, Spring Boot
- 8443 — Common alternative HTTPS
- 9090 — Prometheus, various admin panels
Port Ranges
- 0–1023 — Well-known ports (require root/admin privileges)
- 1024–49151 — Registered ports
- 49152–65535 — Dynamic / private ports
Use Case
Understanding port parsing is essential when configuring reverse proxies like Nginx, building Docker compose files with port mappings, or debugging connectivity issues between microservices. Developers working with local development servers frequently need to parse and construct URLs with non-standard ports.