How to write encode function on node.js by passing input argument from X3

By | September 21, 2021

Node.js files contain tasks that will be executed on certain events. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. Node.js applications are written in JavaScript, and can be run within the Node.js runtime.Node.js files have extension “._js”

STEP1: Creating Node.js File and save it as gumu.js

Writing Node.js code with build in modules and user defined modules and save it on Root Drive:\Sage\SyracuseComponent\syracuse\bin\node_modules\bundles\ greytrix\lib\gumu.js

Create the folder under bundles(greytrix\lib)

Fig 1.1-Code Description:

The “use strict” Directive:

“use strict”: Defines that JavaScript code should be executed in “strict mode”. It helps to write cleaner code, like preventing from using undeclared variables.

Built-in Modules:

To make Node.js act as an HTTP server include the module. Include the module using require() function with the name of the module.

Server:var http = require(‘http’);

Application has access to the HTTP module, and is able to create a server.

Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).

To identity the httpclient we can declare

Client : var httpClient = require(‘syracuse-httpclient/lib/httpClient’);

Create user defined Modules:

Use the exports keyword to make properties and methods available outside the module file.

Example: exports.TOBASE64=function(_,InData){…..};

STEP2: Passing argument from x3 client

Fig 2.1: X3 script file

Fig 2.1-Code Description:

The function EXEC_JS is located in the ASYRWEBSER library and func ASYRWEBSER.EXEC_JS is used to execute node.js file and its definition is the following

MODULE:

  • The name of the published node.js JavaScript module.

FUNCTION 

  • The name of the JavaScript function to be called.

MODE               

  • A mode to determine if the call is synchronous or asynchronous (‘sync’ or ‘wait’).

ARGUMENTS

  • Multiple arguments delimited by ‘,’ ; It can be JSON format or every types.

ENCODINGS

  • Specify if arguments must be base64 encoded or not. A list of ‘0’ or ‘1’ separated by ‘,’.
  • Can be empty if no base64 encoding is needed for any argument.
  • Important: when it is not an empty string, it must match exactly the number of arguments.
  • Base 64_IN.

CALLB        

  • Specify the location of callback function. This is needed only for ‘wait’ mode.
  • The value depends on the function.
  • -1 is the value to say that the callback location is the last parameter.

RETURNS      

  • Used to filter JSON Objects .

RETURNS_ENC    

  • Specify the needs to encode the response to base64 or not. Can be ‘0’ or ‘1’
  • Base 64_OUT

RESHEAD    

  • The http response header.

RESBODY  

  • The Response body. The response will be stored in this variable.

STATUSCODE         

  • The status code returned by the JavaScript runner module.

STEP 3: Output

Fig 3.1: Encoded output

Note: Restart the Sage Syracuse – NODE0 service when changes done on gumu.js file

This blog helps us to understand how to write encode function on node.js server by passing input parameter from X3 client.