Create a NodeJS Application at SAP CP Cloud Foundry Environment


As a demo of the concepts discussed in previous articles of our series, we have collected in a single resource the best practices on how to build a nodejs application. These are a compedium of multiple blogs which explain how to create a Node JS Application. Also, SAP has forked the standard Cloud Foundry NodeJS sample application.

Manifest File – Metadata File

Cloud Foundry apps are configured via the file manifest.yml. This file is located in the root directory of the project. it contains instructions for the CF runtime. These instructions are followed when the application is deployed and started. Basically,It is a simple text file with space indentation (beware only use spaces, not tabs, for indentation).

Cloud Foundry requires a manifest file. This file acts as metadata for your application. It is necessary to create a manifest.yml file with some basic information about the application name, memory allocation and instance number, etc.

The information included in the cf push command for setting up the environment when the application is deployed to the Cloudfoundry instance. To deploying with Application Manifiests, use manifiest.yml file to something unique.

The manifest are written in YAML. The following information is provided in the YAML file.

  • name: The name of the application.
  • buildpack: The name of the Node.js buildpack determined before with command cf buildpacks.It is also possible to reference the buildpacks sources on GitHub.
  • memory: How much memory to use. It indicates the definition of the RAM available for the application. Since this a minimial demo node.js a number of 64MB are used.
  • disk_quota: Definition of the disk space available for the application. A disk space value similar to the memory value is used.
  • host: Host information for the application makes the application accessible from other systems.

The manifest below illustrates some YAML conventions:

  • The manifest may begin with three dashes.
  • The applications block begins with a heading followed by a colon.
  • The application name is preceded by a single dash and one space. The subsequent lines in the block are indented two spaces to align with name.
---
applications:
- name: cfnodejsapp
  memory: 64M
  disk_quota: 64M
  host: cfnodejsapp
  instances: 1
  buildpack: nodejs_buildpack

Package.json

As NodeJS developers already known, the Node applications have their own metadata which included at the package file. Thus, it is necessary to create a package.json.

This file could be generated automatically using the npm command which is installed together with the Node Program using npm init. Since it is not the purpose of this series to get deep in the NodeJS development. I have included some tutorials in the reference area. As an example, I include the package.json file of the cfnodejsapp.

{
	"name": "cfnodejsapp",
	"version": "0.0.1",
	"description": "NodeJS App for SAP Cloudfoundry",
	"keywords": [
		"Cloudfoundry",
		"SAP"
	],
	"bugs": {},
	"license": "[\"ISC\"]",
	"author": "fill your name her",
	"contributors": [],
	"maintainers": [],
	"files": [],
	"man": [],
	"directories": {},
	"repository": {},
	"scripts": {
		"start": "node server/app.js"
	},
	"config": {},
	"dependencies": {
		"body-parser": "^1.17.2",
		"cfenv": "^1.0.4",
		"express": "^4.15.4",
		"mongoose": "^4.11.8"
	},
	"devDependencies": {},
	"bundleDependencies": [],
	"bundledDependencies": [],
	"optionalDependencies": {},
	"peerDependencies": {},
	"engines": {
		"node": "6.11.1"
	},
	"os": [],
	"cpu": [],
	"private": true,
	"publishConfig": {},
	"dist": {},
	"jspm": {
		"keywords": [],
		"bugs": {},
		"licenses": [],
		"author": {},
		"contributors": [],
		"maintainers": [],
		"files": [],
		"man": [],
		"directories": {},
		"repository": {},
		"scripts": {},
		"config": {},
		"dependencies": {},
		"devDependencies": {},
		"bundleDependencies": [],
		"bundledDependencies": [],
		"optionalDependencies": {},
		"peerDependencies": {},
		"engines": {},
		"os": [],
		"cpu": [],
		"publishConfig": {},
		"dist": {}
	}
}

The best way for executing the application is to use npm script in package.json and let the SAP Cloud Platform execute the npm start script. The package.json executes the command included in the start.

Cloud Foundry Ignore File

It is a best practice .cfignore file to the root of your application and add node_modules. This will prevent node_modules being uploaded to CP. This file is created which contains all folders/files to be ignored by the *cf push* command. For instance, the *node_modules* folder can be excluded from the uploading done during the *cf push*.

References

CP Cloud Foundry – Playing around with Node.js, MongoDB and OpenUI5
Node cfenv project