SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

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

Working with Node.js modules

Overview

🎓 intermediate 15 min. SAP HANAIntermediateExpress Edition
Thomas Jung T Thomas Jung November 1, 2022
Created by September 16, 2016
Contributors

Prerequisites

Prerequisites

Steps

Next Steps

Time to Complete

15 Min.


Step 1 New Node.js module
โ€”

Return to the project from the previous tutorial (https://developers.sap.com/tutorials/xsa-xsjs-xsodata.html) and create a new module of type Node.js.

New Module
New Module

Give the module the name: core_node. Leave all other fields at the default and press Finish.

New Module Finish
New Module Finish

In this new Node.js module you have the server.js source file as your mail entry point. You would like to extend server.js to also handle some purely Node.js code as well.

server file
server file

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.

JavaScript
/*eslint no-console: 0, no-unused-vars: 0, no-undef:0, no-process-exit:0*/
/*eslint-env node, es6 */

"use strict";
const https = require("https");
const port = process.env.PORT || 3100;
const server = require("http").createServer();
const express = require("express");

//Initialize Express App for XSA UAA and HDBEXT Middleware
const xsenv = require("@sap/xsenv");
const passport = require("passport");
const xssec = require("@sap/xssec");
const xsHDBConn = require("@sap/hdbext");
xsenv.loadEnv();

https.globalAgent.options.ca = xsenv.loadCertificates();
global.__base = __dirname + "/";
global.__uaa = process.env.UAA_SERVICE_NAME;

//logging
let logging = require("@sap/logging");
let appContext = logging.createAppContext();

//Initialize Express App for XS UAA and HDBEXT Middleware
let app = express();

//Build a JWT Strategy from the bound UAA resource
passport.use("JWT", new xssec.JWTStrategy(xsenv.getServices({
	uaa: {
		tag: "xsuaa"
	}
}).uaa));

//Add XS Logging to Express
app.use(logging.middleware({
	appContext: appContext,
	logNetwork: true
}));

//Add Passport JWT processing
app.use(passport.initialize());

let hanaOptions = xsenv.getServices({
	hana: {
		plan: "hdi-shared"
	}
});

hanaOptions.hana.pooling = true;
//Add Passport for Authentication via JWT + HANA DB connection as Middleware in Expess
app.use(
	xsHDBConn.middleware(hanaOptions.hana),
	passport.authenticate("JWT", {
		session: false
	})
);

//Setup Additional Node.js Routes
require("./router")(app, server);

//Start the Server
server.on("request", app);
server.listen(port, function () {
	console.info(`HTTP Server: ${server.address().port}`);
});

Please 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/hdbext, called xsHDBConn. This module is designed to expose pooled HANA database connections as a Node.js middleware.

Step 2 Create route handler
+
Step 3 Create destination module for route
+
Step 4 Add the route in the web module
+
Step 5 Extend dependencies in package.json
+
Step 6 Build and Run
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 6
1. New Node.js module 2. Create route handler 3. Create destination module for route 4. Add the route in the web module 5. Extend dependencies in package.json 6. Build and Run

Learn more →