World of NodeJS On IBM I
World of NodeJS On IBM I
js
on IBM i
Presented by
Scott Klement
http://www.profoundlogic.com
To understand recursion
one must first understand recursion
The Agenda
Agenda for this session:
1. Node.js introduction
• what it is
• how popular it is
• philosophy
2. Node.js basics
• How it works
• Hello World
3. Node.js Ecosystem
• Node Package Manager (npm)
• Demonstrations
− Express
− Node Inspector
− E-mail
− Spreadsheet
2
What is Node.js
• Cobol 1959
• RPG II 1968
• C 1972
• RPG III 1978
• C++ 1983
• Python 1991
• RPG IV 1994
• Java 1995
• PHP 1995
• Ruby 1995
• Node.js 2009
New enough to have learned from previous languages, old enough to have
grown very robust
4
Browser vs. Server
Popularity of JavaScript
6
Think About It
8
More Companies…
10
Node.js Philosophy
David's take:
11
Support on IBM i
IBM provides support
• 5733OPS no charge option
• Runs in PASE
• DB2 module in provided
• Source kept in IFS
• Edit with any editor…. RDI and Notepad++ (free) work great.
• Integration with IBM I via XMLSERVICE
o Call native programs, access objects, etc
12
Learning Node / JavaScript
I cannot teach the whole language in this session!
• But, maybe you already know JavaScript?
• Or, once you know one language, learning another isn't hard.
Also, just search Google for what you're looking for. This can be really helpful!
13
if ( myVariable == "something") {
doSomething();
}
else {
doSomethingElse();
}
14
JavaScript Syntax Basics (2 of 3)
do {
// do while myNumber > 1, test condition at end of loop.
}
while ( myNumber > 1 );
var employee_rec = {
first: "Scott",
last: "Klement",
num: 1000
}
16
Node Modules
....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
17
18
Hello World (1 of 2)
Since the "http" module contains all the code needed to communicate
with HTTP, it's very easy to write a simple web server in Node.
....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
// Configure our HTTP server to respond with Hello World to all requests.
var server = httpServer.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.end("<h1>Hello World</h1>");
console.log("response sent!");
});
// Even though this ends the program, it will remain active since
// the node event loop has more to do (due to http server above)
console.log("Server running, connect to http://your-server:9876");
19
Hello World (2 of 2)
20
The Real Power of Node.js
The real power of Node.js comes from the "ecosystem" of modules that are
available to use!
21
Node.js on IBM i was written for AIX and runs under PASE. Some tools (npm is one, there
are others) try to use Unix terminal features, such a colors, and so assume you are using a
Unix terminal emulator.
This is not required for using Node.js, but it does work very nicely, and we prefer it!
A good way to do that on IBM i is to start the IBM-supplied SSHD and connect with a Unix
terminal program such as Putty. http://www.chiark.greenend.org.uk/~sgtatham/putty/
STRTCPSVR SERVER(*SSHD)
22
Run Hello World from Putty
Press Control-C to
stop server.
23
By default packages are listed/installed in the local project. Add --global to make
it install system-wide. (or -g for short!)
By default, it tries to use the most current version of a package available. You can
add @VERSION to the package name to install a particular version, such as
24
Express-Generator
The basic http module in Node makes it easy to write your own HTTP server,
but maybe you want something that does a little more work for you?
Express Generator
- add-on for Express.js that generates applications for you
- installs a command line tool that you run to make things
- build a shell application in seconds!
Express-Generator
Once installed, you can generate an application skeleton like this:
express yourApp
This creates a yourApp directory and generates all of the files and
subdirectories to provide a good skeleton of an Express.js application.
cd /home/sklement/node
$
express –e demoApp
[messages show generated files]
$
cd demoApp
$
npm install
[messages show building files]
$
npm start
26
Accessing DB2 for i in Node.js
IBM includes support for DB2 for i (the database built in to IBM i) in the form of
another Node.js package. It is installed with 5733OPS, PTF SI61394
dbconn.conn("*LOCAL");
27
stm.exec(sql, function(result) {
result.forEach(function(row) {
}
}
28
Example, ExcelJs
Distributing reports as spreadsheets has been very successful for me, and so I
wanted to try it in Node.js.
There are a bunch of them, there are also tools for other spreadsheets (such as
Google docs spreadsheets) ExcelJs looked decent, so we installed it.
For the sake of example, this will be installed locally into the project (not system-wide)
$
mkdir spreadsheet
$
cd spreadsheet
$
npm install exceljs
29
stm.exec(sql, function(rs) {
rs.forEach(function(row) {
worksheet.addRow(row);
});
Example, Output
32
Debugging With Node-Inspector
If you've ever debugged JavaScript in the browser, you'll find it just as easy and
powerful to debug Node.js with Node Inspector. It uses Chrome's built-in debugger
remotely.
If you run this on your PC, it'll automatically open Chrome for you. If you debug it
from IBM i, you'll have to open Chrome manually.
This will open the Chrome debugger, and you can debug it, step through it, display
variables, etc. Very powerful!
33
Node-Inspector, Screenshot
34
Example, Node Mailer
It would be convenient to be able to e-mail the spreadsheets we created. For
example, a nightly batch job might create reports, and need to send them to the
appropriate people.
$
mkdir email
$
cd email
$
npm install nodemailer
35
Or, perhaps you'd rather use a separate e-mail server? For example, an
Exchange server?
36
Nodemailer, Building a Message
A JavaScript object (like an RPG data structure) contains fields for the various
things needed in an e-mail message
• from = sender's e-mail address
• to = recipient's e-mail address
• subject = message subject
• html = body of e-mail in HTML format (can be as long as needed)
• attachments = JavaScript array of IFS files to include as attachments
var message = {
from: "Node.js Email Example <[email protected]>",
to: "Scott Klement <[email protected]>",
subject: "Node.js Email Example",
html: "<b>Sent from Node.js on IBM i</b>",
attachments: [{
path: "orders.xlsx"
}]
};
37
if (error) {
console.log(error);
});
38
Nodemailer, Output (in Outlook)
39
Final Thoughts
40
This Presentation
Thank you!
41