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:
- How does it work on init
- How does it work on request
- Practice
- Express example
- Hapi example
How does it work on init
...
Using the following environment variables:
...
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:
...
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
...
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
...
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);
});
|
Full Example on: https://github.com/soajs/soajs.nodejs.hapi