Node.js on IBMi

Node.js Example application

Node.js on IBMil. The following sample code queries the DB2 database table QIWS.QCUSTCDT and returns the result set to the browser. Add the following code to a file called /home/sample.js.


var http = require('http');
var db = require('/QOpenSys/QIBM/ProdData/Node/os400/db2i/lib/db2');
http.createServer(function (req, res) {
     db.exec("SELECT LSTNAM, STATE FROM QIWS.QCUSTCDT", function(rs) {
                 res.writeHead(200, {'Content-Type': 'text/plain'});
}).listen(8081, 'IPaddress');
console.log('Server running at http://IPaddress:8081/');

From a terminal session (best to run ssh server in separate subsystem)

node /home/njs/sample.js

If no problem, you can browse the site to get the query result from database. The output of result set is in JSON format. You can use the JSON.stringify() function to convert it to a readable text, or access the key-value pairs directly.




1.      DB2 for i Access APIs


1.1.    Introduction

DB2i add-on for Node.js is a JavaScript API set for DB2 database manipulation on IBM i. The add-on is shipped with Node.js and located in /QOpenSys/QIBM/ProdData/Node/os400/db2i/. It contains a binary module db2i.node in thebin directory and a library file db2.js in the lib directory. If you want to copy the add-on to other places, please remain the folder structure.

To use the DB2i add-on, you only need to require the db2.js file in your source code.

1.2.    Examples

1.2.1. Basic Query

var db = require('/QOpenSys/QIBM/ProdData/Node/os400/db2i/lib/db2');
    db.debug(true);  // Enable Debug Mode if needed.
    db.init(function(){  // Initialize the environment for database connections.
           db.serverMode(true); // Enable Server Mode if needed
    db.conn(*LOCAL”, “USER”, “PASSWORD”, function(){ // Connect to a database
           db.autoCommit(true); // Enable the Auto Commit feature if needed.
    db.exec(“CREATE TABLE TEST.NAMEID (ID INTEGER, NAME VARCHAR(50), SALARY DECIMAL(8,2)));  // Create a new table in the database.
    db.exec(“INSERT INTO TEST.NAMEID VALUES (0, 'David', 999.99)); // Insert a new record in the table.
    console.log(“There are %d rows affected.”, db.numRows()); // Get the execution result.
    db.exec(“SELECT * FROM TEST.NAMEID,   // Query the data in the new table.
           function(jsonObj) {  // Print the output in a readble way.
           console.log(“Result: %s”, JSON.stringify(jsonObj));
           var fieldNum = db.numFields();
           console.log(“There are %d fields in each row.”, fieldNum);
           console.log(Name | Length | Type | Precise | Scale | Null);  // Print all the fields information.
           for(var i = 0; i < fieldNum; i++)
                  console.log(%s | %d | %d | %d | %d | %d”, db.fieldName(i), db.fieldWidth(i), db.fieldType(i), db.fieldPrecise(i), db.fieldScale(i), db.fieldNullable(i));
    db.exec(“DROP TABLE TEST.NAMEID);  // Delete the example table from the database.
    db.close();  // Release any used resource.
    } catch(e) {  // Exception handler