Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

On Helping IBM Offer Support for Node.js

DZone's Guide to

On Helping IBM Offer Support for Node.js

Read on to see the simple, yet elegant solution one web dev came up with to switch an existing application over to Node.js.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Rocket Software has been working with IBM for more than 25 years, so when IBM announced plans to offer Node.js support for IBM i, the Rocket team immediately began rewriting our ALM Ops Portal application to transform it into a Node.js application. I wouldn’t be surprised if Rocket was the first company to do that – but we definitely won’t be the last!

XMLSERVICE is a service that runs on IBM i, and it allows users to run both SQL queries and native commands and programs. Users activate XMLSERVICE by sending it an XML document describing the command, program, or SQL query they wish to run. The results are returned in an XML document and can be invoked either through DB2 stored procedures or a REST interface.

Combining Node.js with the REST interface to XMLSERVICE is the perfect cloud adaptation. Users can set up the XMLSERVICE REST interface on IBM i and then run their Node.js app on any platform, including the IBM i PASE environment, AWS, Bluemix, and more.

So what does this look like in the real world? Here is just one example of code, paraphrased from the example at idevcloud, that shows how to invoke the web service interface to XMLSERVICE from Node.js to run a SQL query.

// Sample Node.js code to run an SQL query using XMLSERVICE

// output (callback)
function cb(str) { 
   console.log(str); 
}

var url = require('url'); 
var uri = url.parse("http://< hostname>:<port>/cgi-bin/xmlcgi.pgm");

var http = require("http"); 

var ixml  = "<?xml version='1.0'?>\n" 
          + "<xmlservice>"
          + "<sql>" 
          + "<query>select * from QIWS.QCUSTCDT</query>" 
          + "<fetch block='all'/>" 
          + "</sql>" 
          + "</xmlservice>";

var xml_enc = encodeURI("db2=" + '*LOCAL' 
      + "&uid=" + '<user profile id>' 
      + "&pwd=" + '<user profile password>'
      + "&xmlin=" + ixml
      + "&xmlout=" + '512000');

var arr = uri.host.split(":");

var options = { 
   host: arr[0], 
   port: arr[1], 
   path: uri.path + '/?' + xml_enc 
}; 

var httpCallback = function(response) { 
   var str = '';

   //another chunk of data has been received, so append it to `str`
   response.on('data', function (chunk) { 
      str += chunk; 
   });

   //the whole response has been received, so return
   response.on('end', function () { 
      cb(str); 
   }); 
}

// make the call
var req = http.request(options, httpCallback).end();

Just replace:

  • <hostname> with the fully qualified hostname of the IBM i running XMLSERVICE. If the node app is running in PASE on the IBM i this can be ‘localhost.’
  • <port> with the port on which the IBM i integrated web server instance, providing the REST interface to XMLSERVICE, is listening.
  • <user profile id> with a user profile with sufficient permissions to run the query.
  • <user profile password> with the password for the user profile.

Of course, this is just the tip of the iceberg. Over the next few months, I’ll share a few other tips and tricks to showcase how organizations can set up the REST interface to XMLSERVICE and parse the response from XMLSERVICE.

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
node.js ,sql ,rest ,web dev ,xml

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}