Create CloudFoundry app with MongoDB and Express (NodeJS) in SAP Web IDE
In addition to my previous blog, about deploying UI5 apps to CloudFoundry from the SAP Web IDE, I’ll show you how you can include MongoDB and Express (running on NodeJS) to the same app.
Previous blog: https://blogs.sap.com/2017/09/22/deploy-your-app-to-cloudfoundry-from-the-sap-web-ide-cp-trial/
In the current version of the SAP Web IDE trial account, there is no an option for adding NodeJS modules. The options described in this blog https://blogs.sap.com/2017/05/11/develop-a-full-stack-application-for-cf-using-sap-web-ide/ are not yet on the trial accounts available…
Although it isn’t available, it’s possible on CloudFoundry for trial accounts. Not sure if this is the best way, but this is how I made it work.
Start adding mongodb as a requirement of the html5 module in the mta.yaml file.
Open the package.json file in the html5 module
Add express to the dependencies and change the start script to start the app from “server.js”.
Express is a Node.js framework for web apps, read more on https://expressjs.com/
Add a file “server.js” to the html5 module.
In this file, we’ll use Express to access and expose mongodb. Server.js will also start the UI5 app by calling the folder webapp.
In the “server.js” file I define routes by using Express. Depending on the route, I’ll read and return the requested items from mongodb. The function to get the items from mongodb
I also define a path for a post request to create an item in mongodb
In the controller of our UI5 view I use ajax to fetch items from Express. Just using jQuery ajax for the example, other solutions are possible.
And an AJAX POST to create an item
Follow the same steps from my previous blog to deploy the app: https://blogs.sap.com/2017/09/22/deploy-your-app-to-cloudfoundry-from-the-sap-web-ide-cp-trial/
The result will look like this:
I used the CloudFoundry handson from UI5con for the code of Express and mongodb which you can find here:
You can find the complete code of this blog on github: https://github.com/lemaiwo/WebIDECloudFoundryApp
Start having fun creating Cloud Foundry apps in the SAP Web IDE!
I have followed the above steps , successfully built and deployed to cloud foundry.
When I run the app , i get the 502 Bad gateway error for the GET and POST services - that is for '/item' and '/items'.
Please find the screenshot below and help to solve this. thank you.
Can you check if mongodb is running?
I have a similar issue. The status of mangodb is set to started.
Hi Wouter Lemaire and Vishnu Vardhan Tanguturu,
have you found the solution for the problem of 502 bad gateway? I have the same problem here
and really want to fix it.
This is the app log I get. Can the db.collection function be the reason?