Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Showing results for 
Search instead for 
Did you mean: 
In this blog I will show how you can make use of the route handler to switch between NodeJS and XSJS web services.

Earlier I was using the HANA Classic environment for my developments which made use of XSJS APIs. These days I have been migrating few of the projects to SAP Cloud Foundry environment as the future roadmap by SAP. That’s where I stumbled upon XSA and CF. Use a hybrid approach of using earlier developed xsjs service and developing new services using the express framework. Although there are few blogs and tutorials that help us in developing applications using the new WebIDE approach, most solutions talk about XSA and interestingly I found out it doesn’t work the same way when you are using Cloud Foundry. Since XSA is for on premise not designed nor tested for CF [link].

We are familiar with building XSJS APIs using the SAP Neo Environment. Coming to Cloud Foundry we use the NodeJS with support to XSJS to develop our web APIs in XSJS. But there comes a time when you need to look for a hybrid model, using both NodeJS and XSJS APIs in the same module, perhaps to support previous xsjs implementations.

Here, we will see how we can use the route handler to map the incoming request either as a NodeJS call or an XSJS call.

  1. Create a MTA project

  2. Create NodeJS module with support for XSJS

  3. Build your project. Right click your NodeJS modula and run.

  4. In the server.js paste the below code:
    "use strict";
    var https = require("https");

    var xsenv = require("@sap/xsenv");
    var port = process.env.PORT || 3000;
    var server = require("http").createServer(); xsenv.loadCertificates();

    global.__base = __dirname + "/";
    var init = require(global.__base + "utils/initialize");

    //Initialize Express App
    var app = init.initExpress();

    //Setup Routes
    var router = require("./router")(app, server);

    //Initialize the XSJS Compatibility Layer

    //Start the Server
    server.on("request", app);

    server.listen(port, function() {"HTTP Server: " + server.address().port);


    We are first initializing the express framework using
    var app = init.initExpress();​

    Next, we initialize the router. The routing needs to be initialized before the xsjs initialization.
    var router = require("./router")(app, server);

    Next we initialize the XSJS Compatibility Layer.

    Then Start the Server.
    server.on("request", app);


  5. The initialize.js calls the express app and the xsjs layer. Create the file under utils folder:

    Add the below code.
    /*eslint no-console: 0, no-unused-vars: 0*/
    "use strict";
    module.exports = {
    initExpress: function() {
    var express = require("express");

    //Initialize Express App
    var app = express();

    return app;

    initXSJS: function(app) {
    var xsjs = require("@sap/xsjs");
    var xsenv = require("@sap/xsenv");
    var options = {
    anonymous : true, // remove to authenticate calls
    auditLog : { logToConsole: true }, // Required. change to auditlog service for productive scenarios
    redirectUrl: "/index.xsjs",
    xsApplicationUser: false, //Important
    context: {
    base: global.__base,
    env: process.env,
    answer: 42

    // start server
    var xsjsApp = xsjs(options);

  6. Create router folder and create index.js as:

    Add the below code:
    "use strict";

    module.exports = function(app, server){
    app.use("/node", require("./routes/node")());

  7. Create the routes sub folder and create a node.js file as:

    Add the below code:
    /*eslint no-console: 0, no-unused-vars: 0, no-shadow: 0, new-cap: 0*/
    "use strict";
    var express = require("express");
    module.exports = function () {
    var app = express.Router();

    app.get("/", function (req, res) {
    res.send("Response from Node.js");
    return app;

    We are writing a GET call. When the NodeJS is called we send the sample response.

Testing the NodeJS module:

Run the NodeJS module in the WebIDE. You will receive the server URL in run console. Add index.xsjs to your webservice. The xsjs layer is called:

Now lets test the router for calling NodeJS API by adding /node to the URL:

We have successfully testing the routing between NodeJS and XSJS calls. You can also find further references at:



1 Comment
Labels in this area