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
...
- 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
This space will show you:
- How does it work on init
- How does it work on request
- Practice
- Express example
- Hapi example
...
Code Block | ||||
---|---|---|---|---|
| ||||
// install from npm
npm install soajs.nodejs |
Code Block | ||||
---|---|---|---|---|
| ||||
// 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:
...
Hence you can now use the following set of functions appended to req.soajs.reg :
Code Block | ||
---|---|---|
| ||
{
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 | ||
---|---|---|
| ||
// 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 | ||||
---|---|---|---|---|
| ||||
'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 | ||||
---|---|---|---|---|
| ||||
'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); }); |
...