Sub topic 1
Clients and servers: the big picture
Prerequisites
This chapter assumes you can use a web browser, know that a web page is made of HTML, and can run a command in a terminal. You do not need to know any networking.
The two roles
The web is built on two roles. A client is a program that asks for something. A server is a program that waits for requests and answers them. A browser is the most common client. A server is software running on a computer that is connected to the internet all the time.
These are roles, not kinds of machines. One laptop can run a browser (client) and a development server at the same time. A server can also act as a client when it calls another service.
The client always starts the exchange. A server does not send a page to a browser that has not asked for it. The pair of messages is called a request and a response.
What happens when you type a URL
Suppose you type https://example.com/index.html and press Enter. The browser does these steps:
- It reads the URL to find the server's name and the resource you want.
- It looks up the server's address (DNS).
- It opens a network connection to that address (TCP, and TLS for HTTPS).
- It sends an HTTP request.
- The server sends back an HTTP response.
- The browser reads the response and draws the page.
The later chapters in this tutorial take each step in turn. For now, keep the overall order in mind.
Seeing it yourself
You can play the client role from a terminal. The -v flag makes curl print the request it sends and the response it receives.
curl -v https://example.com/Lines that begin with > are what the client sent. Lines that begin with < are what the server returned. The browser does the same exchange, then goes on to render the HTML.
A server in a few lines
A server is not magic. This Node.js program listens on port 3000. For each request, it reads the method and path, then writes back a status, headers, and a body.
import { createServer } from "node:http";
const server = createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
res.end(`You requested ${req.method} ${req.url}\n`);
});
server.listen(3000);Save it as server.mjs and run node server.mjs. Then run curl -v http://localhost:3000/hello in a second terminal. Here localhost means your own machine, so both roles run on one computer.
Common mistakes
- Thinking a server is a special kind of hardware. It is a program, and any computer can run one.
- Expecting the server to push a page without a request. In plain HTTP, the client speaks first.
- Forgetting that one page load usually triggers many requests. The HTML refers to images, stylesheets, and scripts, and each is fetched separately.
Exercise
You run the Node.js server above and then run curl http://localhost:3000/hello. What does the terminal print, and which program is the client?
Show answer
It prints "You requested GET /hello". The client is curl, and the server is the Node.js program. Both run on the same computer, which shows that client and server are roles.
Exercise
Put these steps in order: send request, DNS lookup, render page, open connection, parse URL, receive response.
Show answer
Parse URL, DNS lookup, open connection, send request, receive response, render page.