Ensiklopedia VibeKoding: Principles of Ports and Localhost.Ensiklopedia VibeKoding: Principles of Ports and Localhost.
> π‘ Learning Guide: When you run npm run dev and the terminal shows http://localhost:5173, have you ever wondered: what is localhost? What does 5173 represent? Why do you sometimes get an EADDRINUSE error? This chapter will thoroughly explain these concepts you see every day in development but rarely dig into.> π‘ Learning Guide: When you run npm run dev and the terminal shows http://localhost:5173, have you ever wondered: what is localhost? What does 5173 represent? Why do you sometimes get an EADDRINUSE error? This chapter will thoroughly explain these concepts you see every day in development but rarely dig into.
Before we begin, it's recommended to brush up on two foundational topics:Before we begin, it's recommended to brush up on two foundational topics:
------
localhost:5173 You See Every Day0. Introduction: What Exactly Overview of That localhost:5173 You See Every DayEvery developer's daily routine includes this line of output:Every developer's daily routine includes this line of output:
CODE β Local: http://localhost:5173/
But have you ever thought about how many key concepts are packed into this short line:But have you ever thought about how many key concepts are packed into this short line:
Understand these three things, and you'll be able to figure out 90% of development environment networking issues. Let's break them down one by one.Understand these three things, and you'll be able to figure out 90% of development environment networking issues. Let's break them down one by one.
------
Imagine a server is a building:Imagine a server is a building:
192.168.1.100) is the building's street address β it tells you "which building to go to."An IP address (e.g., 192.168.1.100) is the building's street address β it tells you "which building to go to.":80) is the room number inside the building β it tells you "which room to enter."A port number (e.g., :80) is the room number inside the building β it tells you "which room to enter."A building can simultaneously have a restaurant (room 80), a cafe (room 443), and an office (room 22). Similarly, a single computer can run a web server, a database, and an SSH service simultaneously, each using a different port.A building can simultaneously have a restaurant (room 80), a cafe (room 443), and an office (room 22). Similarly, a single computer can run a web server, a database, and an SSH service simultaneously, each using a different port.
π Try it out:π Try it out:
Click the "room numbers" below to simulate connecting to different ports. Notice what happens when a port is "open" (a program is listening) versus "closed."Click the "room numbers" below to simulate connecting to different ports. Notice what happens when a port is "open" (a program is listening) versus "closed."
A port number is an integer between 0β65535 (65,536 total). These ports are divided into three ranges:A port number is an integer between 0β65535 (65,536 total). These ports are divided into three ranges:
| Range | Values | Purpose | Examples |
|---|---|---|---|
| System Ports | 0 β 1023 | Reserved for standard protocols; ordinary users can't freely use them | 80 (HTTP), 443 (HTTPS), 22 (SSH) |
| Registered Ports | 1024 β 49151 | Registered for common applications | 3306 (MySQL), 5432 (PostgreSQL), 6379 (Redis) |
| Dynamic Ports | 49152 β 65535 | Temporarily assigned by the operating system | When a browser makes a request, the OS randomly assigns a source port |
> Why does your dev server like to use 3000, 5173, or 8080? Because they're all in the "registered ports" range β no admin privileges needed to listen on them, and they're less likely to conflict with system services.> Why does your dev server like to use 3000, 5173, or 8080? Because they're all in the "registered ports" range β no admin privileges needed to listen on them, and they're less likely to conflict with system services.
π Try it out:π Try it out:
Enter a port number or service name to search. Click any row to expand and see usage examples.Enter a port number or service name to search. Click any row to expand and see usage examples.
------
localhost is a special domain name that always points to your own computer.localhost is a special domain name that always points to your own computer.
When you type http://localhost:3000 in your browser, here's what happens:When you type http://localhost:3000 in your browser, here's what happens:
localhost?"The browser asks the OS: "What's the IP for localhost?"127.0.0.1" (no need to look up DNS over the network)The OS answers directly: "127.0.0.1" (no need to look up DNS over the network)127.0.0.1, but never actually leaves your machineThe packet is sent to 127.0.0.1, but never actually leaves your machineThe entire process doesn't go through any network cable, router, or internet connection.The entire process doesn't go through any network cable, router, or internet connection.
π Try it out:π Try it out:
Click "Send Request" to observe the complete journey of a data packet. Then click the "alias cards" below to learn about the different ways to write localhost and their differences.Click "Send Request" to observe the complete journey of a data packet. Then click the "alias cards" below to learn about the different ways to write localhost and their differences.
localhost vs 127.0.0.1 vs 0.0.0.02.2 localhost vs 127.0.0.1 vs 0.0.0.0These three concepts are often confused, but they mean entirely different things:These three concepts are often confused, but they mean entirely different things:
| Notation | Meaning | Who Can Access |
|---|---|---|
localhost / 127.0.0.1 | Loopback address, local machine only | Only your own computer |
0.0.0.0 | Listen on all network interfaces | Local machine + other devices on the LAN |
192.168.x.x | LAN IP | Devices on the local network |
Practical scenarios:Practical scenarios:
bash # Only you can access (secure, good for development) npm run dev -- --host localhost # Your phone can also access (good for mobile debugging) npm run dev -- --host 0.0.0.0
> Many frameworks (like Vite, Next.js) listen on localhost by default, so even if your phone is on the same WiFi, it can't access your dev server. Want to debug on mobile? Just add the --host flag.> Many frameworks (like Vite, Next.js) listen on localhost by default, so even if your phone is on the same WiFi, it can't access your dev server. Want to debug on mobile? Just add the --host flag.
------
One port can only be listened on by one program at a time. It's like one room can only house one family.One port can only be listened on by one program at a time. It's like one room can only house one family.
If you try to start a second service on the same port, you'll see this classic error:If you try to start a second service on the same port, you'll see this classic error:
CODE Error: listen EADDRINUSE :::3000
In plain English: "Room 3000 is already occupied β you can't get in!"In plain English: "Room 3000 is already occupied β you can't get in!"
Common conflict scenarios:Common conflict scenarios:
π Try it out:π Try it out:
Try starting services multiple times in the simulator below. When a port conflict occurs, compare how "direct start" and "smart start" handle it differently.Try starting services multiple times in the simulator below. When a port conflict occurs, compare how "direct start" and "smart start" handle it differently.
When you encounter a port conflict, the troubleshooting steps are very consistent:When you encounter a port conflict, the troubleshooting steps are very consistent:
macOS / Linux:macOS / Linux:
bash # Step 1: Check who is using port 3000 lsof -i :3000 # Step 2: Once you have the PID, force kill it kill -9 <PID>
Windows:Windows:
bash # Step 1: Check who is using port 3000 netstat -ano | findstr :3000 # Step 2: Kill the process taskkill /PID <PID> /F
> Many modern frameworks (Vite, Create React App, etc.) will automatically ask "switch to another port?" when they detect a conflict. But understanding the underlying principles helps you troubleshoot those tricky issues that frameworks can't handle.> Many modern frameworks (Vite, Create React App, etc.) will automatically ask "switch to another port?" when they detect a conflict. But understanding the underlying principles helps you troubleshoot those tricky issues that frameworks can't handle.
------
Browsers have a security mechanism called the Same-Origin Policy: only when the protocol, domain, and port are all identical, are two URLs considered the "same origin."Browsers have a security mechanism called the Same-Origin Policy: only when the protocol, domain, and port are all identical, are two URLs considered the "same origin."
| URL A | URL B | Same Origin? | Reason |
|---|---|---|---|
http://localhost:5173 | http://localhost:5173/about | β Same origin | Protocol, domain, port are all the same |
http://localhost:5173 | http://localhost:3000 | β Different origin | Different ports (5173 vs 3000) |
http://localhost:5173 | https://localhost:5173 | β Different origin | Different protocols (http vs https) |
When your project architecture is:When your project architecture is:
CODE Frontend (Vite) β http://localhost:5173 Backend (Express) β http://localhost:3000
The frontend page loads from :5173, then uses fetch('/api/users') to request the API on :3000 β different ports trigger the cross-origin restriction!The frontend page loads from :5173, then uses fetch('/api/users') to request the API on :3000 β different ports trigger the cross-origin restriction!
Two common solutions:Two common solutions:
Option 1: Configure CORS on the backendOption 1: Configure CORS on the backend
javascript // Express backend app.use(cors({ origin: 'http://localhost:5173' }))
Option 2: Configure a proxy on the frontend (recommended)Option 2: Configure a proxy on the frontend (recommended)
javascript // vite.config.js export default { server: { proxy: { '/api': 'http://localhost:3000' } } }
How the proxy works: The Vite dev server "forwards" requests on your behalf. The browser thinks it's communicating with :5173 (same origin), while Vite secretly relays the requests to :3000 behind the scenes.How the proxy works: The Vite dev server "forwards" requests on your behalf. The browser thinks it's communicating with :5173 (same origin), while Vite secretly relays the requests to :3000 behind the scenes.
------
π Try it out:π Try it out:
Select an issue you've encountered, and follow the steps to troubleshoot. Click "Execute" on each step to see the output.Select an issue you've encountered, and follow the steps to troubleshoot. Click "Execute" on each step to see the output.
------
| English Term | Chinese Translation | Explanation |
|---|---|---|
| Port | η«―ε£ | A number from 0β65535 used to distinguish different network services on the same machine. Each service "listens" on a port, waiting for client connections. |
| localhost | ζ¬ε°δΈ»ζΊ | A special domain name that always points to the local machine (127.0.0.1). Used to access services running on your own machine without an internet connection. |
| Loopback Interface | η―εζ₯ε£ | A virtual network interface in the operating system. Packets sent to 127.0.0.1 never leave the machine; they are "looped back" through this interface. |
| EADDRINUSE | ε°εε·²θ’«δ½Ώη¨ | An error from Node.js / the OS indicating the port you want to listen on is already occupied by another program. |
| CORS | θ·¨εθ΅ζΊε ±δΊ« | A browser security mechanism. When a frontend page tries to request an API from a different origin (different protocol/domain/port), the backend must explicitly grant permission. |
| Same-Origin Policy | εζΊηη₯ | The cornerstone of browser security: only allows free communication between requests with the same protocol, domain, and port, blocking cross-origin data reads. |
| Proxy | 代η | In a dev environment, a proxy server forwards requests from the browser to the backend, bypassing the browser's same-origin restriction. |
| 0.0.0.0 | ζζζ₯ε£ | When a service listens on 0.0.0.0, it accepts connections from any network interface (local machine, LAN, etc.). |
| Well-known Ports | η₯εη«―ε£ | The collective name for ports 0β1023, reserved for standard protocols like HTTP (80), HTTPS (443), SSH (22). |
| PID | θΏη¨ ID | A unique number assigned by the OS to each running program, used to manage and terminate processes. |
| lsof | εεΊζεΌηζδ»Ά | A macOS/Linux command used to see which process is using a specific port (lsof -i :port_number). |
| HMR | η樑εζΏζ’ | A dev server feature: when you modify code, the browser updates automatically without manual refresh. Works via WebSocket notifications under the hood. |
------
Ports and localhost are the most fundamental, most frequently encountered concepts in the development environment:Ports and localhost are the most fundamental, most frequently encountered concepts in the development environment:
Remember these four points, and you'll be able to quickly identify the cause of most networking issues in your development environment.Remember these four points, and you'll be able to quickly identify the cause of most networking issues in your development environment.