Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos
601

Node.js is a rapidly developing server side development framework based on JavaScript, actually it’s more than the framework you familiar with such as Spring for Java. The greatest difference between Node.js and traditional XSJS is that “it uses an event-driven, non-blocking I/O model”, which makes it more powerful when dealing with high concurrent access with I/O requests (including DB query). You can visit official site of Node.js to know more about it: https://nodejs.org

Many popular RDBMS have possible to be accessed from Node.js, and now is also possible for HANA, SAP has opened the source of Node.js connector for HANA at https://github.com/SAP/node-hdb.

Setup environment

Firstly you need to install Node.js environment on you PC, downloading from https://nodejs.org/en/download/ and following the guide to install. It means finished once you can get the version of Node like:

the later you setup node.js the higher version you may get. We will set the current directory as our workspace, you can initialize your project as follow:

You need to set the basic information of this project, we choose default value for all of them here.

Next let’s install the connector as following:

NPM is the package manager of Node.js which used to download the dependency packages from the central repository, you will find the “node_modules” directory in your workspace, all the dependencies downloaded with npm will be saved in it.

To test whether environment is OK, we will write a simple “hello world”. Create a new js file in workspace, here we named “index.js”, although any text editor is OK to edit node.js script, I suggest Visual Studio Code of Microsoft (https://www.visualstudio.com/en-us/products/code-vs.aspx) for node.js project development.

In the “index.js” file, we input the code as following:

And run the following command in terminal:

We can see the output of the server, and the environment is setup finished so far. Please note that the difference between node.js and Java web is that node program does not running in a container like Tomcat, it’s an independent program running on server.

Connect to SAP HANA

In order to connect to HANA we need to import the connector package into our program, just like add .jar to Java projects build path, here we do as follow:

Function “require” in node.js is used to import other modules, for the modules downloaded from npm just set the name of it, most of the modules have same name as you download with “npm install XXX”, but some of them are not, you can check the “(workspace dir)/node_modules/(module dir)/package.json” to get the real name to use here:

Function require can also import the module written by yourself, you need to input the relative path to the module js file (.js suffix can be ignored), we will show this later.

Just like other environments, we need to set the connection information such as hostname, port and authentication information of databases instance to connect:

You need to input the login information for the HANA instance of yourself, and then run the index.js as before and should return:

Congratulations! We have connected with HANA successfully. Of course other authentication mechanisms and encrypted communication is supported. We will see how to run SQL in next part.