[et_pb_section bb_built=”1″ fullwidth=”on” specialty=”off” _builder_version=”3.0.64″ next_background_color=”#000000″][et_pb_fullwidth_post_title admin_label=”Blog Post Header” _builder_version=”3.17.2″ title=”off” meta=”off” categories=”off” comments=”off” featured_placement=”background” text_orientation=”center” module_class=”blog_post_header” title_font_size=”60px” background_blend=”saturation” title_font=”Raleway|on|||” title_text_color=”#274060″ title_letter_spacing=”2px” meta_font=”Raleway||||” meta_font_size=”20″ meta_text_color=”#274060″ meta_letter_spacing=”2px” background_position=”top_left” /][/et_pb_section][et_pb_section bb_built=”1″ _builder_version=”3.0.63″ prev_background_color=”#000000″][et_pb_row _builder_version=”3.0.63″][et_pb_column type=”4_4″][et_pb_text admin_label=”Blog Post” _builder_version=”3.17.2″ module_alignment=”left”]

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.

$ export PATH=/QOpenSys/QIBM/ProdData/Node/bin:$PATH
$ export LIBPATH=/QOpenSys/QIBM/ProdData/Node/bin:$LIBPATH

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.

$ node -v

v0.10.29

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.

var http = require('http');
http.createServer(function (req, res) {
 res.writeHead(200, {'Content-Type': 'text/plain'});
 res.end('Hello World\n');
}).listen(1337, '0.0.0.0');
console.log('Server running at http://0.0.0.0:1337/');

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

$ cd /home/aaron/
$ node server.js

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:

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.

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row][et_pb_column type=”4_4″][et_pb_text admin_label=”Contact Form Title” _builder_version=”3.17.2″ background_layout=”dark” text_orientation=”left” border_style=”solid” text_font=”Raleway|on|||” text_text_color=”#f45d01″ text_font_size=”26″ text_letter_spacing=”2px” module_alignment=”left”]

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

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *