Node, The First Kiss

by | Sep 26, 2017

Originally published: IBM Systems Magazine on 2/2015

Updated author notes: This articles makes mention of Node.js v0.10.29. IBM has since provided many updates to their IBM i port. If you’re running the examples in this article on your machine please know they may not work exactly as expected.

Gimme sugar, baby! When learning about a new technology I always like to get something working in as short a time as possible, so let’s do that with this article and subsequently dive into details. Oh, I forgot to mention what we are talking about – this is an article introducing you to the recently released Node.js platform – a means to write web applications using Javascript on the server.

First things first, head on over to the IBM i Node.js developerWorks site to learn now to install the free IBM licensed program 5733OPS. Not an option to install Node.js on your IBM i? Check out runnable.com for now.

Once the install of 5733OPS is complete you can run the following commands in PASE to setup your environment. Setting these variables will allow you to invoke Node.js from any directory while in PASE. To run the commands in PASE you can either CALL QP2TERM from the 5250 command line or SSH into your machine using a terminal client (I like this Chrome plugin). If you’d like more info on SSH config for your IBM i then please check out this page I’ve setup.

The above is similar to how a library list works, the PATH environment variable declares what directories should be searched when commands are run. Similarly the LIBPATH environment variable declares where to look for shared libraries that the node binary requires. Note a “shared library” is completely different than a QSYS.LIB library.

Now we can test to see if we have access to the node binary by typing the node -v command, as shown below.

Success! Next simply type “node” at the command prompt to start up what’s called the Node.js REPL. The Node.js REPL is an interactive environment that allows you to enter Node.js statements for immediate invocation. Below is a screenshot of an SSH session into my IBM i using the Chrome plugin mentioned earlier. The first thing you see is the addition of 2 + 2 which results in, obviously, 4. This is REPL in action – Read, Evaluate, Print, and loop. Most anything you would write in a Node.js program can be placed into the REPL. I have become a big fan of REPLs (Ruby has one also) for the purposes of not only learning a new technology but also as a means to quickly test code. To return to your PASE shell type .exit.

Testing out Node.js REPL

Testing out Node.js REPL

Now let’s take things a step further by creating our first Node.js web application. Create a file in the IFS named server.js (i.e. put it in /home/aaron/server.js, replace ‘aaron’ with your own profile). Place the following code into the server.js file and save it.

Now go back to your PASE shell session and run the following two commands.

Note that your PASE shell session will now be tied up and you won’t be able to enter commands, as shown below. Use Ctrl+C to end the server.

Starting the Node.js server

Starting the Node.js server

So what happened? Running node server.js is like calling an RPG program. In this case the server.js program contains not only a small hello world application but also an entire web server! That’s right, you now have a web application up and running on your IBM i. You can test this by loading URL http://ibmi_ip:1337 in your browser (replace “ibmi_ip with your IBM i’s IP address), as shown below. This is admittedly significantly more simple than my first RPG-CGI application.

Viewing 'Hello World" in a browser

Viewing ‘Hello World” in a browser

The first line of the program with require is similar to a /copy in RPG – bringing functionality into the program, in this case HTTP capabilities which is necessary for running a web server. The next line, http.createServer, is setting up the HTTP server to listen on port 1337. Specifying 0.0.0.0 for the IP simply means it will listen on the local machine’s IP address (i.e. localhost). The parameter passed into it is what’s called an anonymous function. The code within the anonymous function will be invoked whenever a request comes into port 1337. In this case the code is simply outputting an HTTP header and with “Hello World” as content.

Now that we’ve had a bit of sugar let’s go back and discuss some facts about what Node.js is and isn’t. First a wikipedia.org definition:

Node.js is an open source, cross-platform runtime environment for server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, Linux, FreeBSD, and IBM i.

Ok, guilty, I’m the one that added the IBM i reference in the wikipedia.org page 🙂 I found the easiest approach to convey thoughts on Node.js was to give a list, so here we go…

A list about Node.js:

  • Started in early 2009 by Ryan Dahl of Joyent, but is now driven by the community.
  • Node.js is built on Javascript. Javascript is not in any way, other than name, related to Java.
  • Uses Google’s V8 Javascript runtime under the covers, which IBM also ported.
  • Runtime is written in C (does not run atop the JVM if that is what you were thinking)
  • Primary goal is to build scalable network applications with Javascript on server-side.
  • Not a web framework, though many web frameworks have been written on top of it.
  • Hasn’t reached version 1.0 (currently at version 0.10.35 as of this writing). This means some APIs are subject to change. The good thing is they give a “stability” rating for each API.
  • Has its own package manager named npm which makes it much simpler to resolve and download dependencies. Note some packages require compiling and then you need to load Python and possibly gcc.
  • Has it’s own module system for code organization. This initially threw me for a loop because I couldn’t find it in my normal Javascript resources.
  • Not all Javascript you’ve used in the browser will work on the server-side in Node.js. For example, document.write() will fail.
  • Javascript, and inherently Node.js, attains concurrency more through events where other models do this more through separate processes (aka IBM i jobs) or threads within the same process. That’s not to say Node.js is single-thread-bound. This article describing Node.js for .NET developers actually does a good job of explaining why Node.js is considered single-threaded, yet can still use multiple threads.
  • It is considered “non-blocking” which basically means it can continue on with other statements in the program without waiting for longer processes (like interacting with the file system). The non-blocking nature of Node.js is an article in itself so we won’t be digressing into it here, though I will offer up this YouTube video from codeschool.com that gives a great explanation.
  • One of the most intriguing aspects of Node.js for me is that you are now using the same language on both the client and server. This will inevitably make a web developer more efficient because not only is there less syntax to be mindful of but also there is a lot of code sharing that can happen between client and server.

Hopefully that gives you a high level understanding of Node.js. If you haven’t done so already you will need to learn Javascript. One of the most different things I had to learn in Javascript was the various ways functions worked. Go here to learn more about the ways Javascript functions are different than other languages. In particular take a peek at “Anonymous”, “Self-Invoking”, and “Closure” functions.

If you are at the very beginning of your Javascript journey then I’d encourage you to start with W3Schools(free) who have some of the most concise, yet still helpful and relevant, training around. The codeacademy.com(free) site is also a good place to start your Javascript journey. Once you’ve got Javascript under your belt you can check out some of the other hands on Node.js training like nodeschool.io(free) or codeschool.com($29/month).

Stay tuned for a next article showing how to access your DB2 tables and RPG programs from Node.js. This is great because it allows you to capitalize on your existing investment. Also, check out the IBM i Node.js group on LinkedIn – effectively a “water cooler” gathering place to discuss Node.js as it relates to IBM i.

RPG-XML Suite

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

Litmis Spaces

Affordable IBM i cloud hosting

CyberSource Toolkit for i

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

Follow Us

Need help with Node.js? We’ve been using it on IBM i for a long time and we can help. Let’s talk.

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

Get Social

Share to your favorite social platform