Skip to content

5. Extending the functionality

leonidumanskiy edited this page Feb 16, 2017 · 5 revisions

Extending the functionality

RData-Server is built to be extendable. To extend functionality of the server, you can provide new methods or controllers.

###Exposing new methods In the server options, you can provide exposed or exposedAnonymously methods. This will add or overwrite existing exposed methods. Exposed methods require user to be authenticated, while methods that are Exposed Anonymously do not require that.

For example, to provide your own authentication method, you need to anonymously expose an "authenticate" method:

var customAuthenticationMethod = function(connection, params, callback){
    connection.userId = params.userId;
    connection.authToken = params.authToken;
    callback(null, connection.authToken);
};

var options = { 
    host: '0.0.0.0',
    port: 8888,
    dbUrl: 'mongodb://localhost:27017/data',
    exposedAnonymously: {
        'authenticate': customAuthenticationMethod 
    } 
};
var server = new RDataServer(options);

This example accepts custom "authToken" in params and writes it into the connection object.

Exposing non-anonymous method is very similar:

var myMethod = function(connection, params, callback){
    callback(null, "result");
};

var options = { 
    host: '0.0.0.0',
    port: 8888,
    dbUrl: 'mongodb://localhost:27017/data',
    exposed: {
        'myMethod': myMethod 
    } 
};
var server = new RDataServer(options);

In both anonymous and non-anonymous custom exposed methods, you must call the "callback" and provide error object as null in first argument, and result object in the second argument (this will be sent to the client).

###Exposing custom controllers Exposing custom controllers is very similar to exposing custom methods. However, exposing controllers gives you more power. The most important thing about controllers is that all controllers are asynchronously initialized with the server object when server starts. This gives you ability to asynchronously initialize your controller using, for example, database connection object. For example, you can build your collections, ensure indexes etc.

Here is the example of custom authentication controller:

function CustomAuthController(server){
    if (this instanceof CustomAuthController === false) {
        return new CustomAuthController(server);
    }
    var self = this;
    self.server = server;
    self.db = server.db;
    
    self.init = function(callback){
        self.db.collection("customAuthCollection").createIndex(
            {"userId": 1},
            null,
            function(err, result) {
                if(typeof callback === 'function')
                    err ? callback(err, null) : callback(null, result);
            }
        );
        return self;
    };
    
    self.customAuthentication = function(connection, params, callback){
        connection.userId = params.userId;
        connection.authToken = params.authToken;
        
        // Check the authtoken using self.db here
        
        callback(null, connection.authToken);
    };
    
    self.exposedAnonymously = {
        'authenticate': self.customAuthentication,
    };
}


var options = { 
    host: '0.0.0.0',
    port: 8888,
    dbUrl: 'mongodb://localhost:27017/data',
    controllers: {
        'customAuth': CustomAuthController
    } 
};
var server = new RDataServer(options);

Clone this wiki locally