03 - Open Source (EN)03b - Node.js (EN)

Node.js – Ejs and IBM i Web application

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)
Open your browser and go to the IP address of your IbmI: welcome to express will appear

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.

About author

Project manager, business management consultant, application development with Rpgfree, Python, JavaScript. I entered the IT world when the As400 was born, I am a supporter of the Ibm-I platform and its enormous potential and I believe in the resources offered by the community by open source. I am grateful to collaborate with FAQ400 because I fully share the objectives.

2 Comments

Leave a Reply to Richard Schoen Cancel reply

%d bloggers like this: