Skip to Content
Previous

Create a Node.js module to expose OData services

By Lsubatin

Create a Node.js module to implement backend logics and expose OData services

You will learn

Create a Node.js module to expose data in an OData service.

Details


Step 1: Create a Node.js module

Create a Node.js module. Right-click on your project and select New->Node.js Module:

Create a Node.js module

Call it js:

Create a Node.js module

Add a description, check the XSJS support box and click on Finish

Create a Node.js module
Please log in to access this content.
Step 2: Create an OData file

You will now create an OData file to expose the contents of the artifacts you defined in your CDS module. This is not really Node.js development but the utilization of the runtime environment to expose OData. You can see real examples of Node.js development in the SHINE model later.

Create a folder called xsodata under js->lib

Create a Node.js module

Create a file called PO.xsodata with the following content:

service {
	"PO.PO_VIEW" as "POHeader"
	keys ("PURCHASEORDERID")
	navigates ("Items" as "POItem");

	"PO.ITEM" as "POItem";
	association "Items" principal "POHeader"("PURCHASEORDERID")
	multiplicity "1" dependent  "POItem"("PURCHASEORDERID") multiplicity "*";

}

Note: You can disregard the warning in the OData definition

Build js module

Save and Build the js module.

Please log in to access this content.
Step 3: Add the dependency between the modules

You have a database module and now you also have a Node.js module. These modules could technically be executed separately, even deployed separately. However, the Node.js module needs data from the database module. You need to add this dependency in the file that keeps them all together, the mta.yaml:

Edit yaml

Add both the HDI container and database modules to the Requires section of the js modules.

Save the file.

Please log in to access this content.
Step 4: Test the service

Run the js module. Click on the URL to open a new tab:

Run the js module

Edit the URL to access the OData service, replace index.xsjs with /xsodata/PO.xsodata:

Run the js module

Add ?$format=json to the end of the URL. Use the results to answer the question below.

What do you see as the response to the call to the OData service?
×

Updated 10/10/2017

Time to Complete

10 Min

Beginner

Tags

Next
Back to top