Code Explanation - S3

Let's take a look at the config and index files of S3.


config.js
'use strict';

module.exports = {
   type: 'service',		//either a service or a daemon
   prerequisites: {		//object that is used when deploying the service of daemon in HA mode
      cpu: '',
      memory: ''
   },
   serviceVersion: 1,			//service version
    serviceName: "jsconf3",		//service name
    serviceGroup: "JSConf",		//service group
    servicePort: 4113,			//service port
    requestTimeout: 30,			//time needed before timeout
    requestTimeoutRenewal: 5,	//how many retries before giving up on request
    extKeyRequired: true,		//set to true is service is multi-tenant
    oauth: true,				//set to true if service is secured by oauth
    "errors": {					//object that contains the error codes of the service
       600: "Database Error"
    },
    "schema": {						//object that contains the APIs schema
        "/hello": {						//one API
            "_apiInfo": {				// API label
                "l": "Hello World",
                "group": "Hello",
                "groupMain": true
            },
           "name": {					//API input
              "source": ['query.name'],
              "required": false,
              "default": "John Doe",
              "validation": {
                 "type": "string"
              }
           },
            "email": {				//API input
                "source": ['query.email'],
                "required": true,
                "validation": {
                    "type": "string",
                    "format": "email"
                }
            }
        },
       "/standalone/add": {
          "_apiInfo":{
             "l": "Add Info to Standalone DB",
             "group": "Information",
             "groupMain": true
          },
          "username": {
             "source": ['body.username'],
             "required": true,
             "validation": {
                "type": "string",
                "minLength": 4,			//minimum length of username
                "maxLength": 8,			//maximum length of username
                "pattern": /^[a-zA-Z][0-9a-zA-Z_\-]+$/	//regex to determine if username is valid or not
             }
          },
          "name": {
             "source": ['body.name'],
             "required": false,
             "default": "anonymous",
             "validation": {
                "type": "string"
             }
          },
          "email": {
             "source": ['body.email'],
             "required": true,
             "validation": {
                "type": "array",
                "items": {
                   "type": "object",
                   "properties": {
                      "address": {"type": "string", "format": "email", "required": true},
                      "primary": {"type": "boolean", "required": true}
                   }
                },
                "minItems": 1,		//minimum number of parameters passed in /standalone/add query string
                "maxItems": 5,		//maximum number of parameters passed in /standalone/add query string
                "uniqueItems": true //prevents duplicate ojects
             }
          }
       }
    }
};

The only difference between the config file of S3 and the config file of S2 is that the config file of S3 now sets oAuth to true (by default it is set to false).

index.js
'use strict';
var soajs = require('soajs');								//require soajs
var config = require('./config.js');
var jsconfSRV = new soajs.server.service(config);			//create new service instance

/**
 * Declare a global variable, usage is below
 */
var myMongo;

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

   jsconfSRV.get("/hello", function (req, res) {		//implement the API business logic

      var txt = req.soajs.inputmaskData.name + " <" + req.soajs.inputmaskData.email + "> ";

      return res.json(req.soajs.buildResponse(null, txt)); //generate API response
   });

   jsconfSRV.post("/standalone/add", function(req, res){

      if(!myMongo){
         /**
          * Initializing DB Connection
          */
         req.soajs.log.debug("Creating new DB connection to 'myDatabase' database.");  //output to logs
         var dbConfiguration = req.soajs.registry.coreDB.myDatabase;
         myMongo = new soajs.mongo(dbConfiguration);
      }
      else{
         req.soajs.log.debug("Mongo Instance already created, reusing existing connection.");
      }

      /**
       * Formulating Document to insert
       */
      req.soajs.log.debug("Formulating new record: " + JSON.stringify(req.soajs.inputmaskData));
      var record = {
         'username': req.soajs.inputmaskData.username,
         'name': req.soajs.inputmaskData.name,
         'email': req.soajs.inputmaskData.email
      };

      myMongo.insert("data", record, true, function (error) {
         if (error) {
            req.soajs.log.error(error);
            return res.jsonp(req.soajs.buildResponse({'code': 600, 'msg': config.errors[600]}));
         }

         return res.json(req.soajs.buildResponse(null, true));
      });
   });

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

The index files of S3 and S2 are identical.