Skip to content

Creating Controllers in JS

Controllers are web endpoints handlers that processes a web request and produces a web response.

Controllers are used to expose functionality from a Clarive plugin through a URL, they are needed, for example, to build a dashboard.

The controller is the part that sends response data from the server back to the interface.

Controllers can only be created in a plugin, and should be part of the load init phase of a plugin.

Creating a Controller

To create a controller, just create a JS source file in the init/ directory of your plugin (CLARIVE_BASE/plugins/[plugin-name]/init/) and inside register a new controller:

var reg = require('cla/reg');

    authenticate: false,
    handler: function(req,res){
        res.body( "hello world" );

handler function will run in a new javascript environment on each call, so anything defined outside handler scope will be lost on the next call.

For example:

var reg = require('cla/reg');
var test = 'hello world';
reg.controller('foobar', {
    handler: function(req, res) {
        res.body(test); //error: identifier 'test' undefined

To overcome this limitation we recommend you to put your plugin logic in a separate module and then require from the handler function.

For example:

/* modules/handler.js */
var test = 'hello world';
var another = require('./another.js');
module.exports = function(req, res) {

/* init/myplugin.js */
var reg = require('cla/reg');
reg.controller('foobar', {
    handler: function() {
        return require('handler').apply(this, arguments);

This technique will also give you a better error reports with file name and line number.

Controller URL

Controller URL can be accessed at: http://clariveserver/plugin/[plugin-name]/[controller-name].

For instance, let's suppose you created CLARIVE_BASE/plugins/myplugin/init/controllers.js plugin and registered the following controller:

var reg = require('cla/reg');

    authenticate: false,
    handler: function(req,res){
        res.body( "Hello " + req.username() );

Then you can access this controller at: http://clariveserver/plugin/myplugin/foobar

Controller handler pass 2 arguments, request and response objects.

Request Object

Provides information about the current client request. The request object contains web endpoint input, and is typically sent by the browser.

The request object is the first argument to a Controller handler, which in this document will be identified as req.


Contains an Array of path parts, omitting the first path ("plugin").


Will result in the following Array:

[ 'myplugin', 'mycontroller', 'path', 'part', 'one' ]


IP address of the client.


Shortcut for the Content-Type client header.


Shortcut for the Content-Length client header.


Shortcut for the Content-Encoding client header.


This method returns a cookie value as an Object that has special convenience methods which make it easy to extract the contents of a cookie such as value and date.

reg = require('cla/reg');

reg.controller('mycontroller', {
    handler: function(req,res) {
        var mycookie = req.cookie('mycookie')
            'name=' + +
            'value=' + cookie.value() +
            'path=' + cookie.path() +
            'expires=' + cookie.expires() +
            'maxAge=' + cookie.maxAge()


This method returns all cookies as an Object, with key-value pairs. In contrast to req.cookie(), the values are stringified instead of objects.


Returns the current environment for the call.


Returns value of a given header key.


Returns the request method, ie GET, POST, DELETE etc.

The request method is useful for dispatching different behaviors based on the method requested, like when implementing REST interfaces.


Returns the request query or body (form) parameter for the given key. This includes both GET and POST parameters.

Example, for the following URL request:



print( req.param("foo") );

To get foo's value bar.


Returns the protocol used for the request, such as http or https.


Returns the value for the parameter key given in a request query (from a GET).

This is similar to the req.param() function but only returns query parameters, ignoring body parameters (forms).[maxlength])

Reads a chunk of data from the input.

Use maxlength to limit the number of bytes read. It defaults to the size of the request if not specified.

Returns undefined when the stream has ended, so it's suitable to be used in a while() loop.

var str = '';
var ch;
while( ch = ) {
    str += ch;


Returns the remote user from the header.

Returns true or false, indicating whether the connection is secure (https).


Returns the request URI object.


Returns user agent request header, which typically contains the browser version.


Returns a Clarive user object, which includes the following attributes:

    var user = req.user();; // the Resource object
    user.isRoot(); // true if a user has administrator permissions
    user.username(); // the username id string of the user logged into Clarive
    user.languages(); // returns an Array with the languages the user prefers
    user.hasAction(''); // checks if user has a given action


Returns the user id of the user logged in Clarive.

Response Object

Serving JSON

To send a JSON response, use res.json() method. Clarive will take care of serializing and encoding the data correctly.

    handler: function(req,res){
        res.json({ aa: 100, bb: 200 });

Serving Files

Unicode Response