Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 18 Next »

  • Navigate to the workspace "pjstips".
  • Click New/Javascript File.
  • Enter the code below. Click Save As, and save as pjstips_02_1.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 "reponsse" 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 the 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.

  • 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 in in debug using your favorite debug tool (e.g. VSCode or Chrome), add a breakpoint, then call it from a browser.


The example above 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.
  • 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 "reponsse" 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 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_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.



  • No labels