build-an-http-server / lesson-28.md
Lesson 28 · Building responses

Return 404 for a missing file

A request for a file that is not there should return a clean 404, not an error. Today you handle the missing-file case, completing a working static file server.

The goal

Return 404 when a requested file does not exist, and 200 when it does.

Start here - the target
TO DO
Scenario: Serving a missing versus present file
Givena root directory containing "index.html" but not "nope.html"
WhenGET "/nope.html" is served
Thenthe response status is 404
AndGET "/index.html" is served with status 200
Background

Reading a file can fail — most often because it is not there — and the file handler must turn that into a 404 rather than a panic or a blank 200. Any read error becomes “Not Found”; a successful read becomes the 200 you built, with its Content-Type set.

With that, the chapter closes on a genuinely useful program: a static file server. Point it at a directory of HTML and assets, open a browser, and it serves the site — correct status codes, correct content types, missing files handled. The last chapter makes it robust and able to handle many clients at once.

Make it work
data, err := os.ReadFile(full)
if err != nil { // no such file, or unreadable
return Response{Status: 404, Body: []byte("Not Found")}
}
return Response{Status: 200, Body: data, Headers: ...}
CheckpointDONE
You have a working static file server - point it at a directory and browse it. Commit.