Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

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:

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


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.


How does it work 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:


{
  tenant : {
    id : '',
    code : ''
  },
  key : {
    config : {},
    iKey : '',
    eKey : ''
  },
  application : {
    product : '',
    package : '',
    appId : '',
    acl : {},
    acl_all_env : {}
  },
  package :{
    acl :{},
    acl_all_env : {}
  },
  device : '',
  geo : {},
  urac : {},
  awareness : {
    host : '',
    port : '',
    getHost : 'function'
  }
}


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


{
  	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


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

Consequently, anywhere in your APIs you can get your soajs object and use the data / functions as follows:


// 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(); });
});


Express Example



'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



'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);
});


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





  • No labels

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.