Every Node.js HTTP handler receives two objects, req and res, that represent the incoming request and the outgoing response. This lesson covers their properties and methods in detail.
The req and res Objects
req is an instance of http.IncomingMessage, a readable stream with extra properties describing the request: method, url, headers, and the HTTP version. res is an instance of http.ServerResponse, a writable stream with methods and properties for building the reply: statusCode, setHeader(), write(), and end().
Because both are streams, req emits 'data'/'end' events for reading a body (covered in the http module lesson), and res supports .write() for streaming a response in chunks before finishing with .end().
req.headers is a plain object with lowercase keys.
res.statusCode defaults to 200 if never explicitly set.
res.setHeader() must run before the first res.write()/res.end() call.
res.writeHead(statusCode, headers) can set status and multiple headers at once.
Request & Response Cheatsheet
The properties and methods you'll use on nearly every request.
Object
Property/Method
Purpose
req
req.method
HTTP verb, e.g. 'GET'
req
req.url
Requested path plus query string
req
req.headers
Object of lowercase request headers
res
res.statusCode
HTTP response status, defaults to 200
res
res.setHeader(n, v)
Set a single response header
res
res.writeHead(code, headers)
Set status and headers together
res
res.end(body)
Finish the response, optionally with a final chunk
Reading Query Strings
req.url includes the query string as raw text; parsing it into a usable object is done with the built-in URL class, treating req.url as a relative reference against a base URL (typically built from req.headers.host).
Clients (and API consumers) rely on accurate Content-Type headers and status codes to know how to interpret a response. Getting these consistent across every route is one of the main jobs a framework like Express automates for you.
200 OK for a successful GET/PUT/PATCH.
201 Created for a successful POST that created a new resource.
204 No Content for a successful request with no response body (common for DELETE).
404 Not Found when no matching resource or route exists.
Common Mistakes
Assuming header names preserve their original casing, req.headers keys are always lowercased.
Forgetting res.statusCode defaults to 200, and never explicitly setting it for error cases.
Calling res.setHeader() after res.write()/res.end() has already run, which throws an error.
Manually parsing query strings with string splitting instead of the built-in URL class.
Key Takeaways
req (IncomingMessage) exposes the request's method, URL, and headers as a readable stream.
res (ServerResponse) is a writable stream with status code, headers, and body-writing methods.
Header keys on req.headers are always lowercase.
Use the built-in URL class to parse query strings reliably instead of manual string manipulation.
Pro Tip
Learn the handful of status codes that matter most (200, 201, 204, 400, 401, 403, 404, 500) and use them consistently. Precise status codes make APIs dramatically easier for other developers (and monitoring tools) to work with correctly.
You now understand req and res in depth. Next, learn how to serve static files, HTML, CSS, images, directly from a Node.js server.