How The Internet Works
Before writing a single line of backend code, you need to understand what happens when you type a URL into your browser. This foundational knowledge makes everything else click into place.
The Big Picture
When you visit a website, you are not "going" anywhere. Your computer is having a conversation with another computer somewhere in the world. Your browser (the client) sends a request, and a web server responds with data. This is the client-server model.
┌─────────────────────┐ ┌─────────────────────┐
│ YOUR COMPUTER │ │ WEB SERVER │
│ (Client) │ │ (Backend) │
└─────────────────────┘ └─────────────────────┘
│ │
│ ══════ HTTP Request ═════════════════► │
│ "GET /index.html" │
│ │
│ ◄═════ HTTP Response ════════════════ │
│ HTML, CSS, JS, Images │
│ │
Think of it like ordering food at a restaurant. You (the client) tell the waiter (HTTP) what you want. The waiter takes your order to the kitchen (server), which prepares your food (data) and sends it back via the waiter. You never go to the kitchen yourself - you just make requests and receive responses.
What is a Server?
A server is just a computer that is always on, connected to the internet, and running software that listens for incoming requests. When a request arrives, it processes it and sends back a response.
There is nothing magical about servers - they are regular computers (often more powerful) running special software. Your own laptop can be a server if you run the right program on it.
Types of Servers
- Web Server - Serves websites and web applications (Apache, Nginx, Node.js)
- Database Server - Stores and retrieves data (MySQL, MongoDB, PostgreSQL)
- File Server - Stores and shares files across a network
- Mail Server - Handles sending and receiving emails
- API Server - Provides data to applications (what we will build!)
IP Addresses - Finding Computers on the Internet
Every device connected to the internet has a unique address called an IP address. It is like a phone number for computers.
# IPv4 - Traditional format (32-bit)
192.168.1.1 # Your home router (private)
8.8.8.8 # Google's DNS server
142.250.190.78 # One of Google's web servers
# IPv6 - Newer format (128-bit) for more addresses
2001:4860:4860::8888 # Google's DNS in IPv6
# Special addresses
127.0.0.1 # localhost - your own computer
0.0.0.0 # All network interfaces
IPv4 addresses are running out (only ~4.3 billion possible), which is why IPv6 was created. But we will mostly work with IPv4 in local development.
DNS - The Internet's Phone Book
Remembering 142.250.190.78 to visit Google would be terrible. That is why we have the Domain Name System (DNS). It translates human-friendly names like google.com into IP addresses.
You type: google.com
│
▼
┌─────────────────────┐
│ DNS Server │
│ │
│ google.com ──────► │ ────► 142.250.190.78
│ │
└─────────────────────┘
│
▼
Browser connects to 142.250.190.78
You type a URL - Your browser checks its cache for a recent DNS lookup.
Query DNS Server - If not cached, your computer asks a DNS server (usually your ISP's or 8.8.8.8).
DNS Resolution - The DNS server may query other DNS servers in a hierarchy until it finds the IP.
IP Returned - Your computer receives the IP address and can now connect to the server.
Ports - Multiple Services, One Computer
A single server can run multiple services (web, email, database). Ports differentiate between them. Think of IP as a building address and port as the apartment number.
# Well-known ports (0-1023) - require admin privileges
80 # HTTP - unencrypted web traffic
443 # HTTPS - encrypted web traffic (SSL/TLS)
22 # SSH - secure shell for remote access
21 # FTP - file transfer
25 # SMTP - sending email
53 # DNS - domain name resolution
# Registered ports (1024-49151)
3000 # Common for Node.js development
3306 # MySQL database
5432 # PostgreSQL database
27017 # MongoDB database
6379 # Redis cache
8080 # Alternative HTTP port
When you visit https://google.com, you are actually connecting to google.com:443. The :443 is implied because HTTPS defaults to port 443.
In development, we often use ports like 3000 or 8080 because ports below 1024 require administrator privileges. That is why you will see localhost:3000 throughout this course.
HTTP - The Language of the Web
HTTP (HyperText Transfer Protocol) is the protocol that defines how messages are formatted and transmitted on the web. Every web interaction uses HTTP (or its secure version, HTTPS).
HTTP Request Structure
# Request Line: METHOD PATH HTTP-VERSION
GET /api/users HTTP/1.1
# Headers: metadata about the request
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Accept: application/json
Authorization: Bearer eyJhbGciOiJIUzI1...
# Empty line separates headers from body
# Body (optional - used in POST, PUT requests)
{
"name": "John",
"email": "john@example.com"
}
HTTP Response Structure
# Status Line: HTTP-VERSION STATUS-CODE REASON
HTTP/1.1 200 OK
# Response Headers
Content-Type: application/json
Content-Length: 1234
Date: Mon, 15 Jan 2024 10:30:00 GMT
Server: nginx/1.18.0
# Empty line
# Response Body
{
"users": [
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
}
HTTP Status Codes
Status codes tell the client what happened with their request. They are grouped by the first digit:
Status Code Categories
- 1xx (Informational) - Request received, continuing process
- 2xx (Success) - Request successfully received and processed
- 3xx (Redirection) - Further action needed to complete request
- 4xx (Client Error) - Request contains bad syntax or cannot be fulfilled
- 5xx (Server Error) - Server failed to fulfill a valid request
# Success
200 OK # Standard success response
201 Created # Resource created (POST success)
204 No Content # Success but no body (DELETE success)
# Redirection
301 Moved Permanently # URL has permanently changed
302 Found # Temporary redirect
304 Not Modified # Cached version is still valid
# Client Errors
400 Bad Request # Malformed request syntax
401 Unauthorized # Authentication required
403 Forbidden # Authenticated but not allowed
404 Not Found # Resource does not exist
422 Unprocessable Entity # Validation error
429 Too Many Requests # Rate limiting
# Server Errors
500 Internal Server Error # Generic server error
502 Bad Gateway # Invalid response from upstream
503 Service Unavailable # Server temporarily overloaded
504 Gateway Timeout # Upstream server timeout
The Complete Request Journey
Let us trace what happens when you type https://example.com/about and press Enter:
URL Parsing - Browser parses the URL: protocol (https), domain (example.com), path (/about)
DNS Lookup - Browser asks DNS for the IP address of example.com
TCP Connection - Browser establishes a connection to the IP on port 443
TLS Handshake - Since HTTPS, browser and server establish encrypted connection
HTTP Request - Browser sends GET /about HTTP/1.1 with headers
Server Processing - Server receives request, runs code, queries database if needed
HTTP Response - Server sends back status code, headers, and HTML body
Rendering - Browser parses HTML, requests CSS/JS/images, renders the page
HTTPS and Security
HTTPS is HTTP with encryption. It uses TLS (Transport Layer Security) to encrypt all data between client and server. This prevents:
- Eavesdropping - No one can read your data in transit
- Tampering - Data cannot be modified without detection
- Impersonation - Certificates verify server identity
In production, ALWAYS use HTTPS. Modern browsers mark HTTP sites as "Not Secure." For local development, HTTP on localhost is fine since traffic never leaves your machine.
Video Resources
Knowledge Check: Day 1
1. What does DNS do?
2. What HTTP status code indicates a resource was not found?
3. Why do we use port 3000 in development instead of port 80?
4. In the client-server model, which sends the request?
Day 1 Complete!
You now understand the fundamentals of how the internet works. You know about clients and servers, IP addresses, DNS, ports, HTTP, and status codes. This knowledge will help you understand everything we build going forward.
Tomorrow, we will use this knowledge to create your very first Node.js server!