Overview
Theres object represents the HTTP response that an Express app sends when it gets an HTTP request. By convention, the object is always referred to as res (and the HTTP request is req).
http.ServerResponse object.
Properties
res.app
Reference to the Express application instance.res.headersSent
Boolean indicating if the app sent HTTP headers for the response.res.locals
An object that contains response local variables scoped to the request. Available only to the views rendered during that request-response cycle.res.req
Reference to the request object.Methods
res.append(field, value)
Appends the specified value to the HTTP response header. If the header is not already set, it creates the header with the specified value.The header field name
The value to append
res.attachment([filename])
Sets the HTTP responseContent-Disposition header to “attachment”. If a filename is given, it sets the Content-Type based on the extension.
The filename for the attachment
res.cookie(name, value, [options])
Sets a cookie with the specified name and value.The cookie name
The cookie value (objects are JSON-stringified)
Cookie options
domain- Domain name for the cookieexpires- Expiry date (GMT). If not specified, creates a session cookiehttpOnly- Makes cookie inaccessible to client-side JavaScriptmaxAge- Expiry time relative to current time in millisecondspath- Cookie path (default:/)secure- Marks cookie for HTTPS onlysigned- Indicates if cookie should be signedsameSite- Value of “SameSite” Set-Cookie attribute
res.clearCookie(name, [options])
Clears the cookie specified by name.The cookie name to clear
Cookie options (must match the options used in
res.cookie())res.download(path, [filename], [options], [callback])
Transfers the file atpath as an “attachment” with the optional filename. Automatically sets the Content-Disposition header.
The path to the file to download
The filename for the download
Options to pass to
res.sendFile()Callback function with signature
(err)res.end([data], [encoding])
Ends the response process. Use to quickly end the response without any data.Data to send
Encoding of the data
This method comes from Node.js core, specifically the
response.end() method of http.ServerResponse.res.format(object)
Performs content-negotiation based on theAccept HTTP header. Uses req.accepts() to select a handler for the request.
Object with MIME types as keys and handler functions as values
res.get(field)
Returns the HTTP response header specified by field. The match is case-insensitive.The header field name
res.json([body])
Sends a JSON response. Automatically sets theContent-Type header to application/json.
The data to send as JSON
res.jsonp([body])
Sends a JSON response with JSONP support. Identical tores.json() except it opts-in to JSONP callback support.
The data to send as JSONP
res.links(links)
Sets theLink HTTP header with the provided links object.
Object with link relations as keys and URLs as values
res.location(path)
Sets the responseLocation HTTP header to the specified path.
The location URL
Express passes the specified URL string as-is to the browser without validation or manipulation (except for
back).res.redirect([status], path)
Redirects to the URL with an optional status code (default: 302).HTTP status code (default: 302)
The URL to redirect to
- Basic
- With Status
- Relative
- Back
res.render(view, [locals], [callback])
Renders a view template and sends the rendered HTML string to the client.The name of the view file to render
An object whose properties define local variables for the view
Callback function with signature
(err, html)The callback is optional. If provided, you must explicitly send the response. Otherwise,
res.render() sends the response automatically.res.send([body])
Sends the HTTP response. Thebody parameter can be a Buffer, String, Object, Boolean, or Array.
The data to send
- Buffer →
application/octet-stream - String →
text/html - Object/Array →
application/json - Boolean/Number →
application/json
res.sendFile(path, [options], [callback])
Transfers the file at the given path. Sets theContent-Type response header based on the filename’s extension.
The path to the file (must be absolute unless
root option is provided)Options object
Callback function with signature
(err)maxAge- Sets the max-age property of the Cache-Control header in millisecondsroot- Root directory for relative filenameslastModified- Sets the Last-Modified header (default: enabled)headers- Object containing HTTP headers to serve with the filedotfiles- Option for serving dotfiles. Values: ‘allow’, ‘deny’, ‘ignore’ (default: ‘ignore’)
res.sendStatus(statusCode)
Sets the response status code and sends its string representation as the response body.The HTTP status code
res.set(field, [value])
Sets the response’s HTTP header field to value. To set multiple fields at once, pass an object.The header field name or an object of headers
The value to set (can be array for multiple values)
res.header(field, [value])
res.status(code)
Sets the HTTP status code for the response.The HTTP status code (100-999)
res.type(type)
Sets theContent-Type HTTP header to the MIME type determined by the specified type.
The MIME type or file extension
res.contentType(type)
res.vary(field)
Adds the field to theVary response header, if it’s not already there.
The header field name(s)
Response Status Codes
Common HTTP status codes:- Success (2xx)
- Redirect (3xx)
- Client Error (4xx)
- Server Error (5xx)
200- OK201- Created204- No Content