Page tree
Skip to end of metadata
Go to start of metadata

This exercise guides you through creating a simple Hello World web service. While the typical web service would normally include some business logic and/or database access, this example concentrates on the mechanics of the web service only and does not include any real business logic.

Before you proceed, make sure the pjshello directory is created and configured as described here.

Looking for some advanced examples?

We have now added some example projects to GitHub for you! You can fork the projects to try them out yourself or use them as a template to help get you started!

Consume Web Service to Directly Update DB2 Table

Consume Web Service to Call RPG Program that Updates DB2 Table

 

Create web service module

In Profound.js, a web service is a run-able module that receives a request and a response object parameter. To create your first web service, create a file named ws1.js, place it in the pjshello subdirectory of modules, and add the following code to the file:

/profoundjs/modules/pjshello/ws1.js
function ws1(request, response) {
  response.json({ 
    msg: "Hello World" 
  });
}
 
exports.run = ws1;

 

Mapping a URL to the Web Service

To make the web service active, you must first associate a URL with it in the server's start.js file. Profound.js uses Node.js Express as its web server. The following Express API are used to map a URL to a module:

APIPurpose
app.get()Routes HTTP GET requests for a specified path
app.post()Routes HTTP POST requests for a specified path
app.put()Routes HTTP PUT requests for a specified path
app.delete()Routes HTTP DELETE requests for a specified path
app.all()Routes ALL HTTP requests for a specified path

For this example, add an app.all() line to the bottom of start.js as follows:

/profoundjs/start.js
#!/usr/bin/env node
 
// Load Profound.js
var profoundjs = require("profoundjs");
 
// Process command line arguments
profoundjs.rlog = process.argv.includes("--rlog");
profoundjs.tlog = process.argv.includes("--tlog");
 
// Apply configuration
var config = require("./config.js");
profoundjs.applyConfig(config);
 
// Start Profound.js server
var isWorker = profoundjs.server.listen();
if (isWorker) {
  // This is the top-level Express Application.
  // Custom Express coding can be added here.
  var express = profoundjs.server.express;
  var app = profoundjs.server.app;
  app.use(express.json());  // default to use JSON-encoded post data
 
  app.all("/ws1", profoundjs.express("pjshello/ws1"));  // this line was added for our new web service
 
}

 

Testing the Web Service

You can test the web service using a specialized tool, like Postman, or directly in your browser. Simply point the tool or the browser to the following URL:

http://host:port/ws1

Where host is the Profound.js server host name or IP address and port is the Profound.js port number.

 

Expanding the Web Service to Process Parameters on the URL

To receive a parameter on the URL, you can specify a parameter by name using the following syntax in start.js:

app.all("/ws1/:myparam", profoundjs.express("pjshello/ws1"));

You can then modify your module code as follows:

/profoundjs/modules/pjshello/ws1.js
function ws1(request, response) {
  response.json({ 
    msg: "Hello World",
    received: {
      myparm: request.params.myparam
    } 
  });
}
 
exports.run = ws1;

When you test the web service again, you can add the parameter as follows:

http://host:port/ws1/123456

The parameter value, 123456,will now be shown in the response.

 

Expanding the Web Service to Receive JSON POST Data

In addition to parameters on the URL, additional data in the form of a JSON POST can be received by your web service through the use of request.body. For example, the following JSON object may be posted to your web service:

{
  "first_name": "John",
  "last_name": "Doe"
}

In your module, you will be able to retrieve the first_name property by referring to it as follows:

request.body.first_name

 

Connecting the Web Service to IBM i

If your your web service is already running on IBM i, no additional steps are necessary. However, if your web service is running on a different server for your testing that requires a connection to your IBM i server, please see the Setting up a Connection to a Remote IBM i Instance documentation page.

Should authentication be required for any Connector API to function, signon will occur automatically. 

 

Video Tutorial

  • No labels