system command - Part 2¶
In the first part of this series we got to know the
system command, which allowed us to call CL-commands from within the PASE for i environment (IFS shell). In this part we will explore calling
And that is exactly what we will do! First I connect to our IBM i via SSH, create a project folder in my home folder (denoted by the ~), and navigate into it.
npm init -y command, I create a
package.json for this folder. This basically creates our project. The
npm from asking a bunch of project-specific questions. I prefer editing those manually in my text editor later on.
Now, if we allow ourselves to connect to our IBM i home folder as a network share, we can open our folder in an editor or IDE. My editor of choice is Microsoft's open source editor Visual Studio Code.
exec method from the
child_processes module. As one can see in the Node docs, the
exec command requires at least one of three possible arguments, namely the command that is supposed to be called. In addition to that we pass an options object and our callback, which will process the STDOUT output of our shell command.
I create my
index.js file and write the following into it:
1 2 3 4
In this small example, we're calling the same
Kerim's Note: In the example above, you can see that we passed an encoding parameter to format the output to ISO-8859-1. What's also interesting to note is that
exec doesn't only pass
stdout to our callback but also
stderr. If instead there was an error with the
exec itself, then the
error argument of the callback is filled. This is a common pattern in Node libraries.
Since we want to display our output in the browser, we need a webserver. I am going to use the popular Express webserver library by installing into our project with
and create s
1 2 3 4 5 6 7 8 9 10 11 12 13
Now when we run our server
we can navigate to the specified route via
https://as400:8080/wrkactjob and see the following:
Finished! In only a few minutes we managed to create a web output and even a Web API for our IBM i CL.
We could now extend our API so that it displays
WRKACTJOB with its
sbs parameter to filter by subsystem with route like
https://as400:8080/wrkactjob/sbs. For that I adjust our server like so:
1 2 3 4 5 6 7 8 9
Now there's nothing in our way to create similar API endpoints for other CL commands!