← Build Your Own HTTP Server
Stage 3: Frame the Response
stage 3 of 4 · ~20 min · runs in your browser
Clients don't read your intentions — they read bytes. A malformed status
line or a wrong Content-Length and curl hangs forever waiting for bytes
you never promised correctly. Framing is the contract.
Implement buildResponse(status, headers, body) returning the raw
HTTP/1.1 response string:
- status line
HTTP/1.1 <code> <reason>— know at least 200 OK, 201 Created, 204 No Content, 301 Moved Permanently, 400 Bad Request, 404 Not Found, 405 Method Not Allowed, 500 Internal Server Error - your headers, each
Name: value\r\n, in the order given - a correct
Content-Lengthheader (byte length of the body — measure, don't assume; multi-byte characters exist) appended after the given headers - blank line, then the body. A 204 has no body and no Content-Length.
tests (4)
○ a correct 200
○ reason phrases for common codes
○ Content-Length counts bytes, not characters
○ 204 has no body and no Content-Length
buildResponse.js