GET /api/users HTTP/1.1π How the Web Works
Before writing JavaScript, you need to understand the protocol that powers the web. Every time you visit a website, your browser speaks HTTP β a simple language of requests and responses.
This module covers the fundamentals of HTTP: how clients talk to servers, what happens inside each message, and the status codes and headers that control everything from caching to authentication.
π― By the end of this module, you will:
- Understand the client-server model and how HTTP requests flow
- Read and interpret HTTP messages (start line, headers, body)
- Use status codes correctly (2xx, 3xx, 4xx, 5xx)
- Know key HTTP headers and their purpose
- See how JavaScript runs in browsers and on the server (Node.js)
- Client-server architecture
- Request/response cycle
- Status codes & headers
- Functions, objects & arrays
- Async: Promises & async/await
- Modern ES6+ syntax
- DOM manipulation
- Events & event handling
- Fetch API & AJAX
- Server-side JavaScript
- File system & modules
- Building APIs
π Course Flow
π HTTP Topology: The Big Picture
Below is a live topology of how HTTP works. Multiple clients (browser, mobile, CLI) send requests to the server, and the server can call external APIs too. Click Play to watch the flow.
π₯ Client & Server: How They Connect
Every HTTP request involves a client and a server.
π Roles per request
A client starts a request; a server answers. In chains (your server β external API) the roles switch per hop.
π Why ports?
One IP, many apps. The OS delivers packets to the process bound to the destination port (e.g. 80, 443, 3000).
π DNS β IP
Resolve example.com to an IP address (cached by TTL). CDNs often answer with a nearby edge server.
π€ Connect, then HTTP
TCP 3βway handshake β TLS (for HTTPS) β send request line + headers β receive response.
π Deep Dive: Ports & Sockets
π Overview
- Wellβknown vs dynamic: 80/443 (HTTP/HTTPS), 22 (SSH), 25 (SMTP), 5432 (Postgres). Clients pick an ephemeral source port per connection.
- One IP, many services: Ports let multiple protocols coexist (web, SSH, mail) on a single interface.
- 4βtuple identity: A TCP connection = (srcIP:srcPort β dstIP:dstPort). This uniquely identifies the conversation.
π Lifecycle
- bind: Process requests a local ip:port.
- listen: Kernel queues SYNs on the listening socket.
- accept: Each accepted connection spawns a new socket (same local port, new remote 4βtuple).
- TIME_WAIT: After close, socket stays around briefly to catch late packets.
π Security
- Firewalls: Rules frequently target destination ports (e.g. allow 443, deny 23).
- Least privilege: Services under 1024 often started via a helper (systemd socket activation, reverse proxy).
- Scanning: Port scans reveal attack surface; rate limiting + stealth ports reduce noise.
π Virtual & Proxy Layers
- Virtual hosting: Same 443 handles many domains via HTTP
Host+ TLSSNI. - Reverse proxy: Edge (443) fans into internal ports (e.g. 3000 API, 3100 auth, 3200 assets).
- Containers: Host port 8080 β container
127.0.0.1:80; kubeβservices abstract pods behind stable ports.
π² Ephemeral Ports
- Range: Often 49152β65535 (Linux configurable via
/proc/sys/net/ipv4/ip_local_port_range). - Selection: Kernel chooses a free port; randomness defends against spoofing.
- Observation: Seeing many adjacent source ports can indicate sequential allocation (old OS behavior).
lsof -iTCP -sTCP:LISTEN -n | grep -E ':(80|443|3000)' # Show listening sockets
ss -tulpn # Modern socket summary
netstat -an | grep 443 # Legacy netstat usage
iptables -L -n | grep 443 # Firewall rule referencing a port
sysctl net.ipv4.ip_local_port_range # Ephemeral rangeπ Deep Dive: DNS Resolution
Core Flow
- App asks OS stub resolver.
- Stub queries recursive (e.g. 1.1.1.1).
- Recursive walks: root β TLD (.com) β authoritative (zone NS).
- Answer cached at each layer until TTL expires.
π Record Types
- A / AAAA: IPv4 / IPv6 address.
- CNAME: Alias chain (never with other data at same node).
- MX: Mail exchangers (with priority numbers).
- TXT: Arbitrary text (SPF, domain verification).
- SRV: Service discovery (host + port + priority/weight).
- CAA: Restrict which CAs can issue certificates.
- NS: Delegation points for subzones.
β±οΈ Caching & TTL
- Layered: Browser β OS β recursive β authoritative.
- Negative: NXDOMAIN cached per SOA
MINIMUMor explicit TTL. - Tuning: Low TTL for active failover; higher TTL for stability/perf.
πΊοΈ Geo & CDN
- Anycast resolvers: One IP, many edge POPs.
- Latency steering: Different A answers per region.
- Health checks: Remove bad endpoints before clients see them.
π Security
- DNSSEC: Signatures prove authenticity (no privacy).
- DoT / DoH: Encrypt resolver channel; hides queries from local network.
- Mitigations: Random source port + query ID thwart basic spoofing.
dig +trace example.com # Full resolution path
dig A example.com @1.1.1.1 # Force specific resolver
dig CAA example.com # Certificate authority authorization
dig _sip._tcp.example.com SRV # Service record lookup
drill -S example.com # DNSSEC validation with drill
nslookup -type=MX example.comπ€ Deep Dive: Connect, then HTTP
Handshake Stack
- TCP: SYN β SYNβACK β ACK establishes reliability metadata (seq/ack windows).
- TLS: ClientHello (SNI, ALPN, cipher suites) β ServerHello (chosen params, cert) β key exchange β Finished.
- HTTP framing: Request line + headers (or HTTP/2 binary frames / HTTP/3 QUIC frames).
1οΈβ£ HTTP/1.1
- Textual, one request at a time per connection (pipeline risks HOL blocking).
- Upgrade:
Connection: keep-alivereduces extra handshakes. - Head-of-line: Large response delays following requests.
2οΈβ£ HTTP/2
- Binary frames over one TCP connection: multiplex streams.
- Header compression (HPACK) saves bytes.
- Still vulnerable to TCP-level HOL blocking if packet loss occurs.
3οΈβ£ HTTP/3 (QUIC)
- Runs atop UDP with built-in encryption + congestion control.
- Stream independence removes TCP HOL blocking latency waterfalls.
- Connection migration: resume if client IP changes (mobile network switch).
π TLS 1.3 & Performance
- Reduced roundβtrips vs TLS 1.2; faster first byte.
- 0βRTT resumption: can send early data (idempotent only!).
- Session tickets rotate keys; too many cause memory pressure.
π Connection Strategies
- Pooling: Limit concurrent connections per origin (browsers enforce caps).
- Coalescing: HTTP/2 reuse across origins sharing certificate SANs.
- Retry logic: Exponential backoff; beware retry storms on partial outages.
π Observability
- TTFB: handshake + server generation time until first byte.
- Waterfall charts: Reveal parallelization and blocking gaps.
- Metrics: handshake duration, TLS reuse %, connection reuse %, 0βRTT hit rate.
curl -v https://example.com/product/42 # Show TLS + request/response
curl --http2 -I https://example.com # Force HTTP/2, headers only
curl --http3 -I https://example.com # Force HTTP/3 (if supported)
openssl s_client -connect example.com:443 -alpn h2,http/1.1 # ALPN negotiation
tcpdump -nn 'port 443' # Inspect SYN/SYN-ACK/ACK
traceroute example.com # Path latency contextAdvanced: routing & sockets
π Reverse proxy routing
443 (Nginx) can route api.example.com β localhost:8000, www.example.com β localhost:3000, assets β localhost:9000.
π Ephemeral client ports
Each connection is a 4βtuple (clientIP:clientPort β serverIP:serverPort), e.g. 198.51.100.24:53214 β 203.0.113.10:443.
π Anycast/CDN
One advertised IP maps to multiple edges; DNS and BGP steer you to a close POP.
π¬ Communication Examples
GET Request
A simple GET request to fetch a list of users. The client specifies it accepts JSON.
GET /api/users HTTP/1.1
Host: example.com
Accept: application/jsonPOST Request
A POST request to create a new user. The body contains JSON data.
POST /api/users HTTP/1.1
Host: example.com
Content-Type: application/json
{"name":"Mehdi","role":"student"}Server Response
A successful response with caching headers, compression, and an ETag for revalidation.
HTTP/1.1 200 OK
Date: Thu, 06 Nov 2025 10:11:12 GMT
Content-Type: application/json; charset=utf-8
Cache-Control: max-age=60, public
ETag: "users:v1:af31b2"
Content-Encoding: br
[{"id":1,"name":"Mehdi"},{"id":2,"name":"Ada"}]Reading the response without the body: Status + headers already tell you success, cacheability, compression, and revalidation strategy (ETag). Monitoring tools often ignore the body entirely.
Beyond GET & POST: HTTP defines more methods β PUT (replace), PATCH (partial update), DELETE (remove). We'll use them all when building REST APIs in Module 5.
π¨ More Message Examples (HTML response, GET with query params)
GET with query parameters
GET /api/users?page=1 HTTP/1.1
Host: example.com
Accept: application/json
<!-- GET requests should not include a body -->HTML Response
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=120, public
<!doctype html>
<html>
<head><title>Example</title></head>
<body>
<h1>Hello from the web server</h1>
</body>
</html>Legend: start line β’ headers β’ blank line β’ body. Tip: GET requests do not include a body; use query parameters or POST for complex searches.
π¨ Anatomy of HTTP Messages
A message = start line + headers + optional body. Each part has distinct semantics.
Intermediaries (proxies, load balancers) often ignore the body for routing decisions. They rely on headers for caching, retries, and compression.
π Status Codes: Semantics & Strategy
Status codes drive caching, retries, SEO, monitoring and auth flows before any body is read. Use precise codes consistently.
| Range | Meaning | Common Codes |
|---|---|---|
| 2xx | Success | 200 OK Β· 201 Created Β· 204 No Content Β· 206 Partial |
| 3xx | Redirect | 301 Permanent Β· 302 Temp Β· 303 See Other Β· 304 Not Modified Β· 307/308 Preserve method |
| 4xx | Client Error | 400 Bad Request Β· 401 Unauthorized Β· 403 Forbidden Β· 404 Not Found Β· 409 Conflict Β· 422 Unprocessable Β· 429 Too Many |
| 5xx | Server Error | 500 Internal Β· 502 Bad Gateway Β· 503 Unavailable Β· 504 Timeout |
Decision tree: (1) Did the request succeed? β 2xx. (2) If not, is the fault on the client (4xx) or server (5xx)? (3) Pick the most specific code β never tunnel errors through 200.
π Status Code Patterns (Design Guide)
π Auth vs Authorization
401 = "who are you?" Β· 403 = "I know you, but no"
401No/expired credentials β includeWWW-Authenticate.403Credentials valid but action forbidden (scope/role).- Rule: Can supplying new credentials help? If yes β 401, else 403.
β Validation & Semantics
400 = malformed Β· 422 = well-formed but invalid Β· 409 = conflict
400Malformed (parse/type error).422Well-formed but domain rule failed.409Version / uniqueness / state conflict.410Resource intentionally removed (stop retrying).
πΎ Caching & Conditionals
200 with ETag β 304 on revalidation
200Fresh; sendETag/Cache-Control.304Validator matched (If-None-Match/If-Modified-Since).- Rule: Always emit a strong validator for cacheable GETs.
π Retries & Resilience
502/503/504 are retryable Β· 500 may not be Β· 429 = back off
502Upstream error (dependency failed).503Temporary overload / maintenance (addRetry-After).504Upstream timeout.- Retry only idempotent methods (GET, HEAD, OPTIONS, safe DELETE, some PUT).
β±οΈ Rate Limiting
429Too Many Requests (quota window exceeded).- Return:
Retry-After,X-RateLimit-Remaining,X-RateLimit-Reset. - Rule: Use 429 (not 403) for transient quota exhaustion.
HTTP/2 429
Retry-After: 5
X-RateLimit-Remaining: 0
{"error":"rate_limit","retry_in":5}π¦ Bulk / Partial
207Multi-Status (mixed results) OR200with per-item status objects.- Prefer 200 + array unless spec needs 207.
{"results":[
{"id":1,"status":201},
{"id":2,"status":409,"error":"duplicate"}
]}π₯ Ranges / Downloads
206Partial Content (serve byte range).416Invalid range request.
HTTP/1.1 206 Partial Content
Content-Range: bytes 0-1023/4096π Idempotent Mutations
PUTreplace β 200 (return body) or 204 (no body).PATCHpartial update β 200/204; use409on version mismatch.DELETEexisting or already deleted β often always 204 to be idempotent.
π Error Envelope
- Shape:
{ error, message?, details?, trace_id? }. - No stack traces in prod; supply correlation id.
- Never use 200 with an error wrapper.
{"error":"conflict","message":"Version mismatch","expected_version":12}Decision Cheatsheet
- Malformed JSON: 400
- Business rule fail: 422
- Concurrent update: 409
- Missing auth: 401
- Not allowed: 403
- Over capacity: 503 + Retry-After
- Rate limited: 429
- Removed resource: 410
π Headers & Content Types
Headers = metadata. Start line + headers often decide caching, routing, and auth before the body is read.
π€ Negotiation
Accept, Content-Type, Accept-Language
Client and server agree on format, language, and encoding of the response.
πΎ Caching
Cache-Control, ETag, Last-Modified, Age
Controls how long responses are stored and when to revalidate with the server.
π Auth
Authorization, WWW-Authenticate
Carries credentials (tokens, API keys) so the server knows who you are.
πͺ Session
Cookie, Set-Cookie
Small data stored by the browser and sent automatically with every request to that domain.
π¦ Compression
Accept-Encoding, Content-Encoding
Reduces payload size over the wire. Server picks the best algorithm the client supports.
π‘οΈ Safety
Content-Security-Policy, HSTS, X-Frame-Options
Prevents XSS, clickjacking, and forces HTTPS. Set by the server in response headers.
π§ Routing
Host, X-Forwarded-*, Traceparent
Determines which backend handles the request. Proxies add forwarding headers for origin info.
βοΈ Request Shaping
Range, If-None-Match, If-Modified-Since
Conditional requests and partial content. Avoid re-downloading unchanged resources.
π Deep Dive: Content Types & Representation
π€ Common Responses
HTML Β· JSON Β· XML Β· Images Β· PDF
π₯ Common Requests
JSON Β· Form URL Encoded Β· Multipart Β· Binary
π’ Versioning
- Media type version (
vnd.*+json) - Path / query
ποΈ Compression
- Textual formats compress well (gzip, brotli).
- Pre-compress static assets for best performance.
Correctness: Wrong Content-Type breaks caches, confuses parsers, and can cause security issues. Be explicit; avoid guessing.
β‘ JavaScript Overview
βοΈ How JavaScript Works
JS engines (like V8, SpiderMonkey) implement the ECMAScript standard. JavaScript is singleβthreaded with an event loop; asynchronous work uses callbacks, promises, and async/await.
Core Concepts at a Glance
Types & Variables
Primitives, type coercion, let/const, functions & arrow functions
Objects & Context
Object literals, prototypes, this binding, classes & constructor patterns
Arrays & Iteration
Functional array methods, higher-order functions, transformations & reductions
Errors & Async
Exception handling, promises, async/await, error propagation patterns
Ready to learn JavaScript? Continue to Module 1: JS Language Core for variables, functions, objects, and more.
π Node.js Overview
Node.js runs JavaScript on the server using Chrome's V8 engine plus system bindings for files, network, and OS access.
π Node.js Architecture
Node.js runs JavaScript on the server using the V8 engine plus system bindings for files, network, and more. It's great for I/Oβheavy apps where non-blocking asynchronous processing shines.
We'll explore Node.js in depth in Module 4: Node.js Fundamentals.
π Summary
π HTTP Protocol
A request/response protocol. Clients send requests, servers answer. Stateless by default.
π₯οΈ Client & Server
Client initiates requests, server listens and responds. Proxies, CDNs, and load balancers sit in between.
π¨ Messages
Start line + headers + optional body. Headers control everything before the body is read.
π Status Codes
2xx success, 3xx redirect, 4xx client error, 5xx server error. Be specific, never tunnel errors through 200.
π Headers
Metadata key-value pairs. Control content negotiation, caching, auth, security, and compression.
β‘ JavaScript
Single-threaded with event loop. Runs in browsers (DOM) and on servers (Node.js).
π’ Node.js
JavaScript runtime built on V8 + libuv. Non-blocking I/O, npm ecosystem, ideal for network services.