Skip to Content
Technical Articles
Author's profile photo Pierre Godart

SAP Cloud Application Programming Model (CAP): simple file upload service using node.js

Description of this post

SAP CAP is a powerful framework but it is not yet well documented and some features are not fully operational. This is not a problem since it relies on Node.js runtime… Indeed, Node.js is the event-driven JavaScript runtime that comes with a *lot* of libraries for everything you need thanks to the huge community of users/programmers.

 

Why a file upload service? Do we really need this? Yes and no.

You can use the media handling functionalities on an entity’s attribute… (CDS definition -> @Core.MediaType annotation) this can be done very quickly but you’ll get basically an upload service that will record the file content in your database…

 

I want to process the content of the file, use it for different entities’ feeding, correct errors, format some fields… this seems far more simple using a dedicated service build on my own via a node.js entry point.

 

Let’s begin…

 

Client side UI5 page elements

User interface:

 

Xml view: to upload a file, the “FileUploader” component is used, with this configuration:

 

Points of attention (that will be used on the server side later):

  • “Name” attribute => “UploadedFile”
  • uploadUrl => “/UploadFile”
  • httpRequestMethod => “POST”

 

Controllers: the most important handlers:

 

 

Server side layer

First let’s create the definition of the service… Since CAP is based on the “express” web framework, we need to add our own version of the “server.js” file in our application directory (“/srv” directory)…

I’ll use two libraries/additional packages to deal with the file upload and the file parsing (the file will be a simple CSV file):

=> In the “server.js” file:

  • const fileUpload    = require(‘express-fileupload’)
  • const Papa          = require(‘papaparse’)

 

Full content of the “server.js” file:

 

 

Point of interest:

Var sKeysOfEntity = Object.keys(BankOperationsBankIng.elements);

=> Extract attributes of the CDS entity to allow a mapping with the CSV file fields… (via “transformHeader” inline function)

 

That’s all!

Thanks for reading,

Kind regards,

Pierre

Assigned tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.