WebApps 2

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 .

import * as React from 'react';
import { renderToString } from 'react-dom/server';
import router from '@sitevision/api/common/router';
import App from './components/App';

router.get('/', (req, res) => {
   const name = 'Henry';
   res.agnosticRender(renderToString(<App name={name} />), {
      name,
   });
});

res.send(response)

Sends a character response. Type will be text/html if not explicitly set.

res.send('<p>Hello from WebApp!<p>');
res.type('svg')
   .send('<svg version="1.1" ... </svg>');
import { renderToStaticMarkup } from 'react-dom/server';
import router from '@sitevision/api/common/router';
import App from './components/App';

router.get('/', (req, res) => {
   return res.send(renderToStaticMarkup(App({ foo: 'bar' })));
});

res.json(response)

Sends a application/json-response.

res.json({
   id: '123',
   foo: 'bar'
});	

res.set(name, value)

Sets a HTTP-header in the response. Returns this for chaining purposes.

res.set('Cache-Control', 'no-cache')
	.set('X-Content-Type-Options', 'nosniff');

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.type('.html');     // => 'text/html'
res.type('html');      // => 'text/html'
res.type('text/html'); // => 'text/html'
res.type('svg');       // => 'image/svg+xml'
res.type('txt');       // => 'text/plain'

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).

res.sendFile(file);
res.type('application/xml; charset=ISO-8859-1')
	.sendFile(file);

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.status(404)
   .send('Not found');

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.

router.post('/addToBasket', (req, res) => {
   ...
   if (req.xhr) {
      return res.json(json);
   }
   // redirects back to referer
   return res.redirect('back');
});

router.post('/addToBasket', (req, res) => {
   // redirects to the URL derived from the /search path
   return res.redirect('/search');
});

router.post('/addToBasket', (req, res) => {
   // redirect with query string
   return res.redirect('back', { foo: 'bar' }); // url?foo=bar
});

Use a hook or target your route with a standalone url if you want to redirect

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.

Cookie options

Property

Type

Description

Default

Since

name

String

The name of the cookie



value

String

The value of the cookie



httpOnly

Boolean

Flag to prevent client side scripts from accessing the cookie

false

5.0

secure

Boolean

Sets the cookie to be used with HTTPS only

false

5.0

maxAge

Number

The maximum age of the cookie in seconds.

  • A positive value indicates that the cookie will expire after that many seconds.
  • A negative value indicates a session cookie.
  • A zero value deletes the cookie.

-1

5.0

sameSite

String

Determines in what contexts this cookie will be available/sent. Valid values (case-insensitive) are:

  • "None"
  • "Lax"
  • "Strict"

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

res.cookie({
   name: 'basketId',
   value: '123',
   httpOnly: true,
   secure: true,
   maxAge: 3600,
   sameSite: 'Strict'
});

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 '/'.

res.clearCookie('basketId');