Last Updated on 8 February 2020 by Roberto De Pedrini
Introduction
Node.js is an event-oriented cross-platform Open Source JavaScript runtime for executing JavaScript code, built on Google Chrome’s V8 JavaScript engine. In this document, we will briefly illustrate how to run a simple web node.js application within the IBM PASE environment.
First we will install the open source packages through the Access Client Solution.
IBM i Access Client Solutions offers a platform-independent interface based on Java, which can be run on most operating systems that support Java, including Linux, Mac and Windows. IBM i Access Client Solutions gathers the most commonly used tasks for managing IBM i in one simplified location.
From this link you will find all the useful information to install and manage the open source environment for IBM i.
https://bitbucket.org/ibmi/opensource/src/e9ab9d2e8c1cc1f96b4dd4bd9801d8f8ddf9c8e8/docs/yum/
Express
After installing node.js, we will connect to the PASE environment to install express.js, a microframework that allows you to generate paths (url) more easily and to use templates.
The connection is made by calling the qp2term program or via the putty session present in one of the functions of the access client solution.
- npm install express
- npm install -g express-generator (generates the basis for perfectly functioning projects)
- express –view = ejs (generates the application package with all the necessary folders, to be launched in the new folder)
- npm install (to install all dependencies after installing the ejs application)
- DEBUG = appjson: * npm start (this command will be launched in the shell and will start the application on the http server)
Database
Install idb-connector.
Node.js iDB Connector is an IBM i Node.js Db2 driver open source project from IBM.
npm install idb-connector
Application
View
Insert the html code in the express View folder which will have the extension .ejs N
ote the specifications with “%” in the html format; are the fields passed as requests to the browser by node
<! DOCTYPE html>
<Html>
<Head>
<title> <% = title%> </title>
<link rel = 'stylesheet' href = '/ stylesheets / style.css' />
<link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
</ Head>
<Body>
<h1> <% = title%> </h1>
<p> Welcome to <% = title%> </p>
<table class = "table table-dark">
<Thead>
<Tr>
<th scope = "col"> Nr </th>
<th scope = "col"> Account </th>
<th scope = "col"> Description </th>
<th scope = "col"> Amount </th>
</ Tr>
</ Thead>
<Tbody>
<% if (data.length) {
for (var i = 0; i <data.length; i ++) {%>
<Tr>
<th scope = "row"> <% = (i + 1)%> </th>
<td> <% = dat[i]a.COD%> </td>
<! - <td> <% = dat[i]a.DES%> </td> ->
<td> <% = dat[i]a.IMPO%> </td>
<Td>
<a class="btn btn-success edit" href="../customers/edit/<%=data.i[i]d%> "> Edit </a>
<a class="btn btn-danger delete" onclick="return alert('Are You sure?')" href="../customers/delete/<%=data.i[i]d%> "> Delete </a>
</ Td>
</ Tr>
<%}
} else {%>
<Tr>
<td colspan = "3"> No user </td>
</ Tr>
<%}%>
</ Tbody>
</ Table>
</ Body>
</ Html>
Implement the index.js to insert other routing to the base application. Database connections and sql access are implemented in the page routing. The data is returned in the json format.
var express = require ('express'); var router = express.Router (); / * GET home page. * / router.get ('/', function (req, res, next) { res.render ('index', {title: 'Express'}); }); / * GET new page * / router.get ('/ analysis', function (req, res, next) { const idbc = require ("idb-connector"); const dbconn = new idbc.dbconn (); dbconn.conn ( "* LOCAL"); // db connection const dbstmt = new idbc.dbstmt (dbconn); dbstmt.exec ('select client cod, reasonSoc des, sum (impofc) Taxable from library.file1 inner join library.file2 on client = client2 group by conffc, dscocp ', function (rows, err) {if (err) {
console.log ( 'err');
res.render ('analysis', {title: "Customers - Ejs - Node.js", data: ''});
} else
{Console.log (rows);
/ * return of the page with the rows of the sql * /res.render ('analysis', {title: "Customers - Ejs -Node.js", data: (rows)}); }}); });
module.exports = router;
Web application
DEBUG = appjson: * npm start (this command will be launched in the shell and will start the application on the http server)
Conclusion
Here, we have just created a small application for managing a list of data.
Node is suitable for structuring service-based applications.
The most obvious advantage of Node.js is the possibility of creating server-side applications without having to learn “traditional” programming languages. This allows less experienced users, such as those who deal only with front-end programming, to create multi-user Web applications by managing server-side logic. In other words, Node.js allows you to write JavaScript code as if it were a programming language for dynamic pages, creating everything that usually requires the use of PHP, JSP, ASP or other equivalent technologies.
Thanks for the nice getting started article. There appears to be a few missing steps. Where does appjson come from ? Isn’t there an NPM init required ? Just curious on missing steps as following these exact steps didn’t work for me. Thanks.
Thanks for watching.
Appjson is a folder and not a js(created with mkdir). Go to the folder and run the “/ QOpenSys / pkgs / lib / nodejs12 / bin / express –view = ejs” function.
Then launch npm install.
Just run inside the “npm start” folder and the application is started.
The java script is implemented within index.js present in the routes folder of the express project just generated.
I await feedback.
Good day.