Daemon Index File
Introduction
The below shows the difference between the index file of a daemon written without the composer, and the index file of a daemon written with the composer.
Index.js
Daemon Index without the Composer'use strict';
//require core framework
var soajs = require('soajs');
//require daemon configuration
var config = require('./config.js');
//require daemon business logic (the model is required inside the business logic)
var BLModule = require("./lib/index");
//create a new daemon instance
var daemon = new soajs.server.daemon(config);
//initialize the daemon
daemon.init(function () {
//implement first JOB
daemon.job("myCustomJob", function (soajs, next) {
//call the initBLModel function ( read more below )
initBLModel(SOAJS, function (error, BL) {
//if error is returned stop the execution and return it
if(error){
return next(error);
}
//pass the service configuration and soajs to the business logic
BL.getCart(config, soajs, function (error, response) {
//formulate a standardized response and return it
return next(error, response);
});
});
});
//start the daemon
daemon.start();
});
//function used to read the model name from the parameter provided
//load it in memory and return an instance to it
function initBLModel(soajs, cb) {
//get the model name from the inputs arriving via the request
var modelName = soajs.servicesConfig[process.env.SOAJS_ENV.toUpperCase()].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) {
if (error) {
return cb(error);
}
//otherwise return the business logic instance via the callback
return cb(BL);
});
} | Daemon Indes with the Composer"use strict";
//require the composer
var composer = require("soajs.composer");
//call composer.deploy to deploy the daemon from its config file
composer.deploy(__dirname + "/config.js", function(error){
//optional log message after daemon 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