Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Before you proceed, make sure to create the example workspace as described here.

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, in the pjshello workspace directory, and add the following code to the file:

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

Mapping a URL to the Web Service

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

...

For this example, configure a Named Route (stateless) for the ws1.js file, as described here. Set the route path to example3, and select All for HTTP method.

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:

...

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 the route path:

...

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:

...

Code Block
languagejavascript
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.

...