@brunofuster

www.wesaveapp.com

deploying a node.js project on cloudfoundry (mac osx)

leave a comment »

If you don’t have your cloudfoundry credentials create here: www.cloudfoundry.com

I’ve run into a bug using Mac’s ruby. If you already use rvm and ruby 1.9.2, you can skip the first step. Found the bug solution here.

Setting up Ruby

Install rvm:

$ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

Add the following lines to ~/.bash_profile

# Ruby Version Manager
[[ -s “$HOME/.rvm/scripts/rvm” ]] && . “$HOME/.rvm/scripts/rvm”

Update your terminal env with source or open a new terminal

$ source ~/.bash_profile

Now install ruby 1.9.2

$ rvm install 1.9.2
$ rvm use 1.9.2

Configuring cloudfoundry

Install the vmc gem

$ gem install vmc

Set the target

$ vmc target api.cloudfoundry.com

Login with your credentials… vmc will ask for your email and password.

$ vmc login

Creating a simple express app

* You need Node.JS and NPM from now on

$ sudo npm install express jade
$ mkdir sample-project
$ cd sample-project
$ express

Start your app to check its running:

$ node app.js

Open http://localhost:3000 and see the Welcome to Express msg :)

Configuring your app to run on cloudfoundry

Install cloudfoundry module

$ npm install cloudfoundry

Change your app.js to run locally and at the cloudfoundry

$ vi app.js

Require the cloudfoundry module

var cloudfoundry = require(‘cloudfoundry’);

Go to the app.listen line at the end of the file and change to:

app.listen((cloudfoundry.port || 3000), (cloudfoundry.host || ‘localhost’));

Create a file called package.json and put the used modules:

*jade is the view engine

{
    “name”:”sample-app-bfuster”,
    “version”: “0.0.1”,
    “dependencies”: {
        “express”:””,
        “cloudfoundry”:””,
        “jade”:””
    }
}

Bundle those modules into your project

$ npm bundle

Deploy your app

$ vmc push

Console output:

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

Running app: http://sample-app-bfuster.cloudfoundry.com
source: https://github.com/bfuster/cf-sample-app

Advertisements

Written by brunofuster

October 27, 2011 at 11:54 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: