Versions Compared

Key

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

Objective

As known, SOAJS offers a wide range of solutions for APIs.

Assuming you are using a different technology, nodeJS based, now using the 'soajs.nodejs' middleware, you can benefit from all SOAJS advantages.

Source Code:

...

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

The middleware exposes several functionality and configuration that SOAJS and which can be consumed by the Microservice upon initialization or during runtime when requests to its APIs arrive.

This page explains the NodeJs middleware; the functionality and configuration that it offers, how to install it and how to use it.

Scenario

...

  1. How to Install 
  2. Features
    1. Configuration
    2. Functionality
  3. How does it work
    1. on init
    2. on request
  4. Using the Middleware
    1. Initialization State
    2. Runtime State
  5. Examples
    1. NodeJs Express
    2. NodeJs Hapi


1- How to Install

...

Source Code: https://github.com/soajs/soajs.nodejs

This space will show you:

  1. How does it work on init
  2. How does it work on request
  3. Practice
  4. Express example
  5. Hapi example

...


Code Block
languagejs
themeMidnight
// install from npm
npm install soajs.nodejs


Code Block
languagejs
themeMidnight
// require it in your project
const soajsMW = require('soajs.nodejs');

2- Features

...

Once the mw is initialized, the following list of functions will be available to use through 'req.soajs.reg'

FunctionInputOutputRemarks
getDatabasesdbName (optional)
  • returns database object if dbName provided
  • returns all core and tenant meta databases otherwise
Mongo Client Driver
getServiceConfig-returns service configuration objectServices Config
getDeployer-returns deployer object
getCustom-returns custom objectServices Config
getResourcesresourceName (optional)
  • returns resource object if resourceName provided
  • returns all resources otherwise

getServicesserviceName (optional)
  • returns service object if serviceName provided
  • returns all services otherwise
Service
getDaemonsdaemonName (optional)
  • returns daemon object if daemonName provided
  • returns all daemons otherwise
Daemon
reload-void (reload registry only)Registry

 

3-How does it work

...

On Init:

Using the following environment variables:

  • SOAJS_REGISTRY_API
  • SOAJS_ENV

the middleware is going to invoke the controller and get the registry. Using the service configuration provided, the mw will auto reload the registry every "serviceConfig.awareness.autoRelaodRegistry"

Once the registry is loaded, the middleware is now ready to act on request.

...


On request:

Once a request is reached, the SOAJS controller will be passing through the request headers a 'soajsinjectobj' which will be mapped within the middleware to the following standard object:

...

Hence you can now use the following set of functions appended to req.soajs.reg :

Code Block
languagejs
{
  	getDatabases: returns database object if dbName provided, if not, return all core and tenant meta databases
	getServiceConfig: returns service configuration object
	getDeployer: returns deployer object
	getCustom: return custom object
	getResources: returns resource object if resourceName provided, if not returns all resources
	getServices: returns service object if serviceName provided, if not, returns all services
	getDaemons: returns daemon object if daemonName provided, if not, returns all daemons
	reload: reload registry
}

Practice

(shown in features)


4- Using the Middleware

...

Require this Middle Ware in your restful nodejs service, and append it to your app.

...

Code Block
languagejs
// require the mw
const soajsMW = require('soajs.nodejs');

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

// use it in your requests
app.post('/helloworld', function(req, res){ 
	var soajs = req.soajs;
	// soajs.key.iKey;
	// soajs.reg.getDatabases(); });
});


5- Examples

...

Express Example

...


Code Block
languagejs
themeMidnight
'use strict';

var express = require('express');
var app = express();
const soajsMW = require('soajs.nodejs');

var url = require('url');

app.use(soajsMW({
serviceName : 'test'
}));

app.get('/tidbit/hello', function(req, res){
var url_parts = url.parse(req.url, true);
var query = url_parts.query;

var username = query.username;
var 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()
});
});

app.post('/tidbit/hello', function(req, res){
var response = req.soajs;

req.soajs.awareness.getHost(function(host){

response.controller = host;
res.send(response);
});
});

app.listen(4382);


...

Full Example on: https://github.com/soajs/soajs.nodejs.express

Hapi Example

...


Code Block
languagejs
themeMidnight
'use strict';

const Hapi = require('hapi');
const soajsMW = require('soajs.nodejs')({serviceName : 'test'});
var url = require('url');

const server = new Hapi.Server();
server.connection({
    host: '0.0.0.0',
    port: 4380
});
server.ext({
    type: 'onRequest',
    method: function (request, reply) {
        soajsMW(request, reply, function (err){
            if (err) {
                throw err;
            }
            return reply.continue();
        });
    }
});
server.route({
    method: 'GET',
    path: '/tidbit/hello',
    handler: function (request, reply) {

	    var url_parts = url.parse(request.url, true);
	    var query = url_parts.query;

	    var username = query.username;
	    var lastname = query.lastname;

        return reply({
	        "message": "Hello, I am a HAPI service, you are ["+username+"] and your last name is : ["+lastname+"]"
        });
    }
});

server.route({
	method: 'POST',
	path: '/tidbit/hello',
	handler: function (request, reply) {
		var response = request.soajs;
		request.soajs.awareness.getHost(function(host){
			response.controller = host;
			return reply(response);
		});
	}
});

server.start((err) => {

    if (err) {
        throw err;
    }
    console.log('Server running at:', server.info.uri);
});

...