Node Nods to RPG and DB2

by | Oct 17, 2017

Originally published: IBM Systems Magazine on 3/10/2015

Updated author notes: Check with IBM to obtain newer versions of Node.js. If you install a newer version then know the paths to modules will change. Also note this was the original synchronous DB2 adapter and now there is an asynchronous one.

In the previous article we showed how to get up and running with an initial Node.js “Hello World” web application. That was pretty neat and all but the rubber hits the road when we start being able to interface with DB2 tables and existing RPG programs. The good thing is that IBM was fully aware this was a necessity and has given us some good tools to accomplish the task.

First let’s work through connecting to DB2 and retrieving some data. Instead of creating a web application we’re going to use the Node.js REPL program to copy/paste code to more quickly and easily test functionality. The [[previous article(*Note: Node, The First Kiss*)]] showed how to setup your Node.js environment and start the REPL so we won’t be covering that here. Below is a complete Node.js script you can copy/paste right into the Node.js REPL.

Below what your screen should look like after loading the Node.js REPL and pasting the code. Side Note: Yes, the below screenshot is a browser tab, and yes it is an SSH session to my IBM i. I am using the Secure Shell Chrome plugin developed by Google which I’ve found to be a nice alternative on Windows compared to putty and cygwin.

Loading REPL and running code

Loading REPL and running code

Let’s talk through what is going on. First we see a require statement to bring in the db2 module. The full path needs to be specified because the db2 module it isn’t in a location that Node.js will look for it. Node.js looks for modules in folders named node_modules, first in the current directory and then traverses up the directory structure until it gets to the root.

Next we have db.init() which will, you guessed it, initialize the database. You can specify a number of other parameters on db.init() as described in the documentation. After that the specific database to connect to is specified. In this case we specified *LOCAL because we are going after the database on the same server that this script is running. The call to db.exec() is where the action starts happening. The first parameter is the SQL statement. The QCUSTCDT table should already be on your machine in library QIWS because it comes with the operating system. The second parameter is a Javascript anonymous function that receives in a single parameter named result_set and then outputs the contents to the screen with console.log(). On the screen we can see result_set was a Javascript array with multiple objects. Lastly, the db.close() is invoked for clean-up purposes.

Next let’s look at communicating with an RPG program using the XMLSERVICE Toolkit for Node.js. There are two components to this tool: 1 – the XMLSERVICE RPG code that IBM delivers in PTF SI50835(V7R1) or SI51153(V7R2), and 2 – the Node.js Javascript portion. In this example we’re going to focus on the Node.js portion and how it interfaces with the XMLSERVICE RPG side. You can learn more about XMLSERVICE on the site. One important feature of XMLSERIVCE you should know is that it can receive calls via HTTP or DB2 stored procedures. This means that, if necessary or required, your Node.js application can reside on a separate machine and make HTTP calls to XMLSERIVCE to call on IBM i resources (RPG programs, DB2 tables, etc).

Below is a sample RPG program named PGM1 that has two pass-by-reference parameters. The program is very simple in that it only gives new values to the parameters and returns, but you should know that XMLSERIVCE can work with very complex parameter lists. I determined to write it in 100% free-form because I didn’t want my middle name to be Aaron “Luddite” Bartell. I must admit I am still getting used to the 100% free form – a lot for my eyes to take in.

Below is the Javascript code necessary to invoke the PGM1 RPG program. Go ahead and paste this into a Node.js REPL to see the end results. Note you need to compile the above RPG program first.

Let’s break these down line by line. The first line in bringing in the itoolkit functionality located at the path specified. Note the file is actually named itoolkit.js but the extension can be omitted when using the require API. What’s cool is you can actually view the itoolkit.js source to learn how everything was put together – take a peek yourself.

Next we see the PGM1’s parameter list being defined along with inbound values, though in this case we are specifying default values of blank and zero. You can learn about the various data types by looking at the itoolkit.js file.

Next we see the conn.add() statement which receives in an XML document declaring the structure for the call to PGM1. This is somewhat where XMLSERVICE gets its name from – in that the interface to it is all XML based. This is great for flexibility and loose coupling.

The next portion of code,, is the actual invocation of PGM1. The first parameter to is a Javascript anonymous function which receives back the response from XMLSERVICE – pure XML. The result is converted to JSON (JavaScript Object Notation) with the call to xt.xmlToJson() to make the results easier to work through. Given the results variable is now a Javascript object we can easily traverse it with native Javascript language constructs (i.e. forEach).

The end result is as follows where we display the response type and value.

This example was very simple in nature and the code is somewhat involved. That’s where encapsulation can come into play – effectively wrapping all the above with an easy to call API. If you look in folder /QOpenSys/QIBM/ProdData/Node/os400/xstoolkit/lib/ you’ll see IBM has already started doing this with the likes of iUserSpace.js, iProd.js, and iObj.js. My hope is IBM will create an iPgm.js in the future. Or maybe, just maybe, that’s something the community should develop?


  • Call web APIs from RPG
  • Process XML and JSON
  • Offer web services

CyberSource Toolkit for i

  • Process credit cards from native RPG
  • Integrate CyberSource payment services

Litmis Spaces

Affordable IBM i cloud hosting

Follow Us

Need help integrating Node.js with your existing RPG? Let’s chat.

  • This field is for validation purposes and should be left unchanged.

Get Social

Share to your favorite social platform