Section 4: "JetNode.js" - Building Web Services with Express and the IBM i Toolkit
Now the bit that makes IBM i folks grin: calling your existing programs from Node.js.
Running a CL command
// cl-demo.js
const { Connection, CommandCall } = require('itoolkit');
const conn = new Connection({
transport: 'odbc',
transportOptions: {
connectionString: `DSN=*LOCAL;UID=${process.env.DB_USER};PWD=${process.env.DB_PASS}`,
},
});
conn.add(new CommandCall({
type: 'cl',
command: "SNDMSG MSG('Hello from Node.js!') TOUSR(*SYSOPR)",
}));
conn.run((error, xmlOutput) => {
if (error) throw error;
console.log(xmlOutput); // XMLSERVICE returns XML describing the result
});
const { Connection, CommandCall } = require('itoolkit');
const conn = new Connection({
transport: 'odbc',
transportOptions: {
connectionString: `DSN=*LOCAL;UID=${process.env.DB_USER};PWD=${process.env.DB_PASS}`,
},
});
conn.add(new CommandCall({
type: 'cl',
command: "SNDMSG MSG('Hello from Node.js!') TOUSR(*SYSOPR)",
}));
conn.run((error, xmlOutput) => {
if (error) throw error;
console.log(xmlOutput); // XMLSERVICE returns XML describing the result
});
Run it and check DSPMSG QSYSOPR. Magic.
Calling an RPG program with parameters
Say you have a simple RPG program MYLIB/GETPRICE that takes an ingredient code in and returns a price:
**free
ctl-opt dftactgrp(*no);
dcl-pi *n;
code char(10);
price packed(7:2);
end-pi;
if code = 'FLOUR';
price = 1.25;
else;
price = 0;
endif;
*inlr = *on;
return;
ctl-opt dftactgrp(*no);
dcl-pi *n;
code char(10);
price packed(7:2);
end-pi;
if code = 'FLOUR';
price = 1.25;
else;
price = 0;
endif;
*inlr = *on;
return;
Call it from Node:
// pgm-demo.js
const { Connection, ProgramCall } = require('itoolkit');
const { parseString } = require('xml2js');
const conn = new Connection({
transport: 'odbc',
transportOptions: {
connectionString: `DSN=*LOCAL;UID=${process.env.DB_USER};PWD=${process.env.DB_PASS}`,
},
});
const pgm = new ProgramCall('GETPRICE', { lib: 'MYLIB' });
pgm.addParam({ type: '10A', value: 'FLOUR' });
pgm.addParam({ type: '7p2', value: 0 });
conn.add(pgm);
conn.run((error, xmlOutput) => {
if (error) throw error;
parseString(xmlOutput, (parseError, result) => {
if (parseError) throw parseError;
const parms = result.myscript.pgm[0].parm;
console.log('Price returned:', parms[1].data[0]._);
});
});
const { Connection, ProgramCall } = require('itoolkit');
const { parseString } = require('xml2js');
const conn = new Connection({
transport: 'odbc',
transportOptions: {
connectionString: `DSN=*LOCAL;UID=${process.env.DB_USER};PWD=${process.env.DB_PASS}`,
},
});
const pgm = new ProgramCall('GETPRICE', { lib: 'MYLIB' });
pgm.addParam({ type: '10A', value: 'FLOUR' });
pgm.addParam({ type: '7p2', value: 0 });
conn.add(pgm);
conn.run((error, xmlOutput) => {
if (error) throw error;
parseString(xmlOutput, (parseError, result) => {
if (parseError) throw parseError;
const parms = result.myscript.pgm[0].parm;
console.log('Price returned:', parms[1].data[0]._);
});
});
Note:itoolkitv1 returns raw XML; we usexml2jsto parse it. The exact JSON shape depends on xml2js options -console.log(JSON.stringify(result, null, 2))the first time to see the structure on your system.
That's the bridge between the new world and the old. With Express for HTTP, odbc for SQL and itoolkit for programs, you can build pretty much anything. Let's build something!
