sap-tutorials / Tutorials

Tutorials on sap.com
https://developers.sap.com/tutorial-navigator.html
Creative Commons Attribution 4.0 International
742 stars 780 forks source link

ODATA V2 support not working HANA-XSA Project #6848

Closed saket-sahi closed 3 years ago

saket-sahi commented 3 years ago

I am trying to add ODATA V2 support in my HANA -XSA project. I have completed the changes to the package.json and loaded npm odata-v2-proxy on CDS bootstrap. Below is the code. host:port/odata/v4/catalogservice is working but host:port/odata/v2/catalogservice is not working. Error:Cannot GET /odata/v2/catalogservice

var https = require("https"); var port = process.env.PORT || 4000; var express = require("express"); let app = express(); const server = require("https").createServer(app); const cds = require("@sap/cds"); const xsenv = require("@sap/xsenv"); const xsHDBConn = require("@sap/hdbext"); const xssec = require("@sap/xssec"); const passport = require("passport"); var routerData = require('./router/index'); const proxy = require('@sap/cds-odata-v2-adapter-proxy');

xsenv.loadEnv(); https.globalAgent.options.ca = xsenv.loadCertificates(); global.base = dirname + "/"; global.__uaa = process.env.UAA_SERVICE_NAME; passport.use("JWT", new xssec.JWTStrategy(xsenv.getServices({ uaa: { tag: "xsuaa" } }).uaa)); app.use(passport.initialize()); let hanaOptions = xsenv.getServices({hana:{tag:"hana"}}); hanaOptions.hana.pooling = true; app.use( xsHDBConn.middleware(hanaOptions.hana)

); var options = { kind: "hana", logLevel: "error"

};

cds.connect(options); cds.on('bootstrap',(app)=> app.use(proxy()); var odataURL = "/odata/v4/CatalogService/"; cds.serve("gen/csn.json", { crashOnError: false }) .at(odataURL) .with(require("./lib/handlers/cat")) .in(app) .catch((err) => { console.log(err); process.exit(1);
}); app.use('/node',routerData);
app.get("/", (req, res) => { res.redirect(odataURL); }); app.get("/node", (req, res) => { res.redirect(odataURL); });

app.listen(port);

jung-thomas commented 3 years ago

By default the generated proxy URL path isn't going to be /odata/v2/*. It is just normally just /v2. You can see the proxy rewrite in the logs when you do a cds run: image

If you want to configure the url path for the proxy you can do that as well. In my sample I configure it to /odata/v2 by adding this to the package.json: https://github.com/SAP-samples/hana-opensap-cloud-2020/blob/main/package.json#L129

saket-sahi commented 3 years ago

proxy created -> http://localhost:50051 proxy rewrite rule created :"^v2" host:51003:/catalogservice-> works fine host:51002:/v2/catalogservice-> internal server error

jung-thomas commented 3 years ago

Did you add the configuration in the package.json I pointed to? This is also necessary to configure a non-standard V4 source. From your code you are setting a non-standard V4 URL path as well so you need to let the v2 proxy know about that

saket-sahi commented 3 years ago

I have added the configuration from beginning. It is still not working.I have couple of questions. I am running the service on HANA on-prem. Why it shows localhost proxy created -> http://localhost:50051 instead of the actual host. Do i need to add specific changes just for on-prem systems Also why the services are not working if i remove cds.serve().to().in(app). shouldn't this code be used only to generate multiple service endpoint for the same service.

jung-thomas commented 3 years ago

Because it's connecting internally within the same Node.js service instance. Therefore localhost. No I don't know of any specific changes for on premise. I've used this on HANA Express without any changes. Not sure what exactly is failing or what this is in the context of the overall application.

saket-sahi commented 3 years ago

Just one last thing. We can run XSA services without server.js. It is just for custom bootstrapping. What changes do i need in package.json to run the services without server.js.

jung-thomas commented 3 years ago

If you want OData V2 you must have a server.js so that you can enhance the boostrapping. See the documentation here: https://cap.cloud.sap/docs/advanced/odata#odata-v2-support

saket-sahi commented 3 years ago

If i do not want odata v2 then i should not be having server.js. My odata v4 services should run with out server.js. but they are still not running without server.js. package,json in srv folder has "start":"node server.js". What if i just want to have ODATA V4 only. Do i need to add anything in package,.json . Its just anaylsis purpose. That will give me a better insight and probably i will be able to fix V2 issue as well

jung-thomas commented 3 years ago

If you don't want OData V2 support then, no, you don't need a server.js. cds run command should start your service with just a package.json and a cds model file somewhere. The server.js is only if you want to customize the startup process.

saket-sahi commented 3 years ago

It give error. As start command expects start: node server.js . What changes are required in package.json to run it without server.js?

jung-thomas commented 3 years ago

What does your package.json scripts section look like? Specifically the start script. If its cds run then I don't see why it would require a server.js.

saket-sahi commented 3 years ago

in srv package.json scripts:{ "start":"cds run" } Error SPAWN CDS EACCES ealrier with server,js it node server.js

jung-thomas commented 3 years ago

Error SPAWN CDS EACCES - that's the complete error log? Are you sure you don't still have it running in another terminal session and the port is already in use?

saket-sahi commented 3 years ago

7/20/21 6:25:25.502 PM [APP/3-0] ERR events.js:174 7/20/21 6:25:25.503 PM [APP/3-0] ERR throw er; // Unhandled 'error' event 7/20/21 6:25:25.503 PM [APP/3-0] ERR ^ 7/20/21 6:25:25.503 PM [APP/3-0] ERR 7/20/21 6:25:25.503 PM [APP/3-0] ERR Error: spawn cds EACCES 7/20/21 6:25:25.503 PM [APP/3-0] ERR at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19) 7/20/21 6:25:25.503 PM [APP/3-0] ERR at onErrorNT (internal/child_process.js:415:16) 7/20/21 6:25:25.503 PM [APP/3-0] ERR at process._tickCallback (internal/process/next_tick.js:63:19) 7/20/21 6:25:25.503 PM [APP/3-0] ERR Emitted 'error' event at: 7/20/21 6:25:25.503 PM [APP/3-0] ERR at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12) 7/20/21 6:25:25.503 PM [APP/3-0] ERR at onErrorNT (internal/child_process.js:415:16) 7/20/21 6:25:25.503 PM [APP/3-0] ERR at process._tickCallback (internal/process/next_tick.js:63:19)

saket-sahi commented 3 years ago

This is the error on start:cds run script

saket-sahi commented 3 years ago

I think it is the version issue. I have successfully implemented a model on Trial BTP. On BTP Trial I was using proxy version ^1.6 and it was generating ODATA V2 metadata. I changed the proxy version to 1.4.13 which is current proxy version of my XSA server and I started getting error for ODATA V2. "Cannot GET /odata/v2/POService/"

jung-thomas commented 3 years ago

If you configure your Web IDE in XSA with an upstream link then you can also fetch module versions from public npm and not be limited to what's delivered in the internal npm cache. https://help.sap.com/viewer/1a8e7ab05a2e4119b02b702f211422f5/2.0.01/en-US/5fd9473b7e994e2aa66092da5a10c75a.html

saket-sahi commented 3 years ago

Client has barred the access to the internet on the XSA server. BASIS needs to download the latest version to web ide cache.

saket-sahi commented 3 years ago

When a specific npm package is uploaded the cache, is it the specific version of the package that is getting loaded or all the versions for the package gets available as well? I thought I was restricted by the package version showing in the XSA server. I changed the package version from the XSA server version to the latest one available in npm and the code started working.