← Build Your Own HTTP Server
Stage 1: Parse the Request
stage 1 of 4 · ~20 min · runs in your browser
A raw HTTP/1.1 request is lines of text separated by \r\n, a blank line,
then an optional body. Before any framework existed, someone wrote this
function — now it's your turn.
Implement parseRequest(raw) returning:
method— e.g."GET"(first token of the request line)path— e.g."/users/42"(second token, without the query string)query— object of decoded query params (?q=db%20engine&page=2→{ q: "db engine", page: "2" });{}when absentheaders— object with lowercased header names (header names are case-insensitive per RFC 9112; values keep their case, surrounding whitespace trimmed)body— everything after the first blank line,""if none
tests (5)
○ parses the request line
○ lowercases header names and trims values
○ splits and decodes the query string
○ captures the body
○ body may itself contain blank lines
parseRequest.js