HTTP messages and status
Local practice: Node.js 22.16.0 on Linux x64; proposed 26.13.1 baseline remains untested. Save each example separately as main.mjs in a disposable directory. No packages or external services required. HTTP examples bind only to loopback and shut down after a finite scenario. Static Node.js code, not verified ByteMind execution.
Inspect a structured HTTP response
const response={status:201,headers:{'content-type':'application/json'},body:'{"id":3}'};console.log(response.status, response.headers['content-type']);console.log(JSON.parse(response.body).id);Run:
node main.mjsExpected stdout:
201 application/json3Exit status: 0. Expected stderr: empty.
How it works
For HTTP/1.1, the request line includes method, target and protocol version. Header fields describe the message, while Content-Type identifies the representation. A 2xx status generally indicates success, 4xx indicates a client-side problem, and 5xx indicates server failure. A connection can carry more than one request. The examples inspect a synthetic message and use Node’s status name lookup; they do not implement an HTTP parser.
Look up a status name
import { STATUS_CODES } from 'node:http';console.log(STATUS_CODES[404]);console.log(STATUS_CODES[503]);Run:
node main.mjsExpected stdout:
Not FoundService UnavailableExit status: 0. Expected stderr: empty.
Common mistake
Do not interpret every JSON response as success. Inspect status and content type separately. Do not parse raw HTTP messages with simple split operations in production.
Practice
For a request for an unknown catalog ID, choose a status and explain the response body you would return.
Further reading: Authoritative source 1 · Authoritative source 2. Internal reference routes remain proposed until repository discovery.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTTP messages and status — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Which element describes the response outcome?
Which header describes the representation media type?
Can one persistent HTTP connection carry multiple requests?