Skip to Content

Create simple CAP Service with Node.js using the SAP Business Application Studio

Use SAP Business Application Studio to develop a basic CAP Node.js service for Cloud Foundry. You will use only simple JavaScript functions (no database) and learn some basics from the SAP Cloud Application Programming Model (CAP).
You will learn
  • How to create a Dev Space in SAP Business Application Studio
  • How to create a Project using the CAP Project template
  • How to develop a sample service using CAP and Node.js
  • How to run your service locally
tj-66Thomas JentschDecember 19, 2023
Created by
celineaudinsap
March 15, 2023
Contributors
celineaudinsap

Prerequisites

Overview

Find more information on SAP Cloud Application Programming Model Welcome to CAP.

In this tutorial group you will learn all the steps to finally consume a CAP Service in a process of SAP Build Process Automation.

steps

The Use Case

In this series of tutorials, you will implement a CAP Service with very easy functions like converting a String to a number, converting a list of ids to a comma separated String and finally consume the CAP Service API functions in SAP Build Process Automation Process.

  • Step 1
    1. Open SAP Business Application Studio.

    2. Choose Create Dev Space.

      Create DEV Space
    3. Use name demo for your dev space and select kind Full-Stack Application Using Productivity Tools.

      name

      By selecting Full-Stack Application Using Productivity Tools, your dev space comes with several extensions out-of-the-box that you need to develop CAP services.

    4. Choose Create Dev Space.

      create

      The creation of the dev space takes a while. You see that the status for your dev space will change from STARTING to RUNNING.

    5. Once the dev space is running, click the dev space name to open it.

      open

      Opening the dev space and starting the Application Studio takes a while.

    What is a dev space?

  • Step 2
    1. Use the File menu to create a New Project from Template.

      file menu
    2. Choose CAP Project from the templates and choose Start.

      cap project
    3. Enter the CAP Project Details, use name sap-build-cap-sample-library, select Cloud Foundry: MTA Deployment and choose Finish.

      Runtime Node.js is already selected.

      finish
    4. Change the view from Project Explorer to Explorer, choose View > Explorer.

      guidedHome

      The Explorer view will show all files of your project.

  • Step 3
    1. In the Explorer view you will find the file mta.yaml in the root folder of your project sap-build-cap-sample-library.

      project
    2. Check the file in a text editor, right-click > Open With… > Text Editor default.

      mta
    3. In the srv folder, create a new file called service.cds.

    4. In the same folder, create another new file called service.js.

    5. Select the file service.cds and copy the following code into the editor.

      Contains the definition of data types and function headers. Functions and actions will make the API of your service.

      Simple functions like converting a String to an Integer, a list to a CSV String or getting the list of Todos.

      CDS
      Copy
      @Capabilities.BatchSupported : false
      
      service sap_build_cap_sample_library @(path : '/api/v1') {
      
      define type DataString {
          value : String;
      }
      
      define type DataInteger {
          value : Integer;
      }
      
      define type DataNumber {
          value : Double;
      }
      
      define type DataList {
          id : Integer;
          title: String;
          userId: Integer;
          completed: Boolean;
      }
      
      define type DataListArray {
          responseArray : array of DataList;
      }
      
      @Core.Description : 'toInteger'
      function toInteger(value : String) returns DataInteger;
      
      @Core.Description : 'toNumber'
      function toNumber(value : String) returns DataNumber;
      
      @Core.Description : 'toString'
      function toStr(value : Double) returns DataString;
      
      @Core.Description : 'addQuotes'
      function addQuotes(value : String) returns DataString;
      
      @Core.Description : 'listToString'
      action listToString(responseArray : array of DataList, field : String) returns DataString;
      
      @Core.Description : 'get list of dodos'
      function getListOfTodos() returns DataListArray;
      
      }
      
    6. Select the file service.js and copy the following code into the editor.

      The JavaScript implementation for the API of your service.

      JavaScript
      Copy
      module.exports = srv => {
      
          srv.on('toInteger', req => {
              const {value} = req.data;
      
              return { 'value' : parseInt(value) };
          });
      
          srv.on('toNumber', req => {
              const {value} = req.data;
      
              return { 'value' : parseFloat(value) };
          });
      
          srv.on('toStr', req => {
              const {value} = req.data;
      
              return { 'value' : value.toString() };
          });
      
          srv.on('addQuotes', req => {
              const {value} = req.data;
      
              return { 'value' : "'" + value + "'"};
          });
      
          srv.on('listToString', req => {
      
              var values = req.data.responseArray;
              var resultList = [];
              var field = req.data.field;
              if (values) {
                  for (var i = 0; i < values.length; i++) {
                      resultList.push(values[i][field]);
                  }
              }
      
              return { value : resultList.toString() };
          });
      
          srv.on('getListOfTodos', req => {
              return { responseArray : [            {
                  "id": 1,
                  "completed": false,
                  "title": "delectus aut autem",
                  "userId": 1
              },
              {
                  "id": 2,
                  "completed": false,
                  "title": "quis ut nam facilis et officia qui",
                  "userId": 1
              }] }; 
          });
      
      }
      

    What is the Explorer?

  • Step 4

    This step describes how to test the service locally using a file that contains the Service Endpoints.

    1. On the sap-build-cap-sample-library folder, create a new file called test.http.

    2. Select the file test.http and copy the following content into the editor.

      Samples for HTTP requests for the services of your API.

      HTTP
      Copy
      ### call toInteger
      GET http://localhost:4004/api/v1/toInteger(value='20')
      
      ### call toNumber
      GET http://localhost:4004/api/v1/toNumber(value='20.2')
      
      ### call toString
      GET http://localhost:4004/api/v1/toStr(value=20.2)
      
      ### call addQoutes
      GET http://localhost:4004/api/v1/addQuotes(value='abcd')
      
      ### call getListOfTodods
      GET http://localhost:4004/api/v1/getListOfTodos()
      
      ### call listToString
      POST http://localhost:4004/api/v1/listToString
      Content-Type: application/json
      
      {
          "field" : "id",
          "responseArray": [
              {
                  "id": 1,
                  "title": "delectus aut autem",
                  "userId": 1,
                  "completed": false
              },
              {
                  "id": 2,
                  "title": "quis ut nam facilis et officia qui",
                  "userId": 1,
                  "completed": false
              }
          ]
      }     
      
    3. From the Terminal menu, select New Terminal.

      open terminal
    4. On the sap-build-cap-sample-library folder, run the following command in the Terminal window:

      This will start a local server with your service that will allow you to test your API calls.

      Shell / Bash
      Copy
      cds watch
      
    5. Choose Send Request, you will get the response in a separate window.

      send
    6. Choose Ctrl - C to terminate the Server session.

  • Step 5
    1. Select the package.json file and copy and paste the following code after scripts:

      JSON
      Copy
      ,
      "cds": {
      "requires": { "auth": { "kind": "dummy" }}
      }
      
      Update package json
    2. Run the following command in the Terminal window:

      Shell / Bash
      Copy
      npm install
      
    3. Right click on the file mta.yaml and select Build MTA Project.

      Build MTA Project

      The service gets built, wait until the tasks are completed.

    4. Check your API endpoint definition.

      Check in BTP Cockpit, if the API Endpoint of your Cloud Foundry environment is matching the information you used for the script https://api.cf.us10-001.hana.ondemand.com

      endpoint
    5. Under mta_archives, right click on sap-build-cap-sample-library_1.0.0.mtar and select Deploy MTA Archive.

      Deploy MTA Archive

      You will provide your login information to sign in to the Cloud Foundry environment.

    6. Validate the Cloud Foundry Endpoint and enter your username and password.

      In case you are using two factor authentication, you need to append the One-time password code to your password.

      If you select the SSO Passcode authentication method, SAP Business Technology Platform will grant you a temporary authentication code to sign in.

      Cloud Foundry Sign in
    7. Select Cloud Foundry Organization and Space.

      On trial, dev is created as default Cloud Foundry space

      Cloud Foundry Target

      Your service should be available and running in your Cloud Foundry space.

      In the BTP Cockpit you can check if the service is running:

      service started

Back to top