Technical Articles
How to deploy Node.js application into SAP Cloud Platform CF
Hi All,
I am writing this blog to describe some easy steps to deploy a Node.js application into the SAP cloud foundry environment.
It is the first time for me to setup Node.js environment, so there may be some mistakes on this article.
Environment
Local PC
- Ubuntu18.04.01 LTS on VMWare Workstation
- cf CLI 6.43.0
- node.js 11.10.1
- npm 6.7.0
Cloud Foundry
- Nodejs Buildpack version 1.6.40
- CF Trial (Europe – Frankfurt)
Prerequisites
- your space is created on Cloud Foundry environment
- cf CLI is installed on Local PC(see the official page for the installation)
Steps
1. Install Node.js
Since I have never used Node.js on my local PC, I installed Node.js first.
# Install Node.js using PPA
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
apt show nodejs
sudo apt install nodejs
# Check if it is successful
node --version
nmp --version
2. Run Hello world for checking Node.js
# Create a folder
mkdir sample && cd sample
# Create hello.js, which shows just "Hello world"
echo 'console.log("Hello, World!");' > hello.js
# Run
node hello.js
3. Clone sample repository
Clone sample repository from GitHub.
# Clone repository
git clone -b 1_REST_persist_in_Memory https://github.com/SAP/cloud-cf-helloworld-nodejs.git
cd cloud-cf-helloworld-nodejs
# configure npm
npm config set @sap:registry https://npm.sap.com
# npm instll
npm install
npm install --save express body-parser axios
4. Try Node.js on local PC
Run Node.js and see the result via browser.
# From directry of cloned
nodejs server.js
Open URL via browser.
http://localhost:8088/users
5. Deploy the app into Cloud Foundry
Using CF CLI, login and deploy the app into Cloud Foundly
cf api https://api.cf.eu10.hana.ondemand.com
cf login
# From directory of cloned repository
cf push sample-nodejs
Successfully deployed!
Now it works on Public internet space.
Appendix
Package.json
Node.js dependent information is in package.json.
{
"name": "SAP-CP-CF-HelloWorld",
"description": "Sample SAP-CP-CF Hello World Restful Service",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "4.15.3",
"body-parser": "1.17.2"
},
"engines": {
"node": "6.x.x"
},
"scripts": {
"start": "node server.js"
}
}
manifest.yml
Cloud Foundry dependent information is in manifest.yml.
---
applications:
- name: sapcpcfhw
path: .
buildpack: nodejs_buildpack
memory: 128M
Hi there, awesome tutorial! However, I had a bit of trouble getting everything compiled properly and uploaded to Cloud Foundry. I would like to share the steps I took to fix these issues.
Before Step 5 in this Article:
node -v
remember this version number by writing it down or copying it
nano package.json
It is not recommended, but you can also change this to x.x.x if you are still having problems
cf push NAME_OF_YOUR_APP -b https://github.com/cloudfoundry/nodejs-buildpack
Rename your app to something unique. You may get an error if somebody else has already taken the name
Remember, it will not work correctly if you are not inside the /cloud-cf-helloworld-nodejs folder.
If you followed everything correctly, it should now compile successfully because the correct version of node has been set and a buildpack is specified. Hopefully this helped!
There are a few other things to note as well after following this tutorial:
Increasing the Memory Size for your App:
nano manifest.yml
If you are working with a trial account, you have 4GB available so you can safely bump this number up a little. But remember to save some in case you deploy other apps too
Discovering your App’s Endpoint
Hi there,
Do we have any limitation of using any template engineer (like ejs ) on the cloud platform?
I tried to deploy one which is working find on my local machine, but facing internal server error after deploy to cloud foundry
Hi,
I have no idea. I cannot help you.
Regards,
Yohei
Hi Yohei,
No worries, it does support the engine.
I was misleading by some other issue and thought it was not supported.
Thanks,