response (res)
The res
object is a representation of the HTTP-response.
Methods
res.agnosticRender(string, data)
Accepts server rendered html and initial data that may be used to hydrate the app on the client. The initial data will be received in main.js .
res.send(response)
Sends a character response. Type will be text/html
if not explicitly set.
res.json(response)
Sends a application/json
-response.
res.set(name, value)
Sets a HTTP-header in the response. Returns this
for chaining purposes.
res.type(type)
Sets the response Content-Type
header. If type contains the “/” character, then it will be used as-is. Returns this
for chaining purposes.
res.sendFile(file)
Sends a file as response. Uses best effort for content type matching if no type is explicitly set. File argument must be a Sitevision JCR-Node of type sv:file
, sv:image
or sv:temporaryFile
. Byte array (byte[]
), (java.io.File is also allowed for legacy reasons).
Target your route with a standalone url when delivering files
res.status(code)
Sets the HTTP-status for the response. Returns this
for chaining purposes.
res.redirect(path [, queryStringObject] [, status])
Redirects to a path
with a specified status. If a status
is missing, it defaults to 302. A "back" redirection redirects the request back to the referer, defaulting to / when the Referer header is missing. A ".." redirection redirects to the relative path above current.
The queryStringObject
argument will be converted and added as query string paramater.
Note! Redirecting does not work when the servlet response is committed, i.e. when rendering has started.
Use a hook or target your route with a standalone url if you want to redirect
res.flush() [@since 2024.09.1]
Flushes the current response to the client immediately. Should typically be used only when streaming data (i.e. ai.streamText).
res.cookie(cookie)
Adds a cookie. The cookie
parameter is an object that can have the following properties.
Note! Setting cookies does not work when the servlet response is committed, i.e. when rendering has started.
Property | Type | Description | Default | Since |
---|---|---|---|---|
| String | The name of the cookie | ||
| String | The value of the cookie | ||
| Boolean | Flag to prevent client side scripts from accessing the cookie | false | 5.0 |
| Boolean | Sets the cookie to be used with HTTPS only | false | 5.0 |
| Number | The maximum age of the cookie in seconds.
| -1 | 5.0 |
| String | Determines in what contexts this cookie will be available/sent. Valid values (case-insensitive) are:
Invalid values will be treated as "no value is set by the app" (i.e. no same site attribute will be added to the cookie) | 7.1 | |
| String | If no domain is explicitly specified, the cookie will default to the domain of its creation. For instance, creating a cookie on "example.com" without specifying a domain will result in it being delivered exclusively to "www.example.com" (excluding subdomains). | 2024.03.1 |
The res.cookie
method may also be used to delete cookies by setting its maxAge
to 0. When deleting a cookie, make sure to specify the domain
if it was initially set at the time of addition.
Use a hook or target your route with a standalone url if you want set cookies
res.clearCookie(name [, path])
Clears a cookie given a name. If path is missing, it defaults to '/'. If domain was set when the cookie was added, use the res.cookie
method to delete the cookie.