Versions Compared

Key

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

Objective

...

The NodeJS Middleware provides the ability to modernize existing API with SOAJ when this API is built using NodeJs as a development language.

...

Code Block
languagejs
themeMidnight
titlePackage.json
{
	"dependencies": {
		"soajs.nodejs": "*"
	}
}


2- Features

...

Once the middleware is initialized, the following functionality becomes available:

FunctionTypeInputOutputRemarks
getDatabasesMethoddbName (optional)
  • returns database object if dbName provided
  • returns all core and tenant meta databases otherwise
Mongo Client Driver
getServiceConfigMethod-returns service configuration objectServices Config
getDeployerMethod-returns deployer objectRegistry
getCustomMethod-returns custom objectServices Config
getResourcesMethodresourceName (optional)
  • returns resource object if resourceName provided
  • returns all resources otherwise
Registry
getServicesMethodserviceName (optional)
  • returns service object if serviceName provided
  • returns all services otherwise
Service
getDaemonsMethoddaemonName (optional)
  • returns daemon object if daemonName provided
  • returns all daemons otherwise
Daemon
reloadMethod-void (reload registry only)Registry

This middleware functionality is accessible via the request that is made to the API. The middleware traps the request once it hits the API, injects its functionality and then hands it back to you to add your business logic. 


3-How does it work

...

On Init

The Middleware requires the following environment variables to be set prior to starting your service

...

Once your service starts, the middleware will make a call to the SOAJS API Gateway CON and retrieve the registry of the environment, it will cache it in memory.

...

PropertyCode SnippetDescriptionReference
Tenant


Code Block
languagejs
titlereq.soajs.tenant
tenant : {
    id : '',
    code : ''
}


Represents the tenant that is making the call to the API.

Multitenancy

Key

Device

Geo


Code Block
languagejs
titlereq.soajs[ key | device | geo ]
key : {
    config : {},
    iKey : '',
    eKey : ''
},
device : '',
geo : {}


Represents the key configuration which belongs to the tenant making the call to the API.

Security

Services Config

Application


Code Block
languagejs
titlereq.soajs.application
application : {
    product : '',
    package : '',
    appId : '',
    acl : {},
    acl_all_env : {}
}


Represents the application information which belongs to the tenant making the call to the API.

Multitenancy

Access Levels

Package


Code Block
languagejs
titlereq.soajs.package
package :{
    acl :{},
    acl_all_env : {}
}


Represents the Productization that the tenant making the call to the API is using.

Productization

Access Levels

URAC


Code Block
languagejs
titlereq.soajs.urac
urac : {}


Represents the User whose access token was found in the request.URAC
Awareness


Code Block
languagejs
titlereq.soajs.awareness
awareness : {
    host : '',
    port : '',
    getHost : 'function',
	connect : 'function'
}


Represents the SOAJS API Gateway internal network host & port based on the environment where the API is deployed and running in.Request


4- Using the Middleware

...

Once you download the middleware, simply require it and instruct the web application server to use it.

...

Code Block
languagejs
titleUsing the Middleware with NodeJs
linenumberstrue
// require the mw
const soajsMW = require('soajs.nodejs');

// pass it to your service app
app.use(soajsMW({ serviceName : 'my_service' }));

//...

// invoke req.soajs
app.post('/foo', (req, res) => { 
	//...

	let soajs = req.soajs;
	console.log( JSON.stringify( soajs, null, 2 ) );

	//...
});


5- Examples

...

NodeJs Express

The following code snippets shows a basic example built using NodeJs Express Library and how the SOAJS NodeJs Middleware was added to the code to augment the web server created by express.

...

Code Block
languagejs
themeMidnight
titleNodeJs Express & SOAJS NodeJs Middleware
linenumberstrue
'use strict';

//Create Express App
const express = require('express');
const app = express();

//Require SOAJS Middleware for NodeJs
const soajsMW = require('soajs.nodejs');

const url = require('url');

//Instruct the Express App to use the SOAJS Middleware
app.use(soajsMW({ serviceName : 'my_service' }));

//Invoke the middleware and use it in the Express app API
app.get('/hello', (req, res) => {

	let url_parts = url.parse(req.url, true);
	let query = url_parts.query;

	let username = query.username;
	let lastname = query.lastname;

	res.send({
		"message": `Hello DEMO, I am an EXPRESS service, you are [ ${username} ] and your last name is : [ ${lastname} ]`,
		"dbs" : req.soajs.reg.getDatabases()
	});
});

//Invoke the middleware and use it in the Express app API
app.post('/hello', function(req, res){

	let response = req.soajs;

	req.soajs.awareness.getHost( (host) => {
		response.controller = host;
		res.send(response);
	});
});

//Start the Express app and set it to listen on port 4382
app.listen(4382);

NodeJs Hapi

The following code snippets shows a basic example built using NodeJs Hapi Library and how the SOAJS NodeJs Middleware was added to the code to augment the web server created by hapi.

...