- Modify this app to be a
WebApp
.
Install support packages for Node.js
and Express.
###### Ignore warnings, if any are displayed:
% npm install express
Execute express to create a template for developing WebApp
% cd hxeapp
% express
Above step will 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 hdb
driver.
Edit package.json
and add below line in the dependencies section.
"hdb": "~0.12.4"
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": {
"body-parser": "~1.17.1",
"cookie-parser": "~1.4.3",
"debug": "~2.6.3",
"express": "~4.15.2",
"jade": "~1.11.0",
"morgan": "~1.8.1",
"pug": "^2.0.0-beta6",
"serve-favicon": "~2.4.2",
"hdb": "~0.12.4"
}
}
- 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');
var hdb = require('hdb');
var router = express.Router();
var client = hdb.createClient({
host : '<HXE host id IP>',
port : 39013,
user : 'SYSTEM',
password : '<HXE SYSTEM user password>'
});
var sql = "select * from m_database";
router.get('/', function(req, res, next)
{
client.connect(function (err)
{
if (err)
{
return console.error('Connect error', err);
}
console.log("Connected");
client.exec(sql, function(err, rows)
{
res.render('index', { title: 'Sample Node.js on HANA express', datarow: rows });
});
client.end();
if (err)
{
return console.error('Execute error:', err);
}
});
});
module.exports = router;
-
Replace the <HXE host id IP>
with the IP address or host name of your HANA Express database server. Replace the <HXE SYSTEM 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.