Deploying a Nodejs application on SAP Cloud Platform
In this tutorial you will deploy a simple Node.js application on SAP Cloud Platform. The application uses the Text Analysis Fact Extraction service that is available in YaaS market place as a service. However the developers can replace this with any service of their choice.
You will learn
- Consuming a service (YaaS) in the application
- How to publish and run the Node.js application on SAP Cloud Platform
- How to use backing services like MongoDB service in our application
Prerequisite
SAP Cloud Platform – Cloud Foundry Instance – Trial Instance
YaaS Account and OAuth Registration
Sentiment Analysis Subscription
Installing Nodeclipse
Step 1: Installing Node.js
The first step in getting the Nodeclipse plugin is to install Node.js in the system. You can check for the latest installer from the Node.js downloads. To verify if the Node.js is installed correctly, enter the command ‘node -v’ in the command prompt. This command should display the version of Node.js installed in the system. Check if ‘npm’ is installed as well by issuing the command ‘npm -v’
Step 2: Installing nodeclipse plugin in Eclipse
As a prerequisite for this step, you must have Eclipse installed (Preferably Neon).
- Start Eclipse, then select Help > Install New Software and Enter the update site URL http://www.nodeclipse.org/updates/ into ‘Work with’ textbox and select the following:
Uncheck “Contact all updates site during install to find required software” to make installation quicker. Note that different Eclipse version have different behaviour for that checkbox. Press the Enter key or Next button.
- After a while depending of what you had and what you selected, you should see the center box filled with Eclipse plugins to install. Click Next.
- Review the features that you are about to install. Click Next.
- Read the license agreements, and then select I accept the terms of the license agreements. Click Finish.
- You will then be asked if you would like to restart Eclipse. Click Restart Now.
- Switch to Node perspective (e.g. Window -> Open Perspective -> Other … -> Node).
- Now you are ready to develop Node.js applications with Nodeclipse.
Details
Step 1: Importing and Modifying the Node.js application
The sample code explained in this tutorial is about collecting the participant’s feedback about the various sessions. The application uses the Sentiment Analysis YaaS service to aggregate the sentiments through the comments collection from the participants. The sample Node.js application has been placed in the GitHub [Link] for your reference. You can import the Node.js application into Nodeclipse and perform the modifications to accommodate the YaaS credentials as described below.
Once the application is imported, the Project Explorer will display the structure as follows:
Project Description:
- The project follows the Express framework and uses jade as HTML rendering engine. The package.json file which lists the dependencies to run the application shows the same.
The package file explains the start and test script. The package includes ‘mocha’ which is the unit test tool that is being used in this project for Node.js. These start and test scripts can be used in Travis CI if you intend to implement Continuous Integration.
- All the configurations with respect to the mongo DB (URL, User ID, password) and YaaS service (Client ID and secret for OAuth authorization) are maintained in the config.json under config folder to ease the configurations in various environments.
Replace all the placeholders (<<>>) with the actual values from the system set up. The same is explained in Step 2 [Link]. The “use_yaas” field is used for advanced topic of consuming the YaaS Sentiment analysis services.
- All the services are consumed in the JavaScript files placed under services folder. The Sentiment Analysis service under the Text Analysis Fact Extraction subscription in YaaS is consumed in analysis_service.js file and the authorization for the same through OAuth tokens is executed as part of oAuth.js file. These files will be executed when the “use_yaas” field is set to “true” in the config file.
- The models folder contains the MongoDB collection details for storing the OAuth token and ratings.
- The stylesheets to ensure that the same format is followed across the pages is maintained under the public folder.
- The manifest.yml file describes the application name and the services that are to be bound to the application at the time of deployment.
Step 2: Registering for the services and Configuring the project
- Logon to the SAP Cloud Platform instance to create a backing service. To do so, the first task is to set the API endpoint to the cloud controller of the SAP Cloud Platform instance. Using the ‘cf api’ command. Ensure the proxy settings are done to be successfully able to set the API.
The success message would be like the one shown below:
- Login to the SAP Cloud Platform Cloud Foundry instance using the command ‘cf login’ by providing the user credentials.
Note that some of the instances would consider the Email ID instead of User ID.
Once logged in successfully, you should be able to create the service instance and deploy the application successfully.
- The next step is to create a service instance to be bound to the application for persistency. The service instance for MongoDB service can be created using the ‘cf create-service’ command.
Note that ‘MyMongo’ is the name of the Service instance that is being created to be bound to the Node.js application.
- To get the details on accessing the ‘MyMongo’ service instance, navigate to the SAP Cloud Platform Cockpit and the Service Instance and click on the Dashboard icon as shown below.
The dashboard provides the details to access the service instance from the application.
Note the MONGODB_USERNAME, MONGODB_PASSWORD and MONGODB_DBNAME along with the IP and host to be replaced in the config.json file as mentioned in the Project description.
- You could also use the VCAP services to connect to the Database instead of manually entering the user credentials. The same can be provided as part of the app.js file.
var vcap_services = JSON.parse(process.env.VCAP_SERVICES);
mongo_url = vcap_services.mongodb[0].credentials.uri;
var db_options = {
pass: vcap_services.mongodb[0].credentials.password
};
Step 3: Deploying the application on SAP Cloud Platform
In this step, you will deploy the application on SAP Cloud Platform and the deployment will automatically take care of binding the application and the service instance with the help of manifest.yml file as described in the Project Details section.
- The deployment of the application can be done in the command prompt through the Cloud Foundry CLI command. Navigate to the project root directory and deploy the application through ‘cf push’ command.
Note that the CF CLI uses the manifest file to understand the services to be bound.
The application can be launched with the URL mentioned in the success message.
- The other way to view the application detail is through the SAP Cloud Platform Cockpit by navigating to the Applications pave and selecting the application.
- Clicking on the application provides the details about the application that you deployed.
Click on the application routes URL to launch the application. The application launches as shown below:
Advanced: Consuming YaaS Services
As described in the previous step, the application consumes the YaaS services for analysis of the sentiments based on the comments received from the participants.
- Enable the application to consume the YaaS service by configuring the same in config.json
- The first step is to register in YaaS using the link. Once registered, you will be able to subscribe to the YaaS services and also create projects to access the subscribed services.
- Proceed to the YaaS builder to create organization and projects in the link. Create a new Organization by providing the name and YaaS URL.
- Create a new project and provide the Display Name, Identifier and description as shown below. And save the inputs.
- Subscribe to packages through the link. For this application, we will be consuming the services from Text Analysis Fact Extraction. Search for the same or proceed to the link and subscribe the same.
- Select the newly created project in the pop up that appears and click ‘Subscribe Now’. Upon successful subscription, you will receive a mail (that is registered in the YaaS account) containing the details of the package.
- Navigate to the project and create a new client under ‘Clients’. The client is used to maintain the scopes and credentials to access the subscribed packages. Select the necessary scopes (hybris.org_manage, hybris.org_project_create, etc.) and select next to create the identifiers.
- Navigate to the created client by clicking on ‘Clients’ and selecting the client to access the generated credentials under ‘Client Authorization’. Click ‘Show’ to see the Client ID and Client Secret.
- Copy the generated client ID and client secret and replace the placeholders in config.json described in the previous step under ‘client_id’ and ‘client_secret’.
Is Eclipse actually required for node.js development on the SAP Cloud Platform? I'm not seeing why users couldn't develop their app using any IDE?
Lee
Not necessarily as you suspect. I have only used eclipse as an IDE and it has a plugin that can help in deploying the application to SAP Cloud Platform. Nothing more special there.
Honestly: Imho you can use editors like Visual Code a/o Atom
Great Article. Thank you so much for letting us know about how to deploy a Nodejs application on SAP Cloud Platform. Keep Going.
Great blog, Hariprasauth
Some versions are not really compatible anymore, so I did a bit tweaking of the dependencies.
This seems to work:
"dependencies": {
"body-parser": "1.13.1",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"express": "4.12.4",
"jade": "~1.11.0",
"morgan": "~1.7.0",
"serve-favicon": "~2.3.0",
"mongodb": "latest",
"mongoose": "latest",
"https": "latest",
"express-validator": "2.12.1",
"request":"latest",
"deasync":"latest",
"moment":"latest",
"util":"latest",
"should":"latest",
"supertest":"latest",
"mocha": "latest",
"passport":"latest"
}, "engines" : {
"node" : "4.8.5",
"npm" : "3.0.0"
}
All the best,
@simenhuuse
Thank you.. 🙂
Hi Hariprasauth
Is Node JS available in Full Stack Web IDE ?
Hi GB,
Not yet! There is something coming up! 🙂
Hey G B
NodeJS development is available on the SAP Cloud Platform WebIDE Full-Stack. As Marius Obert shared a few weeks ago:
https://twitter.com/IObert_/status/1016348668893548544