Introduction
Service-Oriented Architecture built using JavaScript (SOAJS). Its services can be combined to provide the functionality of a large software application. Each Service is a collection of APIs easily managed by SOAJS framework and productized with multitenant capability. Its embedded self-awareness makes it easier to scale both horizontally and vertically letting all services running on the cloud to communicate effortless and form a massive reliable and scalable infrastructure to empower the communication between the product frontend and backend. SOAJS comes with tons of features, implementing SOAJS framework means getting a boost start of at least 50%.
In this page we will introduce a simple hello world example that demonstrates how easy it is to build a micro-service on top of SOAJS.
A micro-service is a NodeJs server that uses express and listens on a certain port for arriving http traffic.
Hello World
The "hello world" service has one API and when called, this API returns two parameters: firstName & lastName in its response.
This service requires 2 files to function:
- index.js: contains the code of the service and the API implementation.
- config.js: contains the configuration of the service.
module.exports = { "type": 'service', "prerequisites": { "cpu": '', "memory": '' }, "serviceVersion": 1, "serviceName": "helloworld", "serviceGroup": "SOAJS Example Service", "requestTimeout": 30, "requestTimeoutRenewal": 5, "servicePort": 4020, "extKeyRequired": false, "oauth": false, "errors": {}, "schema": { "get": { "/hello": { "_apiInfo": { "l": "hello world", "group": "Example" }, "firstName": { "source": ['query.firstName'], "required": true, "validation": { "type": "string" } }, "lastName": { "source": ['query.lastName'], "required": true, "validation": { "type": "string" } } } } } };
'use strict'; const soajs = require('soajs'); const config = require('./config.js'); let service = new soajs.server.service(config); service.init(function() { service.get("/hello", function(req, res) { let name = req.soajs.inputmaskData.firstName + " " + req.soajs.inputmaskData.lastName; res.json(req.soajs.buildResponse(null, "Hello " + name)); }); service.start(); });
Simply create the service using SOAJS service constructor, pass along the configuration, initialize it and start implementing the APIs.
- soajs.server.service: used to declare a new service with SOAJS and requires the service configuration as a param. More information on what configuration can be provided is explained in Service section under documentation.
- req.soajs.inputmaskData: maps, format, validate all the inputs to the API and consolidates them regardless of the source they arrive from into one object inputmaskData. For additional information, check out IMFV section under documentation.
- buildResponse: used to build a standardized JSON response regardless of the data returned by your API. For more information check out SOAJS Response section under documentation.
Now that you have an idea what a service looks like, let's run it and invoke its APIs.
Start the Service
The Hello World service is available under the soajs.examples repository, you need to go over the readme under soajs.examples to setup the environment
Now we will show you how to start the service and interact with it.
# go to examples/hello_world and start the service cd soajs.examples/hello_world/ # start the soajs example environment sudo soajs services start --env=example # export necessary environment variables to create local awareness export SOAJS_ENV=EXAMPLE export SOAJS_SRVPORT=4020 export SOAJS_DEPLOY_MANUAL=1 export SOAJS_REGISTRY_API=127.0.0.1:21000 # start service node .
Test the Service
Once the service starts, run the following curl command that calls the API of your "hello world" service and check out the response returned.
CURL -X GET "http://127.0.0.1:4020/hello?firstName=John&lastName=Doe"
The service will reply with a JSON response:
{ "result": true, "data": "Hello John Doe" }
That's it!
Conclusion
As you can see, create a micro-service using SOAJS is straight forward and requires minimal effort to be completed.
The next pages shows additional features, concepts and advanced configuration.
You will learn how to:
- protect your service with OAUTH and/or KEY security
- make use of multi-tenancy, transforming your service to behave differently based on the tenant invoking its APIs and load custom configuration per tenant
- deploy a cloud of services and use the CONTROLLER
- install and integrate the URAC and even transform your service APIs to respond differently based on the logged in user invoking them
- Overall view for DevOps & TechOps of how clouds operate under the hood in SOAJS
Add Comment