Technical Articles
Use CloudSDK for http request in NodeJS on SCP CF
Introduction
In earlier blog posts I explained how to use the destination service in a NodeJS application to get data from an external system:
https://blogs.sap.com/2020/05/28/use-the-destination-service-in-nodejs-locally/
In this example I used the npm package “request-promise” which is in the meanwhile deprecated: https://www.npmjs.com/package/request-promise . I also added an example how to do this with Axios-CF. Besides my examples, there are other ways to do the same. For example, we also have the library from Volker Buzek “sap-cf-destination” : https://blogs.sap.com/2018/11/21/scp-cloud-foundry-destinations-demystified/
There is even a library that could be used for this as well, which is even closer to us then we are aware of. At least, I was not aware of and may be some of you as well. SAP provides us a function to do this as part of the SAP Cloud SDK.
The Cloud SDK is a set of tools and libraries for consuming, building or extending SAP services and applications in the cloud-native way and deploying them to SAP Cloud Platform. More about the Cloud SDK:
https://sap.github.io/cloud-sdk/docs/overview/about
You can use everything of the Cloud SDK for building apps or you could consider to build an app your own way and use some parts of the Cloud SDK.
I’m not sure many devs are aware that the Cloud SDK contains many other functions:
https://sap.github.io/cloud-sdk/api/1.30.0/modules/sap_cloud_sdk_core
One of the many functions provided by Cloud SDK is “executeHttpRequest”. This function can be used to send a http request in combination with a destination.
The benefit of this function as part of the Cloud SDK => it’s being maintained by SAP and supports all possible destination in SCP.
In this blog I want to create awareness of this functionality and shortly show how it can be used in a NodeJS app.
It is also mentioned in this blog post as part of new SAP Cloud SDK release: https://blogs.sap.com/2019/07/18/new-versions-of-sap-cloud-sdk-2.19.1-for-java-1.6.1-for-javascript-and-v22-of-continuous-delivery-toolkit/
How to use
As example I will use the “executeHttpRequest” as part of an ExpressJS app.
The function “executeHttpRequest” is part of the “core” module of the Cloud SDK. It is enough to only install this module. Start by installing the npm package “@sap-cloud-sdk/core”:
npm i @sap-cloud-sdk/core
You can find more in the npm documentation: https://www.npmjs.com/package/@sap-cloud-sdk/core
Start your script by loading the Cloud SDK core:
const core = require('@sap-cloud-sdk/core');
Now you are able to use it in your ExpressJS script by passing the name of the destination as an object and the http config as a second object:
let response = await core.executeHttpRequest({ destinationName: sDestinationName}, {
method: 'GET',
url: "/service/endpoint"
});
res.send(response.data);
You can also consider to separate the “getDestination” function:
const dest = await core.getDestination(sDestinationName);
And pass the destination as the first object now:
let response = await core.executeHttpRequest(dest, {
method: 'GET',
url: "/service/endpoint"
});
The result will be in the property data of the response for both cases:
res.send(response.data);
Full script:
const express = require('express');
const core = require('@sap-cloud-sdk/core');
const xsenv = require('@sap/xsenv');
xsenv.loadEnv();
const app = express()
const port = process.env.PORT || 3000;
const sDestinationName = 'destination-name';
app.get('/data', async (req, res) => {
// const dest = await core.getDestination(sDestinationName);
let response = await core.executeHttpRequest({ destinationName: sDestinationName}, {
method: 'GET',
url: "/service/endpoint"
});
res.send(response.data);
});
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Resources
You can find my example project on GitHub:
https://github.com/lemaiwo/SCP-CF-NodeJS-Example/tree/CloudSDK
This blog post is short but nevertheless valuable in case you want to work with destinations in NodeJS. No need for any third-party library which won’t guarantee long term support like SAP does ?
Kr, Wouter
Hi Wouter,
Great to see that we can do this just by just installing the core package.
I managed to use @sap/destinations yesterday which is very similar in usage and compactness, but as it's unclear if that package will remain updated I am switching to this method instead.
Thanks for sharing!
Hi Pieter,
can you raise an incident via SAP Support and ask SAP for a statement regarding @sap/destinations?
Best regards
Gregor
SAP statement:
Have you asked them how it will be integrated into CAP and SAP Cloud SDK?
Pieter Janssens , Gregor Wolf
At the times when SDK was conceived nothing related to connectivity was in place. We've built quite good abstractions for CF to provide a seamless client experience.
We also comprehensively document and support it.
We are happy to collaborate with libraries that solve some use-cases end-to-end and have flexible API and guaranteed support.
We'll discuss the feasibility within the team.
supercool ? sample code for raising Cloud SDK awareness!
about time to retire sap-cf-destination soon, me thinks...
Keep it alive! It’s good to have options and inspire SAP
What should I replace "service/endpoint" with ?