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 =;



Invalidates current session.

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('Content-Type', 'application/json');	


Sends a text/html-response.

res.send('Hello from WebApp!');	


Sends a application/json-response.

   id: '123',
   foo: 'bar'


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



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

res.status(404).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 [, status])

Redirects to a path with a specified status. If a status is missing, it defaults to 302. Note that redirecting from a regular GET path will not work since the servlet request is commited. Example below shows how to use this method when responding to posted form data.'/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'); 


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