A basic SOAJS service is made up of two files:
- config.js: Contains the metadata of the service (name, port, etc...), in addition to that of each of the APIs alongside its corresponding parameters.
- index.js: Contains the definitions of the APIs, that is their respective implementation.
The below snippets represent the content of both index.js and config.js that represent the MultiTenancy Service (example 03).
The code of MultiTenancy Service resembles that of the Basic SOAJS Service. However, in this example, extKeyRequired property is set to true is used.
This property and its uses is explained in details in the code snippets below.
Below each snippet is a small explanation of how each of them works.
'use strict'; module.exports = { "serviceName": "example03", //service name "serviceGroup": "SOAJS Example Services", //service group "serviceVersion": 1, //service version "servicePort": 4012, //service port "extKeyRequired": true, //set to true if the service is multi-tenant "session": true, //set to true when using urac service "errors": {}, //object that contains the error codes of the service "schema": { //object contains the APIs schema "commonFields": { //object that contains fields that are to be reused "firstName": { "source": ['query.firstName'], "required": true, "default": "John", "validation": { "type": "string" } }, "lastName": { "source": ['query.lastName'], "required": true, "validation": { "type": "string" } } }, "/testGet": { "_apiInfo":{ "l": "Test Get" }, "commonFields": ["firstName", "lastName"] }, "/buildName": { "_apiInfo":{ "l": "Build Name" }, "commonFields": ["firstName", "lastName"] } } };
The service configuration of this example has an extra property that didn't exist in the previous examples. Indeed, the extKeyProperty notifies the service that it is a multitenant service.
Therefore, users requesting the usage of the service APIs must be logged in, and provide a valid authentication key. The authentication key serves as an ID of the user. This ID
is used by the service in order to check the users' permission in order to determine the ability to use a service's API.
use strict'; var soajs = require('soajs'); //require soajs var config = require('./config.js'); var service = new soajs.server.service(config); //create new service instance service.init(function () { //initialize service service.get("/testGet", function (req, res) { //implement the API business logic res.json(req.soajs.buildResponse(null, { firstName: req.soajs.inputmaskData.firstName, lastName: req.soajs.inputmaskData.lastName })); }); service.get("/buildName", function (req, res) { var tenant = ''; if (req.soajs.servicesConfig) { if (req.soajs.servicesConfig.example03) { if (req.soajs.servicesConfig.example03.tenantName) { tenant = req.soajs.servicesConfig.example03.tenantName; } } } var name = req.soajs.inputmaskData.firstName + ' ' + req.soajs.inputmaskData.lastName; res.json(req.soajs.buildResponse(null, { // generate API response tenantName: tenant, fullName: name })); }); service.start(); //start the service });
The index.js file implements the two APIs of the service.
The "testGet" API simply returns the first name and the last name sent by the query.
The "buildName" API, on the other returns the tenant name and the fullName sent by the query.
Note that the tenant name is extracted from the req.soajs.servicesConfig which contains information specific to the user logged in to the service.
0 Comments