Skip to main content

MEAN 101: Part 2

Expand your knowledge and learn how to code and execute the respective programs using Angular, Node, Express, and DB2 for IBM i. Part 2 of 2.

A Mac computer with green text is on the center of the screen, and the sun glints off of it in the lefthand corner. There are sticky notes on the screen and various office supplies on the desk it sits on.

After covering basic concepts about the MEAN stack in this previous article http://www.ibmsystemsmag.com/ibmi/trends/opensource/MEAN-101/, expand your knowledge and learn how to code and execute the respective programs using Angular, Node, Express, and DB2i. Our topology will have Angular running in a laptop browser, Node & Express in the Windows cloud and DB2i is in IBM i.

Node.js Install

First install the Node.js. Download the desired build from https://nodejs.org/en/ and choose the default options to complete it. It shouldn’t take more than 5 minutes to install.

Then, verify its version number using “node -v” or “node --version” in your command prompt. In our case, we use Node version 6.9.1.

Node.js “Hello World” Program

Let’s create our Node program. Create a file “MyFirstNodePgm.js” with the below contents.

console.log(“Hello. World. Your 1st node program running successfully“);

To execute it, go to the OS prompt and run the command

node MyFirstNodePgm.js

Now you will see the output “Hello World. Your 1st node program running successfully”

Install Express

To code an Express app, we need to install the Express. Let us do that now by using the command

npm install express 

To learn more, refer to https://expressjs.com/.

Execute Express Web App

Let’s create our Express program. Create a file “MyFirstExpressWebApp.js” with the below contents.

var express = require('express');
var app = express();
app.get('/mypath', function (req, res) 
{ res.send('Hello. This is your Node Express web app!!!'); }); 
app.listen(80, function () { console.log('Your Website is running in port 80'); });

Execute it thru the command

node MyFirstExpressWebApp.js

The OS command prompt should show the output “'Your Website is running in port 80”

Now open the browser and enter the URL http://localhost:80/mypath

You should see the output in the browser as

'Hello. This is your Node Express web app!!!'

DB2i inside Node.js

Learn how to consume DB2i from a Node program. Detailed information is available here https://github.com/ibmdb/node-ibm_db

In order to connect to DB2i, we did two things:

  1. In the windows directory where your node programs are located, run the command “npm install ibm_db”. This will install DB2 related NPM packages.
  2. If you want to use node ibm_db against DB2 for z/OS or DB2 for i(AS400) Servers, you must have db2connect license, if server is not db2connect activated to accept unlimited number of client connection. You can buy db2connect license from IBM. The connectivity can be enabled either on server using db2connect activate utility or on client using client side license file. If you have client side license file, just copy it under ...node_modules/ibm_db/installer/clidriver/license folder, in your Node machine, to be effective. In our case, we copied our db2connect license file “db2consv.lic” in the ...node_modules/ibm_db/installer/clidriver/license folder. We did not install anything DB2 related software in the node server other than the ibm_db npm package and the db2connect license.

Plenty of sample code is available in the above URL. Here is one of them.

var ibmdb = require('ibm_db');

ibmdb.open("DATABASE=<your-dbname>;HOSTNAME=<your-host>;UID==<your-
ID>;PWD==<your-password>;PORT=<dbport>;PROTOCOL=TCPIP", function 
  if (err) return console.log(err);

  conn.query('select 1 from .', function (err, data) {
    if (err) console.log(err);
    else console.log(data);

    conn.close(function () {
      console.log('done');
    });
  });
});

To run the code, replace the generic information with your DB2 instance, user, password, port details. Now you should be able to run the node program to access the DB2i.

Angular

Let’s see how we can code some Angular applications. I have recently published a detailed article on this exact same topic:. It’s highly recommended that you read this article before proceeding. That link should enable you to run your first Angular application.

Conclusion

Now we have learned how to code using Node, Express, Angular and DB2i. In the bigger picture, Angular runs in the front end while Node & Express run in the back end. In our case, we had Angular running on a home computer in Raleigh, NC, Node/Express running in an IBM Windows cloud in Durham, NC and DB2i in an IBM i in Rochester, MN. Using this technology, we were able to bring in about 1000 records, each having around 14 columns of data, to the browser in about 3 seconds.

Helpful Links

MongoDB:

Express:

Angular:

Node.js

NPM:

GitHub:

IBM StrongLoop:

Node.js Foundation:

IBM Node.js SDK for Power, Intel and z:

IBM Bluemix: 

IBM Node-RED:

IBM Systems Webinar Icon

View upcoming and on-demand (IBM Z, IBM i, AIX, Power Systems) webinars.
Register now →