Understanding Request and Response Objects in Express.js
When building web applications with Node.js and Express.js, understanding how to handle incoming requests and craft appropriate responses is fundamental. The
request
req
response
res
The Request Object (`req`)
The
request
The `req` object is your window into what the client is asking for.
It contains details about the client's request, such as the URL, method, headers, and data.
Key properties of the req
object include:
req.url
: The URL path of the request.req.method
: The HTTP method (e.g., 'GET', 'POST').req.headers
: An object containing the request headers.req.params
: An object containing route parameters (e.g.,/users/:id
would havereq.params.id
).req.query
: An object containing URL query string parameters (e.g.,/search?q=express
).req.body
: The data sent in the request body (often parsed from JSON or form data).req.cookies
: An object containing cookies sent by the client (requires cookie-parsing middleware).req.ip
: The IP address of the client.
The Response Object (`res`)
The
response
res
The `res` object is your tool to communicate back to the client.
It allows you to send data, set status codes, and manage headers for the client's response.
Common methods of the res
object include:
res.send(body)
: Sends the HTTP response. The type of thebody
determines theContent-Type
header.res.json(body)
: Sends a JSON response. Sets theContent-Type
toapplication/json
.res.status(statusCode)
: Sets the HTTP status code (e.g.,res.status(200)
).res.sendStatus(statusCode)
: Sets the response HTTP status code and sends its corresponding text description.res.render(viewName, [options], [callback])
: Renders a view template with data.res.redirect(url)
: Redirects the client to a different URL.res.set(field, [value])
: Sets the response header field.res.cookie(name, value, [options])
: Sets a cookie in the response.res.end([data], [encoding])
: Ends the response process.
Putting it Together: A Simple Example
Let's look at a basic Express.js route handler that uses both
req
res
Consider a route that greets a user by name, passed as a URL parameter. The req.params.name
will capture the name from the URL, and res.send()
will construct the greeting message to send back to the client.
Text-based content
Library pages focus on text content
Loading diagram...
Key Concepts and Best Practices
Always remember that req
is for reading incoming data, and res
is for writing outgoing data. You should never modify the req
object to send a response.
When handling POST or PUT requests, you'll often need to parse the request body. Middleware like
express.json()
express.urlencoded()
req.body
req.params.userId
res
object is used to send a JSON response?res.json()
Learning Resources
The official Express.js documentation detailing all properties and methods of the Request object.
The official Express.js documentation detailing all methods available on the Response object for sending data back to the client.
A comprehensive guide to HTTP request headers, which are accessible via req.headers in Express.
An overview of HTTP response headers, which you can set using methods on the res object.
The underlying Node.js core module documentation for IncomingMessage, which Express's Request object extends.
The underlying Node.js core module documentation for ServerResponse, which Express's Response object extends.
Learn about built-in middleware like express.json() and express.urlencoded() for parsing request bodies.
A practical tutorial that demonstrates building an API, showcasing request and response handling in context.
A comprehensive video tutorial covering Node.js and Express, including detailed explanations of request and response objects.
An extensive beginner-friendly video tutorial on Node.js, with sections dedicated to building web servers and handling requests/responses.