This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
73 lines (63 loc) · 1.93 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Load NodeJS Modules */
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static('public'));
//To Support body on post requests
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
/* Load Local Modules */
var sl = require('./modules/serviceLayer');
//Default SL options on requests
var slOptions = {
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
}
}
var output = {};
//First Thing, coonect to SL and store a SessionID
if (!process.env.APIKey) {
sl.Connect().then((resp) => {
slOptions.headers["Cookie"] = resp.cookie;
})
.catch(error => {
console.error("Can't Connect to Service Layer");
console.error(error);
return; // Abort Execution
})
} else {
slOptions.headers["demoDB"] = process.env.B1_COMP_ENV
slOptions.headers["APIKey"] = process.env.APIKey
}
//EndPoint To retrieve Items from Service Layer
app.get('/GetItems', function (req, res) {
sl.GetItems(slOptions).then((resp) => {
res.setHeader('Content-Type', 'application/json');
res.send(resp);
})
.catch(error => {
console.error("Can't get Items from Service Layer - " + error);
res.send(error);
})
});
//EndPoint to Retrieve Environment Variables
app.get('/GetEnv', function (req, res) {
output.sl = slOptions.headers.Cookie || "API Biz Hub"
output.instance = 0;
output.instance = (process.env.CF_INSTANCE_INDEX * 1) + 1
output.env = process.env.HOME;
res.send(output);
});
// Root path to retrieve Index.html
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'views/index.html'));
});
//Start Server
var port = process.env.PORT || 8080
app.listen(port, function () {
console.log('Example app listening on port ' + port);
});