Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


The quickest way to "test out" a PJS module to see how things work is to create it as a Express Route (web service), and call it using a browser.

Example 1

Sometimes it's convenient to develop PJS applications in a PJS instance installed on your PC, before deploying to a network server or IBMi system. This tip documents how to use commands PJSMYIP andPJSCALL to call a "regular" (i.e. NOT low-code) PJS module on your PC from an IBMi system.

In the following example, we'll create a web service to pass in a customer number = 1234, and get back the customer name  for that customer number"regular" PJS module in your PJS instance on your PC, call it with 2 input parameters, and see the output in the console log.

  • Navigate to the workspace "pjstips".
  • Click New/Javascript File.
  • Enter the code below. Click Save As, and save as pjstips_0207_1.js. Note that this is very simple code, as an example. You can do anything you want here (e.g. use various PJS API's such as pjs.define(), call pjs.query() to get data, use pjs.call() to call an IBMi program, etc).
Code Block
languagejs
titlepjstips_07_1.js.
function app(

...

firstName, 

...

lastName) {

  

...

Image Removed

  • Now, you can "call" that PJS module as a web service from a browser, using the URL http://localhost:8081/run/pjstips/pjstips_02_1 . The format of the URL is http://host:port/run/<Route Path>, where <Route Path> is as shown in the picture above.
  • If you want to pass parameters to the PJS module from the browser, you can use query string parameters, in the format of :
    • <yourURL>?parm1=value1&parm2=value2
  • For example, use this URL http://localhost:8081/run/pjstips/pjstips_02_1?custno=1234 to pass a parm of "custno" with value of "1234" to the PJS module.
  • To debug the module pjstips_02_1.js, put it in debug using your favorite debug tool (e.g. VS Code or Chrome), add a breakpoint, then call it from a browser.

Example 2

The previous example sends back the "response" as a text message. In the next example, we'll send back the reponse as JSON.

...

Code Block
languagejs
function app(req, res) {
  
  // When a PJS module is called as an Express Route (web service), two objects
  // are passed to the module.
  // 1. The "request" object, named as "req" here. You can use another name if you want.
  // 2. The "response" object, named as "res" here. You can use another name if you want.

  // Put this module in debug and call it from a browser. Then examine the "req" and "res" objects passed in, 
  // to see all the good stuff they contain.

  var custno = req.query.custno;          // input parm specified as query string parameter, e.g. ?custno=1234
  var name = `Customer_name_${custno}`;   // get output customer name from input customer number

  // In this example. we'll use function res.json() to send back the "response" as a JSON object
  var data = 					// format the "response" as a JSON object like this
  {
    "custno": custno,
    "name"  : name
  };

  res.json(data);

}

exports.run = app;

...

  • Image Removed

...

Example 3

In the next example, we'll create a web service that's supposed to be called as a POST request, with a "body" that contains input parameters as a JSON object , instead of a GET request with input parms specified as query string parameters.

...

Code Block
languagejs
function app(req, res) {
  
  // When a PJS module is called as an Express Route (web service), two objects
  // are passed to the module.
  // 1. The "request" object, named as "req" here. You can use another name if you want.
  // 2. The "response" object, named as "res" here. You can use another name if you want.

  // Put this module in debug and call it from a browser. Then examine the "req" and "res" objects passed in, 
  // to see all the good stuff they contain.

  var custno = req.body.custno;           // input parm specified in the "body" of the POST request
  var name = `Customer_name_${custno}`;   // get output customer name from input customer number

  // In this example. we'll use the function res.json() to send back the "response" as a JSON object
  var data = 
  {
    "custno": custno,
    "name"  : name
  };

  res.json(data);

}

exports.run = app;

...

  • Image Removed

...

pjs.define("firstName",   { type: 'char', length: 30,  refParm: firstName });
  pjs.define("lastName" ,   { type: 'char', length: 30,  refParm: lastName  });
  
  let fullName = `${firstName.trim()} ${lastName.trim()}`;
  console.log(`fullName = ${fullName}`);
 
}

exports.run = app;

exports.parms = [
  { type: 'char', length:  30 },      // firstName            
  { type: 'char', length:  30 }       // lastName        
];

  
  • This is in the format of a "regular" (i.e. NOT low-code) PJS module. It's also NOT a module meant to be called as an Express Route (web service) as in the example here; e.g. it does NOT have the "req" and "res" parameters. Instead, it has zero or more "usual" parameters that you can pass to the module, either from another PJS module or from command PJSCALL.
  • You can call this module from an IBMi green-screen, using the command PJSCALL. The green-screen session could be a Genie session, but it does NOT have to be. A Genie session is required only if the PJS module uses an RDF (Rich Display File).
  • We'll use command PJSCALL (*CMD object) to call this PJS module. (It's NOT the program PJSCALL (*PGM object) that's used to call low-code modules. This could be a source of confusion. We'll clarify it further in a future tip.)
  • On your IBMi system, in a green-screen session, run the following command to direct the call to the PJS instance installed on your PC. Use the port number for that PJS instance; default is 8081 as used in this example.
No Format
PJSMYIP PORT(8081)
  • After that command is run, the environment variables PROFOUNDJS_COMM_HOST and PROFOUNDJS_COMM_PORT (of your interactive job only, not system-wide) should be updated so that any PJSCALL will call the PJS module on your PC. Run command WRKENVVAR to verify. Should look something similar to this.
No Format
PROFOUNDJS_COMM_HOST        'xxx.xx.x.xxx'
PROFOUNDJS_COMM_PORT        '8081'  
  • Run following command to call the PJS module on your PC.
No Format
PJSCALL MODULE(pjstips_07_1)  
        DIRECTORY('pjstips')  
        PARM('John' 'Smith')               
  • The "DIRECTORY" parameter is relative to the directory <PJS_instance_dir>/modules. The .js file you created previously in workspace "pjstips" should have been created in directory <PJS_instance_dir>/modules/pjstips.
  • The console log should show the output like this:
No Format
fullName = John Smith