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 | ||||||
---|---|---|---|---|---|---|
| ||||||
{ "dependencies": { "soajs.nodejs": "*" } } |
2- Features
...
Once the middleware is initialized, the following functionality becomes available:
Function | Type | Input | Output | Remarks |
---|---|---|---|---|
getDatabases | Method | dbName (optional) |
| Mongo Client Driver |
getServiceConfig | Method | - | returns service configuration object | Services Config |
getDeployer | Method | - | returns deployer object | Registry |
getCustom | Method | - | returns custom object | Services Config |
getResources | Method | resourceName (optional) |
| Registry |
getServices | Method | serviceName (optional) |
| Service |
getDaemons | Method | daemonName (optional) |
| Daemon |
reload | Method | - | 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.
...
Property | Code Snippet | Description | Reference | |||||||
---|---|---|---|---|---|---|---|---|---|---|
Tenant |
| Represents the tenant that is making the call to the API. | Multitenancy | |||||||
Key Device Geo |
| Represents the key configuration which belongs to the tenant making the call to the API. | ||||||||
Application |
| Represents the application information which belongs to the tenant making the call to the API. | ||||||||
Package |
| Represents the Productization that the tenant making the call to the API is using. | ||||||||
URAC |
| Represents the User whose access token was found in the request. | URAC | |||||||
Awareness |
| 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 | ||||||
---|---|---|---|---|---|---|
| ||||||
// 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 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
'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.
...