Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Current »

Introduction


The below shows the difference between the index file of a service written without the composer, and the index file of a service written with the composer.


Index.js



Service Index without the Composer
'use strict';
//require core framework
var soajs = require('soajs');


//require service configuration			
var config = require('./config.js');


//require service business logic (the model is required inside the business logic
var BLModule = require("./lib/index");


//create a new service instance	
var service = new soajs.server.service(config);		

//initialize the service
service.init(function () {		

	//implement first API using method GET
    service.get("/cart/getCart", function (req, res) {		 
		
		//call the initBLModel function ( read more below )
        initBLModel(req, res, function (BL) {
			
			//pass the service configuration and soajs to the business logic			
            BL.getCart(config, req.soajs, function (error, response) {		


				//formulate a standardized response and return it
                return res.json(req.soajs.buildResponse(error, response));	
            });
		});
    });

	//same approach as first API
    service.post("/cart/setCart", function (req, res) {
        initBLModel(req, res, function (BL) {
            BL.setCart(config, req.soajs, function (error, response) {
                return res.json(req.soajs.buildResponse(error, response));
            });
        });
    });

	//same approach as first API
    service.delete("/cart/emptyCart", function (req, res) {
        initBLModel(req, res, function (BL) {
            BL.emptyCart(config, req.soajs, function (error, response) {
                return res.json(req.soajs.buildResponse(error, response));
            });
        });
    });

	//start the service
    service.start();		
});

//function used to read the model name from the inputs provided
//load it in memory and return an instance to it
function initBLModel(req, res, cb) {


	//get the model name from the inputs arriving via the request
    var modelName = req.soajs.inputmaskData.model;		
    
	//call BLModule.init and send the model name to load the later in memory 
	//and return an instance for it via its callback
	BLModule.init(modelName, function (error, BL) {


		//throw an error if model injection failed for some reason
        if (error) {
            req.soajs.log.error(error);		
            return res.json(req.soajs.buildResponse({"code": 407, "msg": config.errors[407]}));  
        }
        else {


			//otherwise return the business logic instance via the callback to use it in the APIs
            return cb(BL);  
        }
    });
}
Service Indes with the Composer
"use strict";
//require the composer
var composer = require("soajs.composer");		


//call composer.deploy to deploy the service from its config file
composer.deploy(__dirname + "/config.js", function(error){


	//optional log message after service is started
    console.log( (error)? error : "Cart Service started ...");	
}); 

As you can see above, the difference is quite significant. The composer takes away all the code you need to write in the index by parsing and rendering the information from the config.js file only.

So what should we add in the config.js so that the composer can do all this magic ? Read More

  • No labels