Versions Compared

Key

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

...

The code of oAuth Security Service resembles that of the Basic SOAJS Service. However, in this example, oAuth is used, as will be seen in the code snippets below.

...

Code Block
languagejs
titleindex.js
'use strict';

module.exports = {
    "serviceName": "example02",						//service name
    "serviceGroup": "SOAJS Example Services",		//service group
    "serviceVersion": 1,							//service version
    "servicePort": 4011,							//service port
    "extKeyRequired": false,						//set to true is the service is multi-tenant
    "oauth": true,									//set to true is service is protected by oauth
    "errors": {},									//object that contains the error codes of the service
    "schema": {										//object that contains the APIs schema
        /**
 * @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": "example02",
	"serviceGroup": "SOAJS Example Service",
	"servicePort": 4022,
	"requestTimeout": 30,
	"requestTimeoutRenewal": 5,
	"extKeyRequired": false,
	"oauth": true,
	
	
	"errors": {},
	"schema": {
		"get": {
			"/buildName": {
								//one API
            "_apiInfo": {		
					//API labels
                "l": "Build Name",
					"group":            },
            "Example"
				},
				"firstName": {
							//API input
                "source": ['query.firstName'],
                					"required": true,
                					"default": "John",
                					"validation": {
                    						"type": "string"
                }
            },
            					}
				},
				"lastName": {			
					//API input
                "source": ['query.lastName'],
                					"required": true,
                					"validation": {
                    						"type": "string"
                }
            }
        }
    					}
				}
			}
		}
	}
};

The config file of example02 differs only by one line than that of example01.The only difference between the file of this example, and that of the previous one is that oAuth is set to true in this example.

...

Code Block
languagejs
titleindex.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 the service
    
	service.get("/buildName", function (req, res) {
		//implement the API business logic
        var let fullName = req.soajs.inputmaskData.firstName + ' ' + req.soajs.inputmaskData.lastName;
        		res.json(req.soajs.buildResponse(null, {
			//generate API response
            fullName: fullName
        		}));
    	});
    	service.start();		//start the service
});

The index.js file above contains the implementation of the buildName API, which takes as an input a firstName and a lastName, and then returns a response containing the full name.

...