Objective
The NodeJS Middleware provides the ability to modernize existing API with SOAJS 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
- How to Install
- Features
- Configuration
- Functionality
- How does it work
- on init
- on request
- Using the Middleware
- Initialization State
- Runtime State
- Examples
- NodeJs Express
- NodeJs Hapi
1- How to Install
Source Code: https://github.com/soajs/soajs.nodejs
// install from npm npm install soajs.nodejs
// 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'
Function | Input | Output | Remarks |
---|---|---|---|
getDatabases | dbName (optional) |
| Mongo Client Driver |
getServiceConfig | - | returns service configuration object | Services Config |
getDeployer | - | returns deployer object | |
getCustom | - | returns custom object | Services Config |
getResources | resourceName (optional) |
| |
getServices | serviceName (optional) |
| Service |
getDaemons | daemonName (optional) |
| 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:
{ 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 set of functions appended to req.soajs.reg (shown in features)
4- Using the Middleware
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(); }); });
5- Examples
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
Add Comment