SAP HANA XS Advanced - Modules within Node.js SAP HANA applications

Details

, but you certainly aren’t limited to only the functionality provided by XSJS and XSODATA. You have access to the full programming model of Node.js as well. In this section you will learn how to extend your existing Node.js module in the SAP Web IDE for SAP HANA.

You will learn about how to create and use reusable code in the form of Node.js modules. You will use package.json to define dependencies to these modules which make the installation of them quite easy. You will use one of the most popular modules – express - which helps with the setup the handling of the request and response object. You will use express to handle multiple HTTP handlers in the same service by using routes.

You will learn about the fundamentals of the asynchronous nature of Node.js You will also see how this asynchronous capability allows for non-blocking input and output. This technique is one of the basic things that makes Node.js development different from other JavaScript development and also creates one of the reasons for its growing popularity. You will see how these techniques are applied to common operations like HTTP web service calls or even SAP HANA database access. You will also see how to create language translatable text strings and HANA database queries from Node.js

Your final part of this section will demonstrate the ease at which you can tap into the powerful web sockets capabilities of Node.js You will use web sockets to build a simple chat application. Any message sent from the SAPUI5 client side application will be propagated by the server to all listening clients.

Step 1: Require modules

Return to the Node.js module and the server.js source file. You currently have enough Node.js logic to start up the XSJS compatibility and that’s it. You would like to extend server.js to also handle some purely Node.js code as well. At the same time we still want to support our existing XSJS and XSODATA services from this server.js.

Begin by adding two new Node.js module requirements toward the beginning of the file. One is for the http built-in module and the other is the popular open source module express. One of the most common tasks in Node.js is acting as a web server and handling http requests/responses. Express is a module that wraps and extends the low level http library and provides many additional services.

If you look into the code, you will see a script called initialize is called, and is supposed to be found in the current application directory. Under js, create a directory called utils and a file named initialize.js.

The server.js script is calling two function modules, initExpress and initXSJS. You will define them here as reusable functions using module.exports as in the code below. Copy it into the initialize.js script.

Before you add the initialization for XSJS so that the previous modules can also be executed, take a look at the code you have just pasted.First, the @sap/logging module is used to write log and trace files. By calling the external node module, passport, you are enforcing the UAA authentication you defined earlier. How? By using a JWT (JSON web token) strategy, a middleware function passes the request to the actual route which, in this case, is the xsuaa service you defined at the very beginning using the XS client. You are then injecting an instance of sap-hdb-ext, called xsHDBConn. This module is designed to expose pooled database connections as a Node.js middleware.

Step 2: Add XSJS initialization

You still want to configure the XSJS module but no longer want it to listen directly on your service port. Instead you will start your own instance of express add a route handler for /node which you can code your own Node.js logic. Then you will add the XSJS as a root route handler. Here is the ending piece of code for initialize.js.

Back in server.js, after calling the initialization scripts, you are calling a router. You will now create a router that will accommodate the different Express route requests. You will mimic part of the folder structure suggested in the Express module documentation and create a folder called router with a file called index.js in it.

Routes determine how your application will respond to different requests based on the URI and HTTP method. In the following code, a call aiming at the path node like https://app_URL:port/node will be handled by a script called myNode:.

You are now responding to a GET request to the relative root location of the file (in this case, router). However, in the index.js file inside the router folder, you set the path to /node in the URL. You need to map this route in the xs-app.json file in the HTML5 module.

Step 5: Add the route in the web module

Because it is the web module that is exposed for calls, you need to add the new /node route to the file xs-app.json:

Back in step 1 you added the modules express and passport using the require function. You now need to add them manually in the package.json file with the rest of the dependencies for the js module.

Step 6: Build and Run

You can now run the js module.

After it has built and started running, you can run the web module. This will open a new tab with the index.html. Test the original .xsjs service is running properly after the paths were changed with the route handler. Replace html with xsjs: