WebApps 2

request (req)

The req object is a representation of the HTTP-request.

Properties

req.params

Object that contains parameters passed to a route. The object contains route parameters ('/user/:id'), query string parameters and values in the request body from POST, PUT and DELETE.

import router from '@sitevision/api/common/router';

// GET /user/456
router.get('/user/:id', (req, res) => {
  	console.info(req.params.id); // 456
});

// GET /?foo=bar
router.get('/', (req, res) => {
  	console.info(req.params.foo); // bar
});

// POST /addToBasket (productId=1)
router.post('/addToBasket', (req, res) => {
	console.info(req.params.productId); // 1
});

req.cookies

Object that contains cookies from the request.

import router from '@sitevision/api/common/router';

// Cookie basketId=789
router.get('/', (req, res) => {
   console.info(req.cookies.basketId); // 789
});

req.xhr

Boolean indicating whether or not the request is an XHR (ajax) request.

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

router.get('/', (req, res) => {
   if (req.xhr) {
      return res.json({ foo: 'bar' });
   }

   const foo = 'bar';
   res.agnosticRender(renderToString(<App foo={foo} />), {
      foo,
  });
});

req.session

Session data is stored and accessed through the req.session property. Session attributes are namespaced for WebApps/RESTapps, i.e. "global" session attributes cannot be accessed. Note that session data must be JSON serializable.

// WebApp x
router.get('/', (req, res) => {
   req.session.name = 'John Doe';
});
// WebApp y
router.get('/', (req, res) => {
   console.info(req.session.name); // John Doe
});

req.hostname

Hostname from the HTTP request.

router.post('/', (req, res) => {
   console.info(req.hostname); // developer.sitevision.se
});

req.protocol

Protocol from the request (http or https).

router.get('/', (req, res) => {
   console.info(req.protocol); // https
});

req.secure

Boolean shorthand checking req.protocol === 'https'.

router.get('/', (req, res) => {
   if (req.secure) {
      // ...
   }
});

If a load balancer or proxy is used, make sure that the client request protocol is forwarded to Sitevision.

req.method

HTTP method of the request.

router.use((req, res, next) => {
   if (req.method === 'PUT') {
      return res.status(403);
   } 
   
   next();
});

req.path

Contains the path part of the request URL (the string used to resolve a route).

// example.com/appresource/4.x/12.x/users
router.get('/users', (req, res) => {
   console.info(req.path); // /users
});

req.uri [@since 2023.04.1]

The client request uri (the uri that triggered rendering of this app). Note! The uri is not always equal to "the uri of current page".

// example.com/anything/something
router.get('/', (req, res) => {
   console.info(req.uri); // '/anything/something'
});

req.serverside [@since 2023.08.1]

Whether the request is a server-side one or not. State is true when:

  • The WebApp is rendered via OutputUtil
  • The Sitevision indexing process renders the WebApp (Indexer user)
  • The Sitevision HTML extraction process ("Web archiving") renders the WebApp (typically the Extractor user)
router.get('/', (req, res) => {
   if (req.serverside) {
      // ...
   }
});

Methods

req.updateSession() [@since 2023.03.1]

Re-synchronizes local req.session state with the global/shared App session state. Typically needed when multiple apps are executed intertwined

req.invalidateSession()

Invalidates current session.

req.file(fileParameterName)

Method to retrieve files from a multipart request. Files are exposed as sv:temporaryFile-nodes.

FileUtil and ImageUtil provide useful methods when working with temporary file nodes.

import router from '@sitevision/api/common/router';
import resourceLocatorUtil from '@sitevision/api/server/ResourceLocatorUtil';
import fileUtil from '@sitevision/api/server/FileUtil';

// ...

router.post('/upload', (req, res) => {
   const file = req.file('file');

   const fileNode = fileUtil.createFileFromTemporary(
      resourceLocatorUtil.getFileRepository(),
      file
   );

   res.json({ fileId: fileNode.getIdentifier() }); // 18.xxx
});

req.header(name)

Request headers from the HTTP-request

router.post('/', (req, res) => {
   console.info(req.header('user-agent')); // mozilla....
});