Deploy your Angular 7 app with Cloud Foundry on SAP Cloud Platform

In this tutorial, I will show how to deploy an Angular 7 app to Cloud Foundry on SAP Cloud Platform.

Prerequisite

The first thing is to install the Angular CLI globally

sudo npm install -g @angular/cli

Note that you must have installed node (v8.9 or higher) and npm (v6.0 or higher)

Frontend

To start quickly and for the demonstration, we will download the CLI starter kit from angular.io website (https://angular.io/generated/zips/cli-quickstart/cli-quickstart.zip). Once you get the zip file, just unzip and follow the next steps with your terminal below.

cd cli-quickstart
npm install
ng serve

Open your browser on http://localhost:4200/ to test the result, you might get something like this:

Great. The project is working locally. Now if you open the package.json file, you will see different scripts to lint, test, build, e2e, etc. The command which is interesting for us is to build the project for production. In order to push this to Cloud Foundry later.

Launch the build script with the following command:

npm run-script build

Frontend is ready.

Backend

For the demo, I wanted to add an API. The steps are easy, the first step is to create a folder api at root of your project.

mkdir api

In this new folder, create two files: server.js and package.json

server.js

var express = require("express");
var app = express();

app.listen(process.env.PORT || 3000, () => {
console.log("Server running.");
});

app.get("/demo", (req, res, next) => {
res.json(["Hello","World"]);
});

package.json

{
"name": "angualar-7-backend",
"version": "0.0.1",
"scripts": {
"start": "node server"
},
"dependencies": {
"express": "^4.16.4"
}
}

Backend is ready.

Prepare for production

Now we have to create new files specifically for Cloud Foundry deployment. The first file is manifest.yml and it allows you to declare the url of the app/service, the memory, disk_quota, etc. You have to create this file at the root folder of your project:

manifest.yml

---
applications:

- name: angular-7-frontend
host: angular-7-frontend
path: dist
buildpack: staticfile_buildpack
memory: 512M
disk_quota: 512M
instances: 1
random-route: true

- name: angular-7-backend
host: angular-7-backend
path: api
buildpack: nodejs_buildpack
memory: 512M
disk_quota: 512M
instances: 1
random-route: true

The second file is .cfignore

node_modules
api/node_modules

Deployment is ready.

Push to production

Before to push, make sure you are connected (and linked to the right spaces in case you have several). To connect to CF account, execute the following command and follow the steps from the CLI:

cf login
# enter email address
# choose your space

Time to push!

cf push

Done 👍

Useful links

SAP Cloud Platform & Cloud Foundry – test, feedback, tips and tricks

After you followed all the steps to setting up Cloud Foundry in your SAP Cloud Platform Cockpit, it’s time to dig into and like all new environment you are testing, you’ll face some problems.

In this article, I’ll quickly overview the activation/installation and talk about a simple NodeJS with a MongoDB scenario. I hope it will help you to be more familiar with the tools, to implement your own needs on the platform.

Quick CF activation/installation

Install CF on your machine and follow instructions detailed here. One it’s installed, you need to test the CF CLI by typing cf in your favorite terminal.

A quota was not automatically assigned for my account because I had several sub accounts. Then, when I deployed an application, I get an error message:

Activate MongoDB / PostgreSQL and other services

Follow the path: Cloud Foundry > Services > Service Marketplace and activate the services you want to use. You can directly map the service with your app if you have already deployed it.

Create and deploy a NodeJS

First, you need to create a skeleton NodeJS project using the Nodeclipse available for free on the Eclipse Market or create your own project from scratch using the framework your like. You can simply download this project from my Github account : js-app-for-sap-cloud-foundry.

If you are not using mongodb in the scenario, you can comment the line require(‘./server/db/mongo-connect.js’)(oAppEnv);. It simplifies the workflow, you can uncomment it later. When your project is ready, be sure it compiles by running the two following commands:

npm install
node app.js

If all is OK, your project is ready to be hosted on CF.

// Logon to Could Foundry
cf login
// Enter your credentials and navigate to the space you want to host the app
// Change directory and push it!
cd js-app-for-sap-cloud-foundry
cf push

The file manifest.yml is very important for your app deployment, to specify the app information: commands, name, buildpack, etc. Below, an example of manifest.yml file

---
applications:
- name: js-app
  memory: 512M
  host: js-app
  command: node app.js

Fields you need to know for the manifest.yml file:

Key Value
name The name of the application
buildpack The name of the Node.js buildpack determined before with command cf buildpacks. (by default an auto determination of the buildpack is done if the buildpack information is missing)
command For NodeJS apps, a start command is needed to start the application. Here for example, it’s “node app.js”.
memory RAM available for the application.
disk_quota Disk space available for the application.
host Host information used in the URL which makes the application accessible.

Connect with a MongoDB

The first step is to create a new MongoDB instance. You have to go to your SAP CP CF sub account > Services > Service Marketplace and select MongoDB. Now in your left panel you can select Instances and create a new one (see below)

Now you are able to map your instance with the app you deployed before. The manifest.yml will change a little bit and the service will be mapped like this:

---
applications:
- name: js-app
  memory: 512M
  host: js-app
  command: node app.js
  services:
  - mongodb-service 

/!\ “mongodb-service” is the name of you instance, you need to adapt /!\

Useful links

Github | SAP/hcp-cloud-foundry-tutorials
SAP CP | Cloud Foundry Tutorials
Github | Cloud Foundry CLI
Cloud Foundry CLI Reference Guide

Useful CLI commands

cf
cf apps
cf login
cf logout
cf push
cf buildpacks
cf marketplace
cf api api.cf.<host information>
cf create-service mongodb v3.0-dev mongo-service