index.js in WebApps

index.js is the entry point and route configuration for a WebApp. The concept is influenced by Express.js. index.js is executed server side, which means SiteVision's Public API can be accessed, while browser objects cannot be accessed.

A simple index.js:

(function() {
   'use strict';

   var router = require('router');

   router.get('/', function(req, res) {
      res.render('/', {});


Routes handle how a WebApp responds to client requests. All WebApps have a unique segment in the URL where routing information is kept. WebApp routes differ from application routes since WebApp routes are local and should be used to keep state for a single WebApp. The router object provides methods to respond to the following request methods:

  • GET
  • PUT
  • POST

A route definition has the following structure:

router.method(path, handler);

Basic examples:

var router = require('router'); // retrieve an instance of the router object

// respond to a GET-request to the root route ('/')
router.get('/', function(req, res) { 
   res.send('Hello from my WebApp!');

// respond to a GET-request to '/user'
router.get('/user', function(req, res){
   res.send('Hello from user route');

Route parameters

Route parameters are used to capture a value in the URL. Below is an example of a path that will capture id on the /user route.

// GET /user/123
router.get('/user/:id', function(req, res) {
 // route is matched and {id: 123} will be populated in the req.params object

Request object (req)

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



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.

// GET /user/456
router.get('/user/:id', function(req, res) {; // 456

// GET /?foo=bar
router.get('/', function(req, res){; // bar

// POST /addToBasket (productId=1)'/addToBasket', function(req, res){; // 1


Object that contains cookies from the request.

// Cookie basketId=789
router.get('/', function(req, res){; // 789


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

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

   return res.render('/', {foo: 'bar'});


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/RESTApp x
router.get('/', function(req, res) { = 'John Doe';
// WebApp/RESTApp y
router.get('/', function(req, res) {; // 'John Doe';


Object containing files from a multi-part request.'/', function(req, res) {
   var myFile =;

req.hostname [@since 4.5.3]

Hostname from the HTTP-request'/', function(req, res) {; //



Invalidates current session.

req.header(name) [@since 4.5.2]

Request headers from the HTTP-request'/', function(req, res) {'user-agent')); //mozilla....

Response object (res)

The res object is a representation of the HTTP-response.


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) [@since 4.5.2]

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'


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

res.send('<p>Hello from WebApp!</p>');	
   .send('<svg version="1.1" ... </svg>');	


Sends a application/json-response.

   id: '123',
   foo: 'bar'


Sends a file as response. Uses best effort for content type matching if no type is explicitly set. File argument must be a or a SiteVision JCR-Node (sv:file or sv:image).

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


Sets the HTTP-status for the response. Returns this for chaining purposes.

   .send('Not found');	

res.render(route, data)

Renders the WebApp. Rendering will always start from the main component. The route parameter will be passed to the data object, which will be the initial state for the WebApp's store.

router.get('/', function(req, res) {
   var data = {
      entries: [],
      buttonText: appData.get('buttonText')
   res.render('/', data);

router.get('/search', function(req, res) {
   var data = {
      entries: [],
      buttonText: appData.get('buttonText')
   res.render('/search', data);

res.redirect(path [, queryStringObject] [, status]) [@since 4.5.3]
res.redirect(path [, status]) pre 4.5.3

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.'/addToBasket', function(req, res) {
   if (req.xhr) {
      return res.json(json);
   // redirects back to referer
   return res.redirect('back'); 
});'/addToBasket', function(req, res) {
   // redirects to the URL derived from the /search path
   return res.redirect('/search'); 
});'/addToBasket', function(req, res) {
	// redirect with query string
   return res.redirect('back', {foo: 'bar'}); //url?foo=bar


Adds a cookie. The cookie is set with httpOnly = false and path = '/'.

   name: 'basketId',
   value: '123'

res.clearCookie(name [, path])

Clears a cookie given a name. If path is missing, it defaults to '/'.