Skip to Content

Connect Using the SAP HANA Node.js Interface

test
0 %
Connect Using the SAP HANA Node.js Interface
Details

Connect Using the SAP HANA Node.js Interface

July 9, 2020
Created by
March 29, 2020
Create and debug a Node.js application that connects to SAP HANA using the SAP HANA client.

You will learn

  • How to install Node.js and the SAP HANA client Node.js driver
  • How to create a Node.js application that queries a SAP HANA database

Prerequisites

  • You have completed the first 3 tutorials in this mission.

Node.js provides a JavaScript runtime outside of the browser and uses an asynchronous event driven programming model. For more details, see Introduction to Node.js.


Step 1: Install Node.js

The first step is to check if you have Node.js installed and what version it is. Enter the following command:

node -v  

If Node.js is installed, it will return the currently installed version, such as v12.16.3.

If node is not installed, download the long-term support (LTS) version of Node.js from Node.js Download.

An install is not provided on Linux. The installation instructions can be found here.

During the installation, there is no need to check the following box as you do not need to install Chocolatey.

Chocolatey

The SAP HANA client provides a 32-bt and a 64-bit install, as does Node.js. The Node.js driver provIded with the SAP HANA client is available for 64-bit only. For additional details see SAP note 2939501 - SAP HANA Client Supported Platforms for 2.5 and later.

Log on to answer question
Step 2: Install SAP HANA client for Node.js from NPM

Node.js packages are available using NPM, which is the standard package manager for Node.js.

  1. Enter @sap/hana-client, and click Search.

    Search for hana-client

    The page for the SAP HANA Node.js package on npm is shown below.

    npm page for hana-client

    It contains additional sample code, a weekly download counter, information about previous versions and the command to install the package using the npm command line interface (cli).

  2. Create a folder named node and enter the newly created directory.

    mkdir %HOMEPATH%\HANAClientsTutorial\node
    cd %HOMEPATH%\HANAClientsTutorial\node
    
    mkdir $HOME/HANAClientsTutorial/node
    cd $HOME/HANAClientsTutorial/node
    
  3. Initialize the project and install the hana-client driver from NPM.

    npm init -y
    npm install @sap/hana-client
    

    The hana-client driver is also available from the HANA client install folder. The install location was set during the install.

    npm install C:\SAP\hdbclient\node
    

    If you encounter an error about permissions, on Microsoft Windows, run or open the command prompt as an administrator, or use sudo on Linux or Mac.

The following command will list the Node.js modules that are now installed locally into the HANAClientsTutorial\node folder. Note that the extraneous message can be ignored.

npm list

Some Tips

At this point, the SAP HANA client module has been installed into the HANAClientsTutorials\node\node_modules folder and added as a dependency in the packages.json file. The following is some extra optional information on NPM.


Node.js modules can also be installed globally. To see the list of node modules installed globally enter the following command.

The depth parameter can be used to specify the number of levels to show when displaying module dependencies. By setting depth=x, a tree-structure is outputted that shows modules that are x levels below the top-level module.

npm list -g
npm list -g --depth=0

Command line help for NPM is available. A few examples of this are shown below.

npm help
npm help list

Additional information can be found out for a module, such as the debug module, via the info command.

npm info @sap/hana-client

The following commands can be used to view the latest available version of a package, remove a package, add a specific version of a package and then update it to the latest version.

npm view @sap/hana-client version
npm uninstall @sap/hana-client
npm install @sap/hana-client@2.4.167
npm list @sap/hana-client
npm update @sap/hana-client
npm list @sap/hana-client

SAP also maintains a NPM registry. The following steps show some commands on how to configure NPM to use the registry npm.sap.com.

npm config list
npm config set @sap:registry="https://npm.sap.com"
npm info @sap/hana-client
npm config set @sap:registry=
Log on to answer question
Step 3: Create a Node.js application that queries SAP HANA
  1. Open an editor on a file named nodeQuery.js.

    cd %HOMEPATH%\HANAClientsTutorial\node
    notepad nodeQuery.js
    

    Substitute pico below for your preferred text editor.

    cd $HOME/HANAClientsTutorial/node
    pico nodeQuery.js
    
  2. Add the code below to nodeQuery.js and update the serverNode values in the connOptions to match your SAP HANA host and port.

    'use strict';
    const { PerformanceObserver, performance } = require('perf_hooks');
    var t0 = performance.now();
    var util = require('util');
    var hana = require('@sap/hana-client');
    
    var connOptions = {
        serverNode: '@User1UserKey',
        //serverNode: 'your host:your port',
        //UID: 'USER1',
        //PWD: 'Password1',
        encrypt: 'true',  //Must be set to true when connecting to SAP HANA Cloud
        sslValidateCertificate: 'false',  //Must be set to false when connecting
        //to an SAP HANA, express edition instance that uses a self signed certificate.
    
        //Below setting is used to specify where the trust store is
        //ssltruststore: '/home/dan/.ssl/trust2.pem',
    
        //Alternatively provide the contents of the certificate directly (DigiCertGlobalRootCA.pem)
        //ssltruststore: '-----BEGIN CERTIFICATE-----MIIDrz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cBAI+0tKIJFPnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0lsYSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQkCAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=-----END CERTIFICATE-----'
    };
    
    var connection = hana.createConnection();
    connection.connect(connOptions, function(err) {
        if (err) {
            return console.error(err);
        }
        var sql = 'select TITLE, FIRSTNAME, NAME from HOTEL.CUSTOMER;';
        var rows = connection.exec(sql, function(err, rows) {
            if (err) {
                return console.error(err);
            }
            console.log(util.inspect(rows, { colors: false }));
            var t1 = performance.now();
            console.log("time in ms " +  (t1 - t0));
            connection.disconnect(function(err) {
                if (err) {
                    return console.error(err);
                }   
            });
        });
    });
    
  3. Run the app.

    node nodeQuery.js
    
    SAP HANA Express result

Note the above app makes use of some of the SAP HANA client Node.js driver methods, such as connect, execute and disconnect.

In nodeQuery.js, the asynchronous versions of these methods are used because the optional callback function is provided. For readers that are unfamiliar with synchronous and asynchronous operations, see The Node.js Event Loop, Timers, and process.nextTick().

To enable debug logging of the SAP HANA Node.js client, enter the following command and then rerun the app.

set DEBUG=*
export DEBUG=*
debug output

The value of the environment variable DEBUG can be seen and removed with the commands below.

set DEBUG
set DEBUG=
set DEBUG
printenv | grep DEBUG
unset DEBUG
printenv | grep DEBUG
Log on to answer question
Step 4: Debug the application

Visual Studio Code can be used to run and debug a Node.js application.

  1. Download Visual Studio Code.

  2. In Visual Studio Code, choose File | Add Folder to Workspace and then add the HANAClientsTutorial folder.

    Workspace
  3. Open the file nodeQuery.js.

  4. Place a breakpoint inside the connection.exec callback. Select Run | Start Debugging.

    Notice that the debug view becomes active and that the RUN option is Node.js.

    Notice that the program stops running at the breakpoint that was set. Observe the variable values in the leftmost pane. Step through code.

    VS Code Debugging

Congratulations! You have created and debugged a Node.js application that connects to and queries an SAP HANA database.

Which of the following statements are true?
×

Next Steps

Back to top