Create a Node.js App on Cloud Foundry in 3 minutes

By | December 28, 2011

I’m currently involved in a couple of projects with ISVs to modernise their application infrastructure and transition shrink wrapped application products to deployment on cloud platforms. My key involvement here is in architecting the underlying physical infrastructure to enable applications to scale up to very large multi-tenant deployment.

In addition, the ISVs are working to replatforming their applications using modern programming languages and frameworks and services-based architecture that enables applications to scale in a elastic manner. This second component is being done in conjunction with VMware. Specifically with VMware’s Cloud Foundry/vFabric ISV team. This has provided a great opportunity learn about VMware’s application modernisation strategy and the products and tools that they are producing to support this.
One of the ISVs that we are working with is undertaking this application replatforming activity using the server-side JavaScript software system, Node.js. So I have had to do some background reading around the subject as this is a new topic area for me. As part of my reading about the subject, I stumbled across this post, which is a very basic walkthrough of Node.js application deployment on Microsoft’s Azure PaaS. As this my first opportunity to look at Cloud Foundry in any detail, I thought I’d record my experiences in getting up an running with Cloud Foundry by way of comparison.
Signing up for Cloud Foundry
The first thing you need to do is visit cloudfoundry.com and Sign Up for a Cloud Foundry account. You may have a to wait a few hours to receive your confirmation email, which will include your temporary password and some links to useful ‘getting started’ documents. For this quick walkthrough the most important link will be the first one in the email, which is the guide to getting started with vmc – the Cloud Foundry command-line interface.
Installing vmc
 
The first thing you need to do is install vmc on your workstation. As vmc is delivered as a Ruby gem, you will also need to install Ruby and RubyGems before vmc if you don’t already have them installed. Ruby and RubyGems are available for Windows, Mac OS X and the 5 most popular Linux distributions, so unless you have a very esoteric choice in workstations, you should be able to find something that works for you. Full details of the installation process are in this document.

Setting the PaaS deployment target and changing your password


Once you’ve successfully installed vmc, you need to do some initial set up as follows.

$ vmc target api.cloudfoundry.com

This tells vmc that subsequent deployment actions should be targeted at the public Cloud Foundry instance which is accessed via the interface at api.cloudfoundry.com.

You can check which deployment target you are using at any time by issuing the same command without specifying a new target:

$ vmc target

The next step is to login to the deployment target. Do this with the following command:

$ vmc login

You will be prompted for the login details that you received in your confirmation email, so enter them here. You should change your password the first time you login by issuing:

$ vmc passwd

Once you’ve done this, it’s time to create some sample code and push this onto the PaaS for testing.

Creating a Sample Application

Create a new directory to hold your sample application and create and save a file called app.js in this directory with the following code in it.

app

The bottom part of application is a pretty standard Hello World application written in Node.js, which creates a http server and responds to each connection with the following output.

HTTP/1.1 200 OK
Content-Type: text/plain

Hello World

The first couple of lines of code are the interesting parts as far as development on Cloud Foundry is concerned. The use of the VCAP_APP_PORT and VCAP_APP_PORT environment variables allow the application to correctly configure itself for operation on the PaaS platform. These are set conditionally with default alternatives that also allow the same application code to be run locally for testing without any modifications.

So, on a Mac or Linux host it’s possible to test the application by running the following commands whilst in the newly created directory.

$ node app.js &
$ curl -i localhost:3000

You can then shut the app down by bringing the application to the foreground with fg and then pressing ctrl + c to terminate the application.

Deploying the Sample Application to Cloud Foundry


Whilst still in the directory containing the app.js sample application, enter the following command:

vmc push

This will start interactive deployment of the application from the current directory. For Node.js applications to be deployed correctly, the main application code must be in a file called app.js in this directory. You will be prompted to enter a few pieces of relevant information before your application can be deployed. Defaults are highlighted in capitals and you should accept these where available.

You will need to enter an Application Name, which needs to be unique within your account and and Application Deployed URL, which needs to be unique for the instance of Cloud Foundry that you are deploying on, so it’s helpful to prefix this with something that only you are likely to use if you’re deploying on a public platform. For my sample deployment I used test as the Application Name and spcto-test.cloudfoundry.com as the Application Deployed URL.

If it’s successful the deployment dialogue should look like this:

$ vmc push
Would you like to deploy from the current directory? [Yn]:
Application Name: test
Application Deployed URL [test.cloudfoundry.com]: spcto-test.cloudfoundry.com
Detected a Node.js Application, is this correct? [Yn]:
Memory Reservation (64M, 128M, 256M, 512M, 1G) [64M]:
Creating Application: OK
Would you like to bind any services to ‘test’? [yN]:
Uploading Application:
Checking for available resources: OK
Packing application: OK
Uploading (0K): OK
Push Status: OK
Staging Application: OK
Starting Application: OK

It’s possible to verify that the application has been deployed and check its status with the following command:

$ vmc list

If the deployment has been successful, this should display the following output:

+————-+—-+——–+—————————–+———-+
| Application | #  | Health | URLS                        | Services |
+————-+—-+——–+—————————–+———-+
| test        | 1  | N/A    | spcto-test.cloudfoundry.com |          |
+————-+—-+——–+—————————–+———-+

You can now test the deployed application by going to the URL that you specified as the Application Deployed URL in your browser. You can see the result of this test for my application below.

 

cf deployed app test

Finally, you can remove the deployed application from your account by issuing the following command:

$ vmc delete test

Make sure you use the same application name that you used during deployment, you can check what this was with the vmc list command.

That concludes my quick guide to application deployment for Node.js applications on Cloud Foundry. You should be able to see that this is very similar to the deployment process for Microsoft’s Azure PaaS.

Leave a Reply