# Port

> The Port is a physical or virtual interface used to connect a device to a computer or network.

## What is a port number in networking?

The point of connection used to send or receive data. It can be a physical interface like USB or a network port, a number from '0' to '65535' that identifies a service on a host, such as '443' for HTTPS.

- Topic: Basics
- Type: hardware, software

## Example

Wrong:

```js
const app = express();
app.listen(3000);
```

Right:

```js
const app = express();
const port = Number(process.env.PORT) || 3000;
app.listen(port, () => console.log(`Listening on ${port}`));
```

Hosting platforms assign the port through `PORT`; a hardcoded port can leave the app unreachable there.

## Common mistakes

- The app fails to start with 'EADDRINUSE' because another process already uses the port.
- The app works locally but is unreachable on the host because it ignores the assigned port.

## Key points

- The server reads its port from an environment variable such as `PORT` and only falls back to a default.
- Development servers bind to `localhost` unless they must be reachable from other machines.
- Database and admin ports are not exposed to the public internet.

## Interview questions

### What is the difference between port 80 and 443?

Port 80 is the default for plain HTTP and port 443 is the default for HTTPS.

### Why do I need root to use ports below 1024?

On Unix-like systems ports 0 to 1023 are privileged, so binding to them needs extra rights. Use a higher port and put a proxy in front.

### How do I find which process is using a port?

On macOS and Linux run `lsof -i :3000`; on Windows run `netstat -ano` and look for the port.

## Related cards

- [IP Address](https://wonderium.dev/web/ip-address.md)
- [TCP](https://wonderium.dev/web/tcp.md)
- [HTTPS](https://wonderium.dev/web/https.md)
- [`kill <pid>`](https://wonderium.dev/processes/kill.md)
- [.env](https://wonderium.dev/processes/env.md)

---

- Level: [Engineering](https://wonderium.dev/engineering.md)
- HTML version: https://wonderium.dev/engineering/port
- Last updated: 2026-11-25
