...
- 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).
Code Block language js 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 "reponseresponse" 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 paratemer, 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.send() to send back the "response" as a text message. res.send(`test from pjstips_02_1.js; custno = ${custno}; name = ${name}`); } exports.run = app;
- 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.
...
- Click New/Javascript File.
- Enter the code below. Click Save As, and save as pjstips_02_2.js.
Code Block language js 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 "reponseresponse" 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 paratemer, 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;
- 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.
...
- Click New/Javascript File.
- Enter the code below. Click Save As, and save as pjstips_02_3.js.
Code Block language js 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 "reponsseresponse" 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;
- 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 "GET", with the Route Path as shown below.
- Now, the URL to call this PJS module is http://localhost:8081/run/pjstips/pjstips_02_03. Note that you can NOT simply put this URL on a browser to "call" the module. 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.
...