Creating a CAP Project

Objective

After completing this lesson, you will be able to generate a SAP Cloud Application Programming Model project

Working with SAP Business Application Studio

Dev Spaces and Project Wizard

As mentioned earlier, we want to use SAP Business Application Studio (BAS) as development environment to create CAP applications for the Node.js runtime. BAS provides the benefit that everything we need for development is available out-of-the-box. For example, Node.js as well as CAP's CDS development kit is already installed ready to use.

Watch the video to learn about creating dev spaces and generating CAP projects in order to develop applications using BAS.

Command Line Interface

CAP projects can also be created with the npm package @sap/cds-dk. This package contains the command line client and development toolkit for CAP.

@sap/cds-dk is available out-of-the-box in BAS. Corresponding commands can be entered directly in a terminal in BAS. To open a new terminal, select TerminalNew Terminal from the hamburger menu in the upper left corner of BAS.

@sap/cds-dk provides commands for various stages of CAP development, from initializing new projects to deploying them to the cloud. Use cds help to get an overview of all commands. Use cds help <command> or cds <command? to get specific help. With cds init a new CAP project can be generated.

Convention Over Configuration

Minimal Configuration

CAP has default settings for many things that you would need to configure in other frameworks. The goal is to make things work without configuration as much as possible. If needed, you can override these default settings with specific configuration. For example, you could override the default names for the above folders in the package.json file as follows:

Code Snippet
123456789
{ ... "cds": { "folders": { "db": "database/", "srv": "services/", "app": "uis/" } } }

Note

We recommend that you follow CAP's conventions to benefit from things just working out of the box. Only add configurations or override the defaults if you really need to do so.

Hint

You can use the cds env command to display the effective configuration for a specific key or the entire current environment in the terminal. For example, if you execute cds env folders in the terminal, you get the following output if you have not overwritten the default folder names:
Code snippet
{ db: 'db/', srv: 'srv/', app: 'app/' }
Expand

Testing Locally

When you execute the cds watch command in the terminal, you start an http server in watch mode. As a result, your project is compiled and served locally. This means that your CAP-based application can be accessed and tested from BAS (or VS Code) using the provided server (refer to the figure Starting an HTTP Server).

The "watch" in cds watch indicates that the command continuously monitors the project's files for changes. Once a change is detected in the source files, the server automatically restarts to reflect those changes. It is not necessary to manually stop and restart the server after changes.

While the server is running in watch mode, it outputs logs to the console. These logs can be helpful to detect problems, understand the flow of requests and troubleshoot errors.

Note

Actually, cds watch is just a convenient shortcut for:
Code snippet
cds serve all --with-mocks --in-memory?
Expand

Run cds serve ? in the terminal to get more information about cds serve and the available options.

The --in-memory? option automatically adds a transient in-memory database, which will be bootstrapped at each (re)start based on the default settings or configuration in package.json. The added question mark applies a more defensive variant that respects the configured database, if any, and only adds an in-memory database if no persistent database is configured.

When cds watch is running, you can open http://localhost:4004 in a browser to see a generic index.html page. This page is mainly for development and testing purposes. In production scenarios, it is usually not exposed or replaced by a proper user interface.

The page provides a list of all exposed service endpoints. This makes it easy for developers and testers to access them directly.

For each OData service, there is also a link to its $metadata document. Clicking on it will show you the metadata for the service, which describes the entity sets, types, and other details of the service in XML format.

For Node.js applications, there is also a Fiori preview link on the index.html page for each exposed entity set. Such a link dynamically serves a SAP Fiori elements list page based on the corresponding entity set. Please note that this is not intended for production use. It is not supposed to be a replacement for a proper SAP Fiori elements application.

Hint

To terminate the server process, press Ctrl+C in the terminal where your application is running.

Demonstration & Exercise: Create a Hello World Application

Note

As exercise, carry out the step-by-step instructions in the following demonstration yourself in the SAP Business Application Studio.

You can find the source code from the simulation in branch 1_hello_world of the following GitHub repository:

https://github.com/SAP-samples/cap-development-learning-journey

Detailed information on the content of this repository and how to use it can be found here.

Watch the video to see how to create a Hello World application.

Now that we know how to create CAP projects in a dev space and how to test CAP applications locally, let's explore basic CAP features.

Log in to track your progress & complete quizzes