POWERUp 2020 Guided Lab (Use Node.js to build Business Apps)

This lab walks you through building a series of Node.js applications that work with employee data. To get started, make sure your browser is pointed to: https://spaces.profoundjs.com/ide/profound-logic/guided-lab/.

The lab requires only a browser to complete. You can use the virtual lab PC provided by POWERUp or your own computer to complete the exercises.

To view this document in a separate browser tab, click here.

Agenda

You will use Profound.js Spaces to build the following modules:

The exercises are intended to be followed in order.

What is the Profound.js Spaces website?

What you need to get started and complete the exercises

The Profound.js Spaces site (https://spaces.profoundjs.com) provides all the tools you need. You just need a modern browser, such as:

Using Spaces

Let's get started!

screenshot

Exercise 1 (Employee Lookup)

screenshot   screenshot   screenshot

Build Employee Lookup Screen

screenshot

Find employee database table

screenshot   screenshot

Use employee database table

Bind Buttons

screenshot screenshot

The case of the field names matters. "ok" is not the same as "Ok".

Name your screen + Save

screenshot

screenshot

Create Node.js script for Employee Lookup

screenshot

screenshot

Code Explained

exports.default = lookup
pjs.defineDisplay("display", "lookup.json");
display.lookup.execute();
while (!cancel) {
  // ...
}
pjs.query("SELECT * FROM employees WHERE employeeNumber = ?", [employeeNumber]);
var record = results[0];
pjs.setFields(record);

Try your application

screenshot

screenshot

Exercise 2 (Using database-driven auto-complete)

screenshot   screenshot

Try using the auto-complete feature

screenshot

Exercise 3 (Adding a dynamic image)

In this exrecise, we'll add an employee photo to the Employee Lookup screen.

Add employee photo as Image Widget

Drag image widget to the Visual Designer canvas:

screenshot

Bind the image source property to the field name source

screenshot

screenshot

Assign employee photo source field

Add the following line of code:

screenshot

Test employee photo feature

screenshot

Exercise 4 (Employee Listing)

screenshot   screenshot   screenshot

Add Grid and Ok Button

screenshot

Name screen and grid; then save Rich Display File

Create Node.js script for Employee Listing

Code Explained

display.grid.replaceRecords(data);

Try running Employee Listing option from Menu

screenshot

Exercise 5 (Enhance the grid)

In the Visual Designer, select the grid and set the following properties to true:

Save, then rerun the Employee Listing to test out the features

screenshot

Exercise 6 (Connect listing to lookup)

This exercise allows users to click on an employee in the grid to see employee details.

Configure row selection

Set the following grid properties:

Add call from list.js to lookup.js

screenshot

Code Explained

display.grid.readChanged();
if (!display.endOfData() && selected) {
  pjs.call("lookup.js", rec.employeeNumber);
}

Alternate approach

There are multiple ways to retrieve record data from a grid.

Using ES6 syntax, we can quickly retrieve all selected records into an array and then use the first record in the array.

var selectedRecords = display.grid.filter(entry => entry.selected);
var record = selectedRecords[0];
if (record) {
  pjs.call("lookup.js", record.employeeNumber);
}

selectedRecords[0] works well because single selection only allows one record to be selected at a time.

Modify lookup.js to receive parameter

screenshot

Code Explained

if (parm) employeeNumber = parm;

Test connected modules

screenshot

Exercise 7 (Listing Web Service)


function get_employees(request, response) {
  // ...
}

Profound.js Spaces use the popular Express.js web framework as its foundation. All Express.js Web Services receive a request and a response parameter.

Create Web Service out of get_employees.js

Right-click the saved file under Files and select Properties:

screenshot

Then select the following:

screenshot

Test Listing Web Service

screenshot   screenshot

Exercise 8 (Details Web Service)

Code Explained

var records = pjs.query("SELECT * FROM employees WHERE employeeNumber = ?", [request.params.empNum]);

if (records.length > 0) response.send({ success: true, record: records[0] });
else response.send({ success: false });

Create Web Service out of get_employee.js

Right-click the saved file under Files and select Properties:

screenshot

Then select the following:

screenshot

Test Details Web Service

screenshot   screenshot

Try modifying employee number in the URL.

Exercise 9 (Consuming a Web Service)

Call a Web Service

Replace:

screenshot

With:

screenshot

Code Explained

Test Consuming a Web Service

screenshot

screenshot

Exercise 10 (Using npm to email Employee List)

In this exercise, you will write code to email a list of employees to yourself.

Install gmail-send npm package

You can install pacakges from the Server menu:

screenshot   screenshot

screenshot

Alternatively, use the Terminal feature and type: npm install gmail-send

What is npm?

Create Node.js script named email.js

Code Explained

Test the email functionality

screenshot

You are all done! Thank you!

Thanks for trying out this lab!

As a next step, try to think of creative ways to enhance the screens or the funcionality behind the screens you built.

Leave questions or comments for us here: