Introduction
...
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.
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
# 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 services start --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 | ||||||
---|---|---|---|---|---|---|
| ||||||
curl -X GET "http://127.0.0.1:50105021/heartbeat" |
The response below indicates that the service is running without any errors.
...
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 | ||||
---|---|---|---|---|
| ||||
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.
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
curl -X GET "http://127.0.0.1:40104021/testGet?firstName=John&lastName=Smith" |
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
{"result": true,"data": {"firstName": "John","lastName": "Smith"}} |
"testPost" API: Success
This API is exactly like testGet but uses the "post" method.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
curl -X POST -H "Content-type:application/json" -H "Accept:application/json" "http://127.0.0.1:40104021/testPost" -d '{"firstName":"John","lastName":"Smith","email":"john@smith.com"}' |
...
This request fails because of the invalid firstName "johnx" (Instead of "john"). This restriction is specified in the service itself.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
curl -X POST -H "Content-type:application/json" -H "Accept:application/json" "http://127.0.0.1:40104021/testPost" -d '{"firstName":"Johnx","lastName":"Smith"}' |
...
SOAJS also provides supports the PUT method:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
curl -X PUT -H "Content-type:application/json" -H "Accept:application/json" "http://127.0.0.1:40104021/testPut" -d '{"firstName":"John","lastName":"Smith"}' |
...
SOAJS also provides supports the DEL protocol:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
curl -X DELETE "http://127.0.0.1:40104021/testDel?firstName=John&lastName=Smith" |
...
In this first request, both "firstName" and a "lastName" parameters are supplied.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
curl -X GET "http://127.0.0.1:40104021/buildName?firstName=James&lastName=Smith" |
...