Posted on

#3 Hello World Service in SAP® CAPM – 7 Steps to Get Started With SAP® CAPM

Before we begin!

In this 7 blog series, we are going to provide a small end-to-end scenario to work with SAP® CAPM. Here the usecase we are going to use is of Learning Management System(LMS) and build a Student List App and Deploy it to SCF.

Link to Master(or Main) Blog from which all the other blogs can be reached is Here.

  • If you wanted to check out the detailed course on SAP® CAPM the link is Here.
  • If you want to get learn SAP® Cloud Platform Development in Detailed then we have a 35hrs hands-on course Here.
  • For current offers and discounts check offer page Here.

In this section we will be creating and running a simple CAPM Service, so lets start!

 

 

 

 

Step 1: Create a New CAPM Project

      cds init <ProjectName>

This will create a Blank project of capm.

You will see 3 main content in the folder:

  • package.json
  • package-lock.json
  • node_modules

On going inside the node_modules folder you will see list of individual modules which got downloaded

    root@apples-MacBook-Air:~/Desktop/Blog/capm1/demo1/demop/node_modules$ ls -l
total 0
drwxr-xr-x   3 root  staff  102 Nov 21 15:20 @sap
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 accepts
drwxr-xr-x   6 root  staff  204 Nov 21 15:20 array-flatten
drwxr-xr-x   8 root  staff  272 Nov 21 15:20 body-parser
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 bytes
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 content-disposition
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 content-type
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 cookie
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 cookie-signature
drwxr-xr-x  15 root  staff  510 Nov 21 15:20 debug
drwxr-xr-x   8 root  staff  272 Nov 21 15:20 depd
drwxr-xr-x   6 root  staff  204 Nov 21 15:20 destroy
drwxr-xr-x   6 root  staff  204 Nov 21 15:20 ee-first
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 encodeurl
drwxr-xr-x   6 root  staff  204 Nov 21 15:20 escape-html
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 etag
drwxr-xr-x   8 root  staff  272 Nov 21 15:20 express
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 finalhandler
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 forwarded
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 fresh
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 http-errors
drwxr-xr-x   8 root  staff  272 Nov 21 15:20 iconv-lite
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 inherits
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 ipaddr.js
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 media-typer
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 merge-descriptors
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 methods
drwxr-xr-x  11 root  staff  374 Nov 21 15:20 mime
drwxr-xr-x   8 root  staff  272 Nov 21 15:20 mime-db
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 mime-types
drwxr-xr-x   6 root  staff  204 Nov 21 15:20 ms
drwxr-xr-x   8 root  staff  272 Nov 21 15:20 negotiator
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 on-finished
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 parseurl
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 path-to-regexp
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 proxy-addr
drwxr-xr-x  12 root  staff  408 Nov 21 15:20 qs
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 range-parser
drwxr-xr-x   8 root  staff  272 Nov 21 15:20 raw-body
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 safe-buffer
drwxr-xr-x   9 root  staff  306 Nov 21 15:20 safer-buffer
drwxr-xr-x   8 root  staff  272 Nov 21 15:20 send
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 serve-static
drwxr-xr-x   8 root  staff  272 Nov 21 15:20 setprototypeof
drwxr-xr-x   8 root  staff  272 Nov 21 15:20 statuses
drwxr-xr-x   6 root  staff  204 Nov 21 15:20 toidentifier
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 type-is
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 unpipe
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 utils-merge
drwxr-xr-x   7 root  staff  238 Nov 21 15:20 vary

Step 2: Content of package.json

If you go inside the package.json, you will see two dependencies

  • @sap/cds
  • express
{
    "name": "demop",
    "version": "1.0.0",
    "description": "Generated by cds init",
    "repository": "<Add your repository here>",
    "license": "ISC",
    "dependencies": {
        "@sap/cds": "^3.18.4",
        "express": "^4.17.1"
    },
    "engines": {
        "node": "^8.9"
    },
    "scripts": {
        "build": "cds build/all --clean",
        "deploy": "cds deploy",
        "start": "cds run"
    }
}

Step 3: server.js of @sap/cds

