Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
titleconfig.js
'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": {
                /**
 * @license
 * Copyright SOAJS All Rights Reserved.
 *
 * Use of this source code is governed by an Apache license that can be
 * found in the LICENSE file at the root of this repository
 */

module.exports = {
	"type": 'service',
	"prerequisites": {
		"cpu": '',
		"memory": ''
	},
	"serviceVersion": 1,
	"serviceName": "example03",
	"serviceGroup": "SOAJS Example Service",
	"requestTimeout": 30,
	"requestTimeoutRenewal": 5,
	"servicePort": 4023,
	"extKeyRequired": true,
	"oauth": true,
	
	"errors": {},
	"schema": {
		"commonFields": {
			"firstName": {
				"source": ['query.firstName'],
                				"required": true,
                				"default": "John",
                				"validation": {
                    					"type": "string"
                }
            },
            				}
			},
			"lastName": {
                				"source": ['query.lastName'],
                				"required": true,
                				"validation": {
                    					"type": "string"
                }
            }
        },
        				}
			}
		},
		"get": {
			"/testGet": {
            				"_apiInfo":{
                 {
					"l": "Test Get",
					"group":            },
            "Example"
				},
				"commonFields": ["firstName", "lastName"]
        			},
        			"/buildName": {
            				"_apiInfo":{
                 {
					"l": "Build Name",
					"group":            },
            "Example"
				},
				"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.

...

Code Block
languagejs
titleconfig.js
'use strict';
var
/**
 * @license
 * Copyright SOAJS All Rights Reserved.
 *
 * Use of this source code is governed by an Apache license that can be
 * found in the LICENSE file at the root of this repository
 */

const soajs = require('soajs');					//require soajs
var 
const config = require('./config.js');

varlet 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 		let tenant = '';
        		if (req.soajs.servicesConfig) {
            			if (req.soajs.servicesConfig.example03) {
                				if (req.soajs.servicesConfig.example03.tenantName) {
                    					tenant = req.soajs.servicesConfig.example03.tenantName;
                }
            }
        }
        var 				}
			}
		}
		let 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.

...