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" >

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.

Related Posts
DB2 for i SQL – String Manipulation – POSSTR-LOCATE-LOCATE_IN_STRING (EN)

Introduction Often, in our applications, we need to work with text strings, and DB2 SQL can come in very useful Read more

DB2 for i – FAQ & Howtos (EN)

DB2 Database and SQL ... maybe the most important things on IBM i platform: here's a collection of FAQs, tips Read more

IBM i 7.4 Announcement (En)

Comes directly with the Easter egg this IBM announcement for the news of the IBM i 7.4 version, iNext version Read more

Generated Always Columns (EN)

Introduction "Generated Always Column": are columns, table fields, filled by DB2 engine: something like columns with a default value but Read more

View Comments

  • 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.

Recent Posts

Code for IBM i 2.10.0 – Debug IBM i App with Visual Studio Code

Debugging functions with Visual Studio Code have been available for some time but this new version 2.10.0 simplifies the handling…

1 day ago

ObjectConnect over TCP/IP

Two and a half years after my post Transferring objects with ObjectConnect and Enterprise Extender, I finally got around to…

2 days ago

SQL: SELECT with “superpowers”

With a little trick even a simple SELECT statement can execute any system command! Let's see how to do that...

5 months ago

NetServer for everyone – part 5

A mini-serial guide to configuring, managing, using, and troubleshooting the IBM i NetServer

1 year ago

NetServer for everyone – part 4

A mini-serial guide to configuring, managing, using, and troubleshooting the IBM i NetServer

1 year ago

NetServer for everyone – part 3

A mini-serial guide to configuring, managing, using, and troubleshooting the IBM i NetServer

1 year ago