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 - Database access from Node.js

Connecting to a SAP HANA database using Node.js

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 Add handler for new example request
โ€”

In the previous tutorial, you added a handler for a path called /node by modifying the files myNode.js.

Add a new route for example1 in myNode.js to get the database connection/client from the express request object (req.db). Then create a prepared statement for the SELECT of SESSION_USER from dummy (dummy is the synonym created in the initial HDI tutorial). Execute the statement and send the results as JSON in the response object.

JavaScript

//Simple Database Select - In-line Callbacks
//Example1 handler
app.get("/example1", (req, res) => {
	let client = req.db;
	client.prepare(
		`SELECT SESSION_USER, CURRENT_SCHEMA
									 FROM "DUMMY"`,
		(err, statement) => {
			if (err) {
				return res.type("text/plain").status(500).send(`ERROR: ${err.toString()}`);
			}
			statement.exec([],
				(err, results) => {
					if (err) {
						return res.type("text/plain").status(500).send(`ERROR: ${err.toString()}`);
					} else {
						var result = JSON.stringify({
							Objects: results
						});
						return res.type("application/json").status(200).send(result);
					}
				});
			return null;
		});
	return null;
});

As follows:

Extend for select
Extend for select

Step 2 Run the node and web modules
+
Step 3 Use the async module to access the database
+

Resources

Discussion

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

Submit detailed feedback Discuss in Community
Steps
Step 1 of 3
1. Add handler for new example request 2. Run the node and web modules 3. Use the async module to access the database

Learn more →