Skip to Content

Deploy a Node.js Application for SAP HANA

Deploy a sample Node.js application which connects to SAP HANA, express edition or SAP HANA Cloud.
You will learn

This tutorial will guide you through the process to deploy a sample Node.js application which connects to SAP HANA, express edition or SAP HANA Cloud.

jung-thomasThomas JungJanuary 6, 2022
Created by
thecodester
March 4, 2018
Contributors
thecodester
jung-thomas

Prerequisites

and Assumptions
- User knows how to install packages and develop using Node.js
- Setup: HANA, express edition or SAP HANA Cloud must be running and accessible from your client platform. For instructions on how to setup a HANA, express edition see the HANA Express database deploy tutorial. For instructions on starting SAP HANA Cloud trial see the Help Thomas Get Started with SAP HANA.
- This tutorial assumes that you have a database login that can access the M_DATABASE view in the HANA, express edition SystemDB or in SAP HANA Cloud.
- Tutorials: Setup Node.js to connect to SAP HANA

Additional Information

  • Step 1

    Install support package for Node.js and Express:

    Install the Node.js package called express-generator globally:

    shell
    Copy
    npm install -g express-generator
    
  • Step 2

    Create a simple standalone node.js app:

    shell
    Copy
    % mkdir hxeapp
    % cd hxeapp
    

    Edit a file named hxeapp.js with the following content:

    JavaScript
    Copy
    console.log ("Hello Node World!")
    

    Test the simple app created in the previous step

    shell
    Copy
    cd hxeapp
    node hxeapp.js
    

    Access the HANA content from this Node application. First install HANA database driver to Node. Ignore any warnings that it may produce.

    shell
    Copy
    npm install sap-hdbext-promisfied
    

    Create a Node application that allows you to access SAP HANA, express edition or HANA Cloud data.

    Open the hxeapps.js file and add the following content:

    JavaScript
    Copy
    // Reference HANA driver in the Node app and update the connection details
    let dbClass = require('sap-hdbext-promisfied')
    
    // Modify the host IP and password based on your system information
    let hanaConfig = {
        host: '<HANA hostname>',
        port: `<HANA Port>`,
        sslValidateCertificate: false,
        encrypt: true,
        user: `<SYSTEM|DBADMIN>`,
        password: '<HANA SYSTEM or DBADMIN user password>'
    }
    
    // Execute the query and output the results
    async function intro() {
        try {
            let db = new dbClass(await dbClass.createConnection(hanaConfig))
            let rows = await db.execSQL("select * from m_database")
            console.log('Results:', rows)
        } catch (error) {
            return console.error(error)
        }
    }
    intro()
    

    Replace the <HANA hostname> with the IP address or host name of your HANA Express or HANA Cloud database server. Replace user with SYSTEM if targeting HANA Express or DBADMIN if targeting HANA Cloud. Replace HANA Port with the SQL port for your HANA system. Replace the <HANA SYSTEM or DBADMIN user password> with your database system user password. Then save changes.

    Start your application:

    shell
    Copy
    node hxeapp.js
    

    The above command should produce results something like the below:

    Results
  • Step 3

    Modify this app to be a WebApp.

    Install support packages for Node.js and Express.

    shell
    Copy
    npm install express
    

    Execute express to create a template for developing WebApp

    shell
    Copy
    express
    

    Above step will create template files with dependencies.

    Note: It was installed manually previously. We want to do it automatically along with other packages

    Modify package.json to include @sap/hdbext driver.

    Edit package.json and add below line in the dependencies section.

    json
    Copy
    "sap-hdbext-promisfied": "latest"
    

    After the edits, your package.json file would looks something like:

    json
    Copy
    {
      "name": "hxeapp",
      "version": "0.0.0",
      "private": true,
      "scripts": {
        "start": "node ./bin/www"
      },
      "dependencies": {
        "cookie-parser": "~1.4.4",
        "debug": "~2.6.9",
        "express": "~4.16.1",
        "http-errors": "~1.6.3",
        "jade": "~1.11.0",
        "morgan": "~1.9.1",
        "sap-hdbext-promisfied": "latest"
      }
    }
    

    Call npm install to pull the dependencies

    shell
    Copy
    npm install
    

    Create an app that does what our hxeapp.js does, but now we should modify the template file to do it.

    Your directory content would look something like:

    shell
    Copy
    app.js  
    hxeapp.js      
    public/  
    views/
    bin/    
    node_modules/  
    package.json      
    routes/
    
    shell
    Copy
    $ ls routes/
    index.js  users.js
    

    Modify the index.js file to include the following code snippet:

    JavaScript
    Copy
    var express = require('express')
    let dbClass = require('sap-hdbext-promisfied')
    var router = express.Router()
    var hanaConfig = {
      host: '<HANA hostname>',
      port: `<HANA Port>`,
      sslValidateCertificate: false,
      encrypt: true,
      user: `<SYSTEM|DBADMIN>`,
      password: '<HANA SYSTEM or DBADMIN user password>'
    }
    
    /* GET home page. */
    router.get('/', async function (req, res, next) {
      try {
        let db = new dbClass(await dbClass.createConnection(hanaConfig))
        let rows = await db.execSQL("select * from m_database")
        res.render('index', { title: 'Sample Node.js on HANA ', datarow: rows })
      } catch (error) {
        res.send(error.toString()).status(500)
        return console.error(error)
      }
    })
    module.exports = router
    

    Replace the <HANA hostname> with the IP address or host name of your SAP HANA, express edition or SAP HANA Cloud database server. Replace user with SYSTEM if targeting HANA Express or DBADMIN if targeting SAP HANA Cloud. Replace HANA Port with the SQL port for your SAP HANA system. Replace the <HANA SYSTEM or DBADMIN user password> with your database system user password. Then save changes.

    Edit the file views/index.jade that controls the WebApp output layout as follows:

    text
    Copy
    extends layout
    
    block content
      h1= title
      - each v, k in datarow[0]
        p #{k} : #{v}
    

    Start the application

    shell
    Copy
    npm start
    

    Open a new browser and access http://localhost:3000/
    Notice that the browser displays information like what was displayed in earlier output.

Back to top