Logotype Sitevision Developer
Log in
Log in

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.

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

js
import router from '@sitevision/api/common/router'; // Cookie basketId=789 router.get('/', (req, res) => { console.info(req.cookies.basketId); // 789 });

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.

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

req.hostname

Hostname from the HTTP request.

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

req.protocol

Protocol from the request (http or https).

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

req.secure

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

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

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

js
// 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".

js
// 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 RestApp is invoked via RestAppInvoker.

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

js
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

js
router.post('/', (req, res) => { console.info(req.header('user-agent')); // mozilla.... });
Did you find the content on this page useful?