# 3. Getting Started - Hello World In this section, we are using a simple example to demonstrate how to build a "Hello World" data process. After executing this data process, we are going to complete these 3 things (1) parse JSON data from the request body (payload) to print out name (2) print out "Hello World" (3) print out the result of calculations ## Create Data Process To being with the 1^st^ logic (namely, 1.js), we are going to create a few variables and keep them in the session storage for later use, including a string that is parsed from the request body, a hello variable, and a number. ### Create 1^st^ Logic ```javascript // a function that transforms byte array to string function UTF8ArrToStr(aBytes) { let utf8decoder = new TextDecoder(); return utf8decoder.decode(new Uint8Array(aBytes)); } export async function run(ctx) { // read and parse JSON data from the request body const body = JSON.parse(UTF8ArrToStr(ctx.payload.http.body)); const hello = "Hello World!"; const number = 5; // store "body", "hello", and "number" in the session storage // for the 2nd logic to use await ctx.agents.sessionStorage.putJson("body", body); await ctx.agents.sessionStorage.putString("hello", hello); // pass number string await ctx.agents.sessionStorage.putString("number", JSON.stringify(number)); } // The codes in 'handleError' are executed when an error occurrs in Generic Logic, // or the CURRENT running Logic just gets an error. export async function handleError(ctx, error) { ctx.agents.logging.error(error.message); } ``` Once the 1^st^ logic is done, we are going to do some calculations in the 2^nd^ logic, such as addition and multiplication. Afterwards, we will keep these calculated results in the session storage again for later aggregator logic's use. ### Create 2^nd^ Logic ```javascript // a function used to add 1 after passing "num" function addOne(num) { return (num += 1); } // a function used to multiply 2 after passing "num" function multiplyTwo(num) { return (num *= 2); } // a function used to put the results of addition and multiplication into the session storage export async function run(ctx) { const number = parseInt(await // call back "number" that was kept in the session storage in the 1st logic ctx.agents.sessionStorage.get("number")); // use the previous setup function to calculate "number" const addOneResult = addOne(number); const multiplyTwoResult = multiplyTwo(number); await ctx.agents.sessionStorage.putJson("resultFromSecond", { addOneResult, multiplyTwoResult, }); } // The codes in 'handleError' are executed when an error occurrs in Generic Logic, // or the CURRENT running Logic just gets an error. export async function handleError(ctx, error) { ctx.agents.logging.error(error.message); } ``` Lastly, when the 1^st^ and the 2^nd^ logics have been created, you might want to move to the aggregator logic to aggregate and form up your desired results as stated in the [beginning](#Getting-Started---Hello-World-Simple). Here you can expect the response to be (1) the name, parsed from your request body, (2) "Hello World", and (3) the calculation results of 6 and 10. ### Create Aggregator Logic ```javascript export async function run(ctx) { // call back "body", "hello", and "resultFromSecond" that were kept // in the session storage in the 1st and the 2nd logic const bodyResult = await ctx.agents.sessionStorage.get("body"); const helloResult = await ctx.agents.sessionStorage.get("hello"); const resultFromSecond = await ctx.agents.sessionStorage.get( "resultFromSecond" ); // put these results in the response when triggering this data process ctx.agents.result.finalize({ body: bodyResult, hello: helloResult, resultFromSecond, }); } // The codes in 'handleError' are executed when an error occurrs in Generic Logic, // or the CURRENT running Logic just gets an error. export async function handleError(ctx, error) { ctx.agents.logging.error(error.message); } ``` ### Edit YAMLs for API Route and Configuration - API Route: You need to (1) change the method (GET --> POST) because we are going to parse a value from the request body according to [1^st^ logic](###Create-1^st^-Logic); (2) rename the API route name that you favour, such as **Test_0514**; (3) create your own path, such as **/YH/Test_0514**; and leave the rest unchanged. ```yaml method: POST mode: Sync encapsulation: true name: Test_0514 path: /YH/Test_0514 dataProcessPids: - pid: 00000000-0000-0000-0000-000000000000 revision: latest ``` <!-- (You can expect your `api-route-config.yaml` to be like this.) ![](https://hackmd.io/_uploads/HkSbBz0L5.png) --> - Configuration: You need to (1) rename the configuration name that you favour, such as **Test_0514**; (2) rename the names of the aggregator logic, and 2 generic logics that you favour, such as **aggregator_0514**, **generic-1_0514**, **generic-2_0514**; and leave the rest unchanged. ```yaml version: 0.1.0 name: Test_0514 description: description timeoutSeconds: 180 aggregatorLogic: name: aggregator_0514 file: aggregator-logic.js genericLogics: - name: generic-1_0514 file: 1.js - name: generic-2_0514 file: 2.js ``` (You can expect your `config.yaml` to be like this.) ![](https://i.imgur.com/HNWzYaS.png) ### Deploy Data Process :::info Please note that we are going to show you how to [deploy a data process](###Deploy-Data-Process) and [configure an API route](###Configure-API-Route) step by step. As for an advanced method to do both things (deploy a data process and configure an API route at the same time), you can jump to [this section](###Advanced-Deploy-Data-Process-amp-Configure-API-Route). ::: Once the setting above is completed, you can now deploy this "Hello World" data process by using this command: `loc deploy [template name]`. Here we use `loc deploy Test_0514`. ![](https://hackmd.io/_uploads/BkO8hrUPq.png) ### Configure API Route After the data process deployment is done, you will get a permanent ID (PID) for that data process. Next, you must put that PID into the file of `api-route-config.yaml`, and now you can finally configure the API route by using this command: `loc ar deploy -f [template name]/api-route-config.yaml`. Here we use `loc ar deploy -f Test_0514/api-route-config.yaml`. ![](https://hackmd.io/_uploads/SyHKRHLv5.png) ### (Advanced) Deploy Data Process & Configure API Route One major difference of deploying the data process and configure an API route simultaneously is that you DO NOT need to edit the PID into the file of `api-route-config.yaml`. Let's show you how to deploy this "Hello World" data process and configure an API route at the same time by using this command: `loc deploy [template name] -ar`. Here we use `loc deploy Test_0514 -ar`. (You can expect your configuration to be like this.) ![](https://i.imgur.com/f2iNC6L.png) ### Prepare Payload We will use the payload as below: ++**payload**++ ```json { "body": "YH" // change YH to whatever you like } ``` ### Response of Triggering API Route You can use an API client tool such as **[Postman](https://www.postman.com/)** or **[Insomnia](https://insomnia.rest/)** to request an URL. Select ```POST``` and paste the full API route (including server URL). Afterwards, paste the [JSON payload](###Prepare-Payload) you just made. :::info - ++[Basics of API Testing Using Postman](https://www.geeksforgeeks.org/basics-of-api-testing-using-postman/)++ - ++[Understanding Insomnia REST Client Made Easy 101](https://hevodata.com/learn/insomnia-rest-client/)++ ::: Click ```Send``` and if all go well, here is the response that you can expect, in which the body is "YH", the summation is 6 (=5+1), and the multiplication is 10 (=5x2) as well as saying "Hello World". ![](https://i.imgur.com/W4wFkQR.png) ###### tags: `Workshop`