Sub topic 10
What a server does with a request
This chapter assumes you can read an HTTP request and response, and that you know methods, status codes, and headers from earlier chapters. Here you will follow one request through a server and see where each of those pieces is used.
The request lifecycle
A server turns a request into a response in a fixed order of steps. Frameworks hide these steps, but they all follow the same pattern.
- Parse. The server reads bytes from the TCP connection and splits them into the method, path, headers, and body. A malformed request gets a
400 Bad Request. - Route. The server picks code to run based on the method and path.
GET /index.htmlandPOST /index.htmlcan reach different code. - Handle. The handler does the work. It might read a file, query a database, or check a cookie.
- Respond. The server writes a status line, headers, a blank line, and a body.
A server in code
This Node.js server routes on method and path and returns a different status for each outcome.
import { createServer } from "node:http";
const server = createServer((req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);
if (req.method === "GET" && url.pathname === "/") {
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Home page\n");
} else if (url.pathname === "/") {
res.writeHead(405, { Allow: "GET" });
res.end();
} else {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end(`Nothing at ${url.pathname}\n`);
}
});
server.listen(3000);The handler checks the path first and the method second. A known path with the wrong method gets 405 Method Not Allowed and an Allow header. An unknown path gets 404 Not Found.
You can test each branch with curl:
curl -i http://localhost:3000/
curl -i -X POST http://localhost:3000/
curl -i http://localhost:3000/missingCommon errors
| Symptom | Likely cause |
|---|---|
| Browser shows a 404 for a file that exists | The route or document root does not match the path in the request |
| 405 on a form submit | The route accepts only GET, but the form sends POST |
| Request hangs with no response | The handler never calls `res.end()` |
| Garbled text in the browser | The `Content-Type` header is missing or has the wrong charset |
| 500 Internal Server Error | The handler threw an exception that nothing caught |
Choosing the right status
Status codes come from what the handler found, not from the route alone. Return 200 when the work succeeded, 404 when the resource does not exist, 400 when the client sent bad input, and 500 when the server itself failed. The client reads the status code before it reads the body, so a wrong code misleads caches, browsers, and scripts.
Exercise
A client sends DELETE /items/5 to a server whose route for /items/5 only allows GET and PUT. Which status code and header should the server return?
Show answer
It should return 405 Method Not Allowed with an Allow header such as "Allow: GET, PUT". The path exists, so 404 would be wrong. The method is the problem.
Exercise
Your handler runs without errors, but curl waits forever for a response. What is the most likely cause?
Show answer
The handler never finished the response, for example by not calling res.end(). The server accepted the request, but the client never receives a complete response.