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!
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:
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:
|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:
You must then restart your server instance!
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 start.js:
You can then modify your module code as follows:
When you test the web service again, you can add the parameter as follows:
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:
In your module, you will be able to retrieve the first_name property by referring to it as follows:
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.