The following is a simple example service built using SOAJS, containing a number of APIs, each of which uses a different method: GET, POST, PUT, and DEL.
The example focuses on how easy it is to create a micro-service and its APIs as well as how to use the IMFV.
The Input mapping, filtering and validation (IMFV) is an engine provided by the SOAJS framework and is responsible to fetch and clean the inputs arriving to your API at runtime.
...
Example01 can be found inside the soajs.examples/example01/.
Code Walkthrough
...
This page shows you how to interact with example01 service after you install it and run it.
The code walkthrough is located in a sub page and explained in depth. You can click on this link to learn more about how the code works.
Start The Service
...
Code Block
language
bash
title
Running the service
linenumbers
true
# go to correct directory examples/example01 and start the service
cd /opt/soajs/node_modules/soajs.examples/example01/
# start the soajs example environment
sudo soajs servicesstart--env=example
# export necessary environment variables to create local awareness
export SOAJS_PROFILE=/opt/soajs/node_modules/soajs.utilities/data/getStarted/profile.jsENV=EXAMPLE
export SOAJS_SRVPORT=4021
export SOAJS_ENVDEPLOY_MANUAL=test1
export SOAJS_SRVIPREGISTRY_API=127.0.0.1:21000
# start service
node .
Each running service listens on two ports: The data port and the maintenance port.
The service in this example runs on the local machine, and listens on port 40104021.
The second port is the maintenance port. The convention we use at SOAJS for maintenance ports is maintenance port = data port + 1000 therefore, the maintenance port is 5010 for this service.
...
Code Block
language
bash
title
Heartbeat
linenumbers
true
curl -X GET "http://127.0.0.1:50105021/heartbeat"
The response below indicates that the service is running without any errors.
Having checked the health of the service, let's invoke the remaining APIs.
...
Parameter
Mandatory
Type
Source
Format
firstName
YES
String
Query String
N/A
lastName
YES
String
Query String
N/A
email
NO
String
Query String
Email only
"testGet" API: Failure
The first API to test is the "TestGet". In this scenario, the request is sent with only the "firstName" input. The request does not contain the second mandatory input: "lastName". So we expect the response to return with an error.
Code Block
language
bashjs
title
TestGet request: Missing Last Name
curl -X GET "http://127.0.0.1:40104021/testGet?firstName=John"
As expected, the API returned an error response. Since "lastName" is a required field and is missing in the querystring, the IMFV stopped the request returned an error response.
This API receives a "firstName" and a "lastName" parameters, and returns the resulting fullName. However, the "firstName" parameter is not required, and if it is not sent within the request, the API has a default value to replace the missing one.
In this first request, both "firstName" and a "lastName" parameters are supplied.
Code Block
language
bashjs
title
BuildName request: Success
linenumbers
true
curl -X GET "http://127.0.0.1:40104021/buildName?firstName=James&lastName=Smith"