cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to run express server with exenv.loadEnv() in VSCode

former_member25214
Participant
0 Kudos
661

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]

Accepted Solutions (1)

Accepted Solutions (1)

CarlosRoggan
Product and Topic Expert
Product and Topic Expert

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?

former_member25214
Participant
0 Kudos

I figured it out, it was not problem with the xsenv but its the problem of hdbext, I am not able to connect to the hana database using hdbext.middleware(connectionjson). and that's why it's printing [object Object].

I'll close this question.

Answers (1)

Answers (1)

CarlosRoggan
Product and Topic Expert
Product and Topic Expert

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

former_member25214
Participant

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.