Modify this app to be a WebApp
.
Install support packages for Node.js
and Express.
npm install express
Execute express to create a template for developing WebApp
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.
"sap-hdbext-promisfied": "latest"
After the edits, your package.json
file would looks something like:
{
"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
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:
app.js
hxeapp.js
public/
views/
bin/
node_modules/
package.json
routes/
$ ls routes/
index.js users.js
Modify the index.js
file to include the following code snippet:
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:
extends layout
block content
h1= title
- each v, k in datarow[0]
p #{k} : #{v}
Start the application
npm start
Open a new browser and access http://localhost:3000/
Notice that the browser displays information like what was displayed in earlier output.