on 2020 Nov 04 7:20 PM
Hi
I deployed my my application on SAP Cloud Foundry and I am trying to do edits in vscode using xsenv.loadEnv() but its not wotrking.
Can anyone help me what is wrong? below is the code.
/*eslint no-console: 0*/
"use strict";
const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const compression = require("compression");
const xsenv = require("@sap/xsenv");
xsenv.loadEnv();
const xssec = require("@sap/xssec");
const xsHDBConn = require("@sap/hdbext");
const JWTtoken = require("./middleware/JWTtoken/tokenchecks")()
const passport = require("passport");
const port = process.env.PORT || 3000;
//Initialize Express App for XS UAA and HDBEXT Middleware
const app = express();
//logging
app.use(morgan("dev"));
app.use(
bodyParser.json({
limit: "200mb"
})
);
const logging = require("@sap/logging");
const appContext = logging.createAppContext();
const helmet = require("helmet");
// ...
app.use(helmet());
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", "sapui5.hana.ondemand.com"],
scriptSrc: ["'self'", "sapui5.hana.ondemand.com"]
}
}));
// Sets "Referrer-Policy: no-referrer".
app.use(helmet.referrerPolicy({
policy: "no-referrer"
}));
// passport.use("JWT", new xssec.JWTStrategy(xsenv.getServices({
// uaa: {
// tag: "xsuaa"
// }
// }).uaa));
// app.use(logging.middleware({
// appContext: appContext,
// logNetwork: true
// }));
// app.use(passport.initialize());
var hanaOptions = xsenv.getServices({
hana: {
tag: "hana"
}
});
hanaOptions.hana.pooling = true;
app.use(
// passport.authenticate("JWT", {
// session: false
// }),
xsHDBConn.middleware(hanaOptions.hana)
);
//Compression
app.use(require("compression")({
threshold: "1b"
}));
// Handling cors
const cors = require("cors");
app.use(cors());
// compress responses
app.use(compression());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.get("/", async(req, res) => {
// try {
console.log(req)
const dbClass = require("sap-hdbext-promisfied")
let db = new dbClass(req.db);
const statement = await db.preparePromisified(`SELECT SESSION_USER, CURRENT_SCHEMA FROM "DUMMY"`)
const results = await db.statementExecPromisified(statement, [])
let result = JSON.stringify({
Objects: results
})
return res.type("application/json").status(200).send(result)
// } catch (e) {
// return res.type("text/plain").status(500).send(`ERROR: ${e.toString()}`)
// }
});
response : [object Object]
Hi,
if this statement:
var hanaOptions = xsenv.getServices({
if this statement returns a value, then your loadEnv has worked fine.
You can also print process.env to see if the values in default-env have been loaded
Now, if your express middleware is failing, maybe the content of your default-env.json is not correct, or not complete, or not uptodate.
What exactly is failing in your express?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
I had that problem as well.
I had to solve it with hard-coded path:
xsenv.loadEnv('C:/dev/myproject/default-env.json')
Not nice, but solved the problem
Hope it helps,
Kind Regards,
Carlos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey,
I tried hardcoding as well, its still not working. I am able to print the hanaOptions it contains user and credentials. But when I use that in middleware it breaks my express.
User | Count |
---|---|
62 | |
9 | |
8 | |
6 | |
6 | |
6 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.