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
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.
- Navigate to the workspace "pjstips".
- Click New/Javascript File.
- Enter the code below. Click Save As, and save as pjstips_02_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).
- In the Files tree, right-click on newly-created file pjstips_02_1.js, and click Properties. Mark the file as an Express Route, using HTTP method of "GET", with the Route Path as shown below.
- 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.
- Since this is called as a URL on a browser, you can bookmark it, to use it later if needed.
- 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.
- Click New/Javascript File.
- Enter the code below. Click Save As, and save as pjstips_02_2.js.
- In the Files tree, right-click on newly-created file pjstips_02_2.js, and click Properties. Mark the file as an Express Route, using HTTP method of "GET", with the Route Path as shown below.
- Now, the URL to call this PJS module is http://localhost:8081/run/pjstips/pjstips_02_2?custno=1234 .
- You should see the response on as JSON as shown below on the browser.
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.
- Click New/Javascript File.
- Enter the code below. Click Save As, and save as pjstips_02_3.js.
- In the Files tree, right-click on newly-created file pjstips_02_3.js, and click Properties. Mark the file as an Express Route, using HTTP method of "POST", with the Route Path as shown below.
- Now, the URL to call this PJS module is http://localhost:8081/run/pjstips/pjstips_02_3. Note that you can NOT simply put this URL on a browser to "call" the module (unless you use some browser extensions). You'd need to use some tool (e.g. POSTMAN or PJS low-code module or PAPI) to send a POST request, with the input parms specified in the "body" of the request. We'll use this web service in the tips related to using low-code modules that consume a REST web service.