Deploying an Application to Cloud Foundry V2

I was struggling to get my Cloud Foundry V1 application migrated to V2 so wrote this article to show the steps necessary to get a (simple) Node.JS application deployed on the Cloud Foundry V2 hosted service. I’m assuming that you have Node.JS and NPM installed on your development machine.

Step 1 – A Hello World example.

Here’s a simple Node.JS example which simply posts a “Hello World” message. Note, this program is using Express – a Node.js web application framework.

Create a directory (hello-world) containing the following files

app.js

var express = require("express");
var app = express();
  app.use(express.logger());
  app.get('/', function(request, response)
  {
    response.send('Hello World!');
  });

var port = process.env.PORT || 3000;
  app.listen(port, function()
  {
    console.log("Listening on " + port);
  });

package.json

{
  "name": "hello-world",
  "description": "hello world test app",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "express": "3.2.6"
  }
}

manifest.yml

---
applications:
- name: kml_Hello
  command: node app.js
  memory: 256M
  instances: 1
  url: kml_Hello.cfapps.io
  path: .

Run the following command line, in this directory, to install the necessary dependent libraries

npm install

This will create a sub-directory node_modules, containing the dependencies (in this case the express framework).

Test the program by running

node app.js

and point your browser at

http://localhost:3000

to see the message “Hello World”.

Step 2 – Deployment

Assuming you have an account set-up on Cloud Foundry, it is easy to deploy this application. For V2, you need to use the command line interface cf which is used to deploy and manage applications.

http://docs.cloudfoundry.com/docs/using/managing-apps/cf/

First check the target api using

cf info

You may need to login (using your cloud foundry account details) and you should check that the target is set correctly to

https://api.run.pivotal.io

Deploy the application using

cf push kml-Hello

Answer the simple questions and the application will be deployed.


C:\DATA\cloudfoundry\hello-world>cf push kml_hello
Could not find kml_hello' in the manifest.

C:\DATA\cloudfoundry\hello-world>cf push kml_Hello
Using manifest file manifest.yml

Creating kml_Hello... OK

1: kml_Hello
2: none
Subdomain> kml_Hello

1: cfapps.io
2: none
Domain> cfapps.io

Binding kml_Hello.cfapps.io to kml_Hello... OK
Uploading kml_Hello... OK
Starting kml_Hello... OK
-----> Downloaded app package (748K)
Installing nodejs.
-----> Resolving engine versions

       WARNING: No version of Node.js specified in package.json, see:
       https://devcenter.heroku.com/articles/nodejs-versions

       Using Node.js version: 0.10.12
       Using npm version: 1.2.30
-----> Fetching Node.js binaries
-----> Vendoring node into slug
-----> Installing dependencies with npm
       npm WARN package.json hello-world@0.0.1 No repository field.
       npm WARN package.json hello-world@0.0.1 No readme data.
       npm WARN package.json cookie-signature@1.0.1 No repository field.
       npm WARN package.json fresh@0.1.0 No repository field.
       npm WARN package.json methods@0.0.1 No repository field.
       npm WARN package.json range-parser@0.0.4 No repository field.
       npm WARN package.json send@0.1.0 No repository field.
       npm WARN package.json bytes@0.2.0 No repository field.
       npm WARN package.json pause@0.0.1 No repository field.
       npm WARN package.json hello-world@0.0.1 No repository field.
       npm WARN package.json hello-world@0.0.1 No readme data.
       npm WARN package.json cookie-signature@1.0.1 No repository field.
       npm WARN package.json fresh@0.1.0 No repository field.
       npm WARN package.json methods@0.0.1 No repository field.
       npm WARN package.json range-parser@0.0.4 No repository field.
       npm WARN package.json send@0.1.0 No repository field.
       npm WARN package.json bytes@0.2.0 No repository field.
       npm WARN package.json pause@0.0.1 No repository field.
       express@3.2.6 /tmp/staged/app/node_modules/express
       connect@2.7.11 /tmp/staged/app/node_modules/express/node_modules/connect
       qs@0.6.5 /tmp/staged/app/node_modules/express/node_modules/connect/node_modules/qs
       formidable@1.0.14 /tmp/staged/app/node_modules/express/node_modules/connect/node_modules/formidable
       cookie-signature@1.0.1 /tmp/staged/app/node_modules/express/node_modules/cookie-signature
       buffer-crc32@0.2.1 /tmp/staged/app/node_modules/express/node_modules/buffer-crc32
       cookie@0.0.5 /tmp/staged/app/node_modules/express/node_modules/connect/node_modules/cookie
       send@0.1.1 /tmp/staged/app/node_modules/express/node_modules/connect/node_modules/send
       debug@0.7.2 /tmp/staged/app/node_modules/express/node_modules/debug
       mime@1.2.9 /tmp/staged/app/node_modules/express/node_modules/connect/node_modules/send/node_modules/mime
       fresh@0.1.0 /tmp/staged/app/node_modules/express/node_modules/fresh
       range-parser@0.0.4 /tmp/staged/app/node_modules/express/node_modules/range-parser
       bytes@0.2.0 /tmp/staged/app/node_modules/express/node_modules/connect/node_modules/bytes
       pause@0.0.1 /tmp/staged/app/node_modules/express/node_modules/connect/node_modules/pause
       commander@0.6.1 /tmp/staged/app/node_modules/express/node_modules/commander
       mkdirp@0.3.4 /tmp/staged/app/node_modules/express/node_modules/mkdirp
       cookie@0.1.0 /tmp/staged/app/node_modules/express/node_modules/cookie
       methods@0.0.1 /tmp/staged/app/node_modules/express/node_modules/methods
       send@0.1.0 /tmp/staged/app/node_modules/express/node_modules/send
       mime@1.2.6 /tmp/staged/app/node_modules/express/node_modules/send/node_modules/mime
       Dependencies installed
-----> Building runtime environment
-----> Uploading staged droplet (16M)
-----> Uploaded droplet
Checking kml_Hello...
Staging in progress...
Staging in progress...
  0/1 instances: 1 starting
  0/1 instances: 1 starting
  1/1 instances: 1 running
OK

Point your browser at this address (http://kml_hello.cfapps.io/) to see your “Hello World” message.

The application can be managed from your Cloud Foundry console or from the cf (cli) application.