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.
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.
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.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; |
The previous example sends back the "response" as a text message. In the next example, we'll send back the reponse as JSON.
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; |
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.
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; |