Informing the IBM Community

Node is Weird!


Firstly, thanks to all that attended my Node.js on IBM i workshop at the International i Power conference in June. Once again, a great event, meeting up with old friends and colleagues. Well done to the organisers.
In this article I intended giving you a quick introduction to Node.js on our favourite server, the IBM i, and explain the reason for the title of this article.

To run node on the IBM i, we need the following licensed programs.

I’m not sure why the Apache server is there as a pre-req. I cannot find any dependencies for node to use the Apache HTTP server. I’m sure there are readers out there that can enlighten me.
Like other open source software on our box, Apache, PHP etc, Node runs in PASE.
The open source software stack is presently updated by PTF group SF99225. The current level, 6 is dated 11th June 2017.

Once we have loaded our open source software, we find that we have many versions of Node installed, which one do we use, since we have Node version 0, 4 & 6 installed?
IBM has provided a shell script, hence the .sh extension, to allow us to pick a node version to use.
/QOpenSys/QIBM/ProdData/OPS/Node6/bin/ list

To ensure you are running the version you expected, run the Node -v command in QShell to check the version.

Another useful command is Which Node, this shows us where node is located in the IFS.

Ah, wait a mo, that doesn’t show Node 6? We get more details on this file, by running
ls -l /QOpenSys/usr/bin/node
we can see that it is a symbolic link to the Node6 directory. That explains it!

Which Editor Should I Use To Code Node.js?
We have lots of options to code in Node. A few of them are listed below.

In reality, any editor you are used to, carry on using it. So, if your company has paid silly pounds for RDi, but you are happy with RDi for your RPG development, use that for your node projects.
There is nothing more annoying than trying to learn another editor with its keyboard differences as well as learning a new language. What’s wrong with SEU I hear you say!
Personally, I have started using Visual Studio Code for my Node development as I had previously used Visual Studio. I found it ideal for Node use and easy to pick up and its free!

Sync or Async Programming
This is where Node gets weird! – Weird in a different way of programming that we are used to.
Let’s take a look at a typical CL program.

As we all know, if the call to the GetCust program takes a considerable amount of time to complete, the rest of the CL program will just wait for completion. This is synchronous programming.
Well node.js handles things differently, it uses asynchronous programming.
In the diagram below, which is a typical web flow, we can see as soon Node gets a request, it deals with it in the background. Throws it off to batch as we know it.

This is using the power of Node. This feature is also known as a Callback. Callbacks work in the background, in batch!

Let me show a couple of examples to see the differences between these two methods of programming.
Firstly, sync programming, or blocking mode. The type we are all used to in CL, RPG etc.

// sync.js
var fs = require(“fs”);

var data = fs.readFileSync(‘formaserve.txt’);

console.log(“Program Ended”);

This program will list out, using the console log, the contents of the file formaserve.txt and then say ‘Program Ended’
Luckily, the formaserve.txt file is only a few lines long.

The results are, just as what we expect.

Now onto the async, or non-blocking method;

// async.js
var fs = require(“fs”);

fs.readFile(‘formaserve.txt’, function (err, data) {
if (err) return console.error(err);

console.log(“Program Ended”);

Functionally, this program is very similar to the sync example above. It prints out the contents of a text file.
Let us just run this example;
The first thing it does, is that it says the program ended!

This is because it used a callback function to go away and get the file. If the file had been enormous, getting the file would not have stopped the rest of the program from running – just what is needed!

Another way to tell if it is using a callback function, is that a function is embedded within a functional call. The code we ran shows it calling an anonymous function as a callback function. The more you use callback functions, the easier they will get, honest!




Hopefully, this article will give you a quick insight in how node works and what we have to do it get it running on the IBM i.  In my next article, I’ll be looking at how we can use node to call our traditional programs and access data in our DB2 tables.

So actually, as I use Node more and more, I find that Node is not so weird, it’s just different, not what we are used to.  We can use this to our advantage on the IBM i for tasks that our traditional programming languages can struggle with.

If you have any questions on IBM i with Node.js, let me know in the comments below.

I’m looking forward to the next round of the i-UG user group meetings this year in the UK, this is at the Mount Hotel in Wolverhampton on 8th November 2018.  Full details can be found on the i-UG site here.

Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems.


How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.