Skip to Content
Technical Articles
Author's profile photo Wouter Lemaire

How to add OData v2 adapter proxy to your CAP project

Hi there,

 

Introduction

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

 

Beside this tutorial, Gregor Wolf  has a great example project on GitHub on how to implement this as well: https://github.com/gregorwolf/bookshop-demo

 

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:

https://cap.cloud.sap/docs/get-started/in-a-nutshell

 

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:

%appdata%\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds

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:

  1. 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

—- Update

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:

https://gregorwolf.github.io/SAP-NPM-API-collection/apis/cds-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 🙂

Assigned Tags

      16 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Gregor Wolf
      Gregor Wolf

      Hi Wouter,

      do you know the README.md of the @sap/cds-odata-v2-adapter-proxy? It does a good job to describe how the proxy is used.

      Best regards
      Gregor

      Author's profile photo Wouter Lemaire
      Wouter Lemaire
      Blog Post Author

      Thank you Gregor Wolf for mentioning, it indeed explains some more details of the proxy! I have seen the readme from the npm package of sap but was not aware of nice SAP NPM explorer 🙂

      Author's profile photo Leonel Eduardo Rojas Aranda
      Leonel Eduardo Rojas Aranda

      Hi team,

      I have a question,

      With this proxy, can I use all CRUD requests with odata v2?
      I understand yes, but I want to remove that doubt.

       

      Regards,

       

      Author's profile photo Balbino Soares Ferreira Filho
      Balbino Soares Ferreira Filho

      Yes, the goal of the ODava-v2-adapter is permitted to use the CAP application, which originally only supports OData v4 to work with OData v2. For example to use for SAP Cloud SDK to Extend S/4HANA.

      Author's profile photo sangeetha kanagaraj
      sangeetha kanagaraj

      Hi,

      I am trying to consume odata service v2 and followed your blog. Initially it was working fine. But after 3 to 4 run,  I am getting the Internal server error occured in browser level. In console getting error as "ERROR: request to http://localhost:0/catalog/$metadata failed, reason: connect ECONNREFUSED 127.0.0.1"

       

      Thanks in advance

      Sangeetha

      Author's profile photo Ahmed Ali Khan
      Ahmed Ali Khan

      I am getting this when trying the same server.js file from your GitHub, Can you please help

      user: sap-ibp-promotions $ cds run
      [cds] - Loading server from { file: './srv/server.js' }
      [ERROR] Cannot find module '../node_modules/@sap/cds/lib/utils/app/index_html'
      Require stack:
      - /home/user/projects/sap-ibp-promotions/srv/server.js
      - /home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/bin/serve.js
      - /home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/bin/run.js
      - /home/user/.node_modules_global/lib/node_modules/@sap/cds-dk/bin/cds.js
      at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
      at Function.Module._load (internal/modules/cjs/loader.js:745:27)
      at Module.require (internal/modules/cjs/loader.js:961:19)
      at require (internal/modules/cjs/helpers.js:92:18)
      at Object.<anonymous> (/home/user/projects/sap-ibp-promotions/srv/server.js:41:26)
      at Module._compile (internal/modules/cjs/loader.js:1072:14)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
      at Module.load (internal/modules/cjs/loader.js:937:32)
      at Function.Module._load (internal/modules/cjs/loader.js:778:12)
      at Module.require (internal/modules/cjs/loader.js:961:19)

      Author's profile photo Lena Scholz
      Lena Scholz

      Hey Ahmed,

      I get the same error message. Have you found a solution to your problem?

      Kind regards,

      Lena

      Author's profile photo Lena Scholz
      Lena Scholz

      Just in case anyone encounters this problem again: There is a solution in CAP's documentation that fixes the error for now.

      Since CAP is still new territory for me, I can't say at this point whether this is a really lasting solution...

       

      Author's profile photo Ahmed Ali Khan
      Ahmed Ali Khan

      I use only this code in my server.js, And it get resolved, Hope it helps you as well

      "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;
      Author's profile photo Former Member
      Former Member

      Hi, I am able to convert the OData service from v4 to v2 in BAS local.

      How I can use the v2 service after the CAPM service is deployed with the same proxy setting?

      Thanks

      Author's profile photo Ahmed Ali Khan
      Ahmed Ali Khan

      Add the dependencies in package.json file

       "@sap/cds-odata-v2-adapter-proxy": "^1.8.5",
      Author's profile photo Ahmed Ali Khan
      Ahmed Ali Khan

      The easiest way of creating and deploying the app with v2 enabled is in 3 steps below.

      1. In your srv folder create a "server.js" file and add this code
        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;
      2. Open your terminal and run "npm i @sap/cds-odata-v2-adapter-proxy"
      3. Add the dependencies in package.json file
         "@sap/cds-odata-v2-adapter-proxy": "^1.8.5",

         

      Author's profile photo Niklas Ullrich
      Niklas Ullrich

      Hello, i tried to use the v2-proxy-adapter, but it doesn't work or don't know how to call the v2 service.

      i add the server.js in the srv-folder:

      "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;
      and add the required package @sap/cds-odata-adapter-proxy in my package.json
      when i call "cds run" i select the http://localhost:4004
      my app opened in another tap with the url: https://workspaces-ws-xdgqr-app2.us10.trial.applicationstudio.cloud.sap/
      so how can i extend the url with "v2"?
      Author's profile photo Saranya Sampath
      Saranya Sampath

      Hi,

      I have implemented odata v2 adapter in my cap project. cds run app was working fine. But while deploying I am getting below error

       

       

      My root package.json having dependencies also.

      "dependencies": {
          "@sap/cds": "^5",
          "@sap/cds-odata-v2-adapter-proxy": "^1.8.14",
          "@sap/hana-client": "^2.12.20",
          "express": "^4",
          "hdb": "^0.18.3"
        },
      Thanks and regards
      Saranya
      Author's profile photo Daniel Camacho
      Daniel Camacho

      Hi I'm facing the same error, everything is working in the local version:

      But when I deploy:
      I get the next result in the $metadata v2:

      Author's profile photo Jenifer Ranjani
      Jenifer Ranjani

      Hi Wouter Lemaire

       

      Thanks for the blog 🙂

      We are currently moving the app from webide full stack to BAS.. I'm facing an issue in the below line, I was comparing the source code from your github and it was the same.

      The same piece of code runs fine in Webide but it fails, post the migration. Let me know if I have missed anything.

      This is my file server.js.

      Thanks,

      Jenifer