Deploy a React-Redux Node.js application on SAP HCP with Cloud Foundry
Cloud Foundry has several buildpacks. One of them is the system buildpack node.js. This buildpack enables us to deploy node.js applications on HCP in just a few easy steps.
To deploy a node.js application there are a few prerequisites.These are:
1. Enable the Starter Edition for Cloud Foundry Services
Login to HCP and enable the service Starter Edition for Cloud Foundry Services. Yes, the Cloud Foundry Services are in beta. I have no idea when this will go into production but I cannot imagine this will be too long because it is working.
Go to the service and create an Organization and a project within that organization. There seems to be a litte meshup between the terms because the project you create within an organization is in fact the name you want to remember as the organization when you push your application to HCP-CF. When you’re done creating a project/organization, create a space.
Create a space by first go to Administration and add a subscription to the HCP Starter Edition. Make sure you have the left menu entry “SAP HANA Cloud Platform”.
2. Install cf cli
Cloud Foundry has it’s own command line interface like the Heroku Toolbelt which makes it very easy to deploy a node.js application to HCP.
Now create your application. I created a react-redux application node.js application which I bundled with WebPack but any node.js application you create or have created will do ofcourse .
The steps to follow are :
1. Add files to your node.js application
Add a .cfignore file to the root of your application and add node_modules. This will prevent node_modules being uploaded to HCP. You do not need the node_modules folder since HCP will do a npm install when you deploy your application. The .cfignore is much like your .gitignore file.
Add a Procfile (optional) and add the line:
web: node server/server.js or web: node app.js
Add a manifest.yml file with some basic information about the application
applications: - name: my-reactredux-app
The best way imo is to use npm scripts in package.json and just let HCP execute the script npm start. This would avoid having to create a Procfile. So in my package.json HCP will execute $ node server/server.js
2. Run the cf api / login / push commands
The cf commands are basically the same as Heroku has.
//creates an endpoint $ cf api https://api.cf.us10.hana.ondemand.com //login to the organization(project) and space $ cf login https://api.cf.us10.hana.ondemand.com -o bertdeterd -s dev //deploy $ cf push
3. check your application
Within the project click on the button MANAGE HCP APPLICATIONS. A project is made up of 1 or several spaces.
Click on the url in Application Routes to see your application working.
And the app:
That’s it. With just a few easy steps you can deploy your node.js application on HCP. If you have a trial HCP account, give it a try.