@sap/cds is the module which is inside folder node_modules/@sap/cds.
Here you will find server.js which is actually the file which is run when you execute
a CAP Project with cds run command.

 

Content inside node_modules/@sap/cds

    root@apples-MacBook-Air:~/Desktop/Blog/capm1/demo1/demop/node_modules/@sap/cds$ ls -l
    total 928
    -rw-r--r--   1 Ajay  staff   25104 Nov 15 21:06 CHANGELOG.md
    -rw-r--r--   1 Ajay  staff     746 Nov 15 21:06 README.md
    -rw-r--r--   1 Ajay  staff  396160 Nov 18 12:48 SIGNATURE.SMF
    drwxr-xr-x   5 Ajay  staff     170 Nov 21 15:20 _i18n
    drwxr-xr-x  10 Ajay  staff     340 Nov 21 15:20 apis
    drwxr-xr-x  20 Ajay  staff     680 Nov 21 15:20 bin
    -rw-r--r--   1 Ajay  staff    3619 Nov 15 21:06 common.cds
    -rw-r--r--   1 Ajay  staff   12591 Feb 28  2017 developer-license-3.1.txt
    drwxr-xr-x   3 Ajay  staff     102 Nov 21 15:20 etc
    drwxr-xr-x  12 Ajay  staff     408 Nov 21 15:20 lib
    drwxr-xr-x  53 root  staff    1802 Nov 21 15:20 node_modules
    -rw-r--r--   1 Ajay  staff    7293 Nov 15 21:06 npm-shrinkwrap.json
    -rw-r--r--   1 Ajay  staff    9574 Nov 21 15:20 package.json
    -rw-r--r--   1 Ajay  staff    1517 Nov 15 21:06 server.js

Step 4: Create a simple Service

Create srv

First, we create a folder with name srv inside the project directory

     mkdir srv

Create js

then we create a file mySimpleService.js which is going to contain our service

    touch mySimpleService.js

Implement js

then, we create a module which holds our service, in simple js we can write as


    const exportSRV = function(srv) {
    srv.on("somesrv", req => {
            return "Hello " + req.data.msg;
        });
    };

    module.exports = exportSRV;

the same code we can write using ES6 and arrow notation as

    //this code is same as above but with arrow(=>) function
    module.exports = srv => {
    srv.on("somesrv", req => {
            return "Hello " + req.data.msg;
        });
    };

Create cds service

Create a new file with name mySimpleService.cds in same folder of srv.
Here the name of the cds file can be different from js file but to keep
track of our service we have named it same

    touch mySimpleService.cds

Add Service as function

then, we will add same service name which we exported in above js file

    service exportSRV {
        function somesrv (msg:String) returns String;
    }

Run project

So, far we have two files one js and one cds. Now we come to root of project and
run it with command cds run

    cds run

You will see a message in console that server is running in port 4004

    apples-MacBook-Air:/Users/apple/Desktop/Blog/camp2/demop# cds run

[cds] - serving exportSRV at /exportsrv - with impl: srv/mySimpleService.js
[cds] - service definitions loaded from:

  srv/mySimpleService.cds

[cds] - launched in: 1949.628ms
[cds] - server listening on http://localhost:4004 ...
[ terminate with ^C ]

Open the browser and go to url

http://localhost:4004/exportsrv/somesrv(msg='world')

You will get below output

{
@odata.context: "$metadata#Edm.String",
@odata.metadataEtag: "W/"wF4SeVv7xAHyDG7O8zaA1wEHxkfG89tmiLrYI88n37c="",
value: "Hello world"
}

🙂 Congratulations! You have now a Simple CAP Service Working.


Before we end!

In this 7 blog series, we are going to provide a small end-to-end scenario to work with SAP® CAPM. Here the usecase we are going to use is of Learning Management System(LMS).

Link to Master(or Main) Blog from which all the other blogs can be reached is Here.

  • If you wanted to check out the detailed course on SAP® CAPM the link is Here.
  • If you want to get learn SAP® Cloud Platform Development in Detailed then we have a 35hrs hands-on course Here.
  • For current offers and discounts check offer page Here.