writeHead()
The ServerResponse method that sends the status code and headers of an HTTP response in one go.
In more detail
After it runs the headers are fixed, so set every header first. If you never call it, Node.js sends the headers when you first write the body.
Related terms
- ServerResponse — The Node.js object a server uses to answer one HTTP request: a Writable stream with methods for setting the status and headers.
- HTTP status code — The three-digit number at the start of a response, such as 200 or 404, that says how the server handled the request.
- HTTP header — A named line of metadata in an HTTP message, such as Content-Type: application/json, that describes the body, the caller or how to handle the exchange.
Taught in
- Native HTTP Server: Request & Response Lifecycles — Node.js Internals & Backend Services
- Routing & Middleware: Path Parsing & Async Context — Node.js Internals & Backend Services