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.
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.
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
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:
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]:
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.
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.