2

I am trying to create a standard SAP CAP (Cloud Platform Application Model) in SAP Business Application Studio and also extending it with additional Express endpoints besides the ones served by CDS services. I have the following project structure:

Project structure

Content of package.json:

{
"name": "<app-name>",
"version": "1.0.0",
"description": "A simple CAP project.",
"repository": "<Add your repository here>",
"license": "UNLICENSED",
"private": true,
"dependencies": {
    "@sap/cds": "^3",
    "@sap/cds-dk": "^1.8.5",
    "@sap/hana-client": "^2.4.177",
    "express": "^4",
    "multer": "^1.4.2"
},
"scripts": {
    "start": "npx cds run"
},
"cds": {
    "hana": {
        "deploy-format": "hdbtable"
    },
    "requires": {
        "db": {
            "kind": "sql"
        }
    }
},
"devDependencies": {
    "sqlite3": "^4.2.0"
}}

Server.js:

module.exports = async() => {
const express = require('express')
const cds = require('@sap/cds')

const app = express()
const port = process.env.port || 4004

app.use('/', express.static('app/'))
app.get('/', (req, res) => res.redirect("/app/index.html"))

cds.connect("db")
.serve("all")
.in(app)

return app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))}

When I run cds watch command the application properly starts, however when I open the Fiori Elements application the following error message appears:

Error message in Fiori App

If I remove the server.js file from the srv folder, the application works perfectly. What could cause the error message? Is there any additional configuration required?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
  • do you use HANA or SQLite? Do you experience this error in Development or in Production? The context of `Server.js` differs between environments – Suncatcher Jul 01 '20 at 09:45
  • So far I've only run it in Development with sqlite in-memory database. What are the differences in server.js between development and production? – Gombos Levente Jul 01 '20 at 09:51

1 Answers1

1

Answering your question about the difference, you use standard data source declaration:

"cds": {
  "requires": {
    "db": {
      "kind": "sql"
    }
  }
}

As we know from the help:

kind:sql declares the requirement for an SQL database. It evaluates to sqlite in the development profile (active by default), while in production it equals hana. This way you don’t need to modify this file if you want to switch between the two databases

It is important to understand this difference. So what is that particular problem you faced?

With specification "db": {"kind": "sql"} without any attributes, the IDE created your project in SQLite in-memory DB, so it doesn't exist anywhere except application server memory.

Also you specified serve("all") in server.json which constructs all service data model definitions and most probably some of them seems to be broken and/or some other settings in server.json. What I propose is:

  1. To declare datasource explicitly and remove "hana": {"deploy-format": "hdbtable"}

    "cds": {
       "requires": {
           "db": {
             "kind": "sql",
              "credentials": {
               "database": "db/source.db"
           }
         }
       }
    }
    

after cds deploy this will create SQLite database in db folder, so you can check the schema.

  1. Define your server.js in the most simplistic way for the first time:

    const app = require('express')()
    cds.serve('all').in(app)
    app.listen()
    

cds.connect("db") is the same as cds.connect() in your case, and the same as lack of connect() at all. Yes, you can freely omit connect command with default DS.

  1. If this server.js fails, then you should check every service separately and find the error

    cds.serve('./srv/cat-service1.cds')
    cds.serve('./srv/cat-service2.cds')
    cds.serve('./srv/cat-service3.cds')
    ...
    

If serve("all") goes fine, then add other server.js directives one by one: app.use, app.get and so on.

There is definitely some inconsistency between services and server conf but it is hard to determine the exact one without seeing all definitions.

Other helpful commands for debugging the error:

  • this command mocks all your registered service from package.json and allows isolating schema errors

    const cds = require("@sap/cds");
    cds.exec("run", "--with-mocks", "--in-memory?");
    
  • this command adds table and views for all services automatically

    cds deploy --with-mocks
    
  • mocking individual service

    cds serve --mocked
    
Suncatcher
  • 10,355
  • 10
  • 52
  • 90