I’ve been playing around with CAP for NodeJS to develop business applications for customers as well as some fun projects. In those apps I also had to add a UI5 app to interact with the service layer of the CAP framework. The UI often comes with the requirement to have an OData service version v2 while the CAP framework provides an OData service v4. For this, SAP provides an “OData v2 adapter proxy” as an npm package. There is already a tutorial on how to implement this into your CAP project: https://developers.sap.com/tutorials/cap-cloudsdk-2-v2-adapter.html
Both will help you perfectly to implement this OData v2 adaptor proxy in your CAP project yourself. Nevertheless, in this blog I want to share how you can set up this OData v2 adapter proxy exactly the same as the original server with all the features of CAP. This will also give you the possibility to run the proxy from the command “cds run” without the need of another npm script to run the proxy.
On top of that, I will also share how I came up with this proxy implementation.
Start creating a CAP project
For starting, create a basic CAP project by following the steps in this tutorial:
If you run “npm install”, npm will install the cds npm package in your project. Normally this is not needed as this is probably installed globally. For working with the proxy, you will need to do this to be able to use the cds module in the proxy.
Original “server.js” implementation
If you look in the node_modules folder (after you run “npm install”), you will find a file “serve.js”
If not yet installed locally, you can look for this file in the global npm package:
When looking at the code in this file you’ll notice the following line:
This line of code will look for a “server.js” file in the npm package itself or for a custom “server.js” implementation in your CAP project. If CAP finds a “server.js” file in your project, it will use your version instead of the default one. The “server.js” needs to call “server.js” and needs to be located in the “srv” folder, otherwise CAP won’t find it.
This means, as soon as you create a “server.js” file in the “srv” folder of your CAP project that “cds run” will use your implementation of “server.js”.
Now, to have a complete version of the “server.js” file and to not start from scratch you can find the original “server.js” version of CAP here:
This can be used as a starting point for your “server.js” version:
Implement your own “server.js”
Before we implement the “server.js” we need to install the proxy by running the following npm command:
npm i @sap/cds-odata-v2-adapter-proxy
(if not yet done, run npm install. This will add the cds npm package to your project which is needed to use it in the “server.js” file)
Create the “server.js” file in the “srv” folder:
Copy the code from the “server.js” file of the CAP framework (see earlier in this blog). This version of the “server.js” does not contain or use the proxy yet. We still do not have a v2 OData service. To achieve this, we need to implement the proxy into this “server.js” code. This can be done by adding the following pieces of code:
- Load the proxy “@sap/cds-odata-v2-adapter-proxy” at the top of the “server.js” file
2. Apply the proxy just before the return statement:
3. This will not yet work. The original “server.js” will try to load the html index page from the lib folder in the node module. If we want to do this in our implementation, the path to this html index page will be different. Next to that, it will also be different when running locally compared when running in SCP. When running the app locally, you start “cds run” from the root level but when it’s deployed the service will have his own modules. This small fix will make the proxy work locally and when deployed to SCP:
You can find the full code on GitHub: https://github.com/lemaiwo/CAP-UI5-APP/blob/master/srv/server.js
As Gregor Wolf mentioned, instead of making a copy of the original “server.js” file you can create a shorter version as described in the readme of the OData v2 adapter proxy:
"use strict"; const cds = require("@sap/cds"); const proxy = require("@sap/cds-odata-v2-adapter-proxy"); cds.on("bootstrap", app => app.use(proxy())); module.exports = cds.server;
That’s it, the OData service will now support v4 and v2!
Run your implementation
This can be tested by simply running the command: “cds run”
The result will be exactly the same result for v2 as v4 version. The original url will still expose OData v4. Adding “/v2/” will provide the OData v2 version:
No need to create a start script for your “server.js” file:
Hope it will help you and enjoy building apps with CAP 🙂