Over a million developers have joined DZone.

Node.js Love w/ PaaS: Part 2 - Deploying on AppFog

DZone's Guide to

Node.js Love w/ PaaS: Part 2 - Deploying on AppFog

· Cloud Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

Originally authored by Adron Hall

In the first part of this series about Node.js and PaaS deployment options, I covered Windows Azure. In this part, I’m going to dive in with a little Cloud Foundry-powered awesomeness by AppFog. The development team at AppFog has one of the most elegant user interfaces and clean user experiences available for Cloud Foundry today. In addition they provide the ability to avoid being limited to a single Infrastructure-as-a-Service (IaaS) provider like AWS, HP or Rackspace. Instead, you can pick the one you want or several of them. Right now they’re working on adding more infrastructure providers, including Windows Azure.

AppFog logoThe first step with AppFog is to signup for an account. AppFog has a really competitive pricing model based around memory; basically anything over 2 GB you pay for, up to that is free. So that makes testing, experimenting, and getting a basic site up and running is really easy on the pocketbook.

Once you have an account, sign in and navigate to the AppFog Console. This is the first page you’ll usually come to whenever you login:

AppFog console

From here click the New App button:

AppFog: Choose an Application

Here select the Node.js 0.6.8 application option, pick the cloud IaaS provider you want to use and name the application. Then click Create App:

AppFog: Choose an Infrastructure

Once the application is created you’ll see a simple status screen popup:

AppFog: Building App

Once the application starts you’re redirected to the control page:

AppFog: My Wall of Code

There is already a basic blank Node.js App.js file on the site, so if you navigate to the path the AppFog interface creates for you, you’ll see a simple ‘Hello World’ style page:

Hello World

Let’s walk through this starting with an empty application and putting together the application using the existing Github repo. This is the repo we discussed in Part 1. If you want to review the application again check out Part 1 and Part 2 of how I put together the project. For this deployment I did update my app.js a bit to be very simplified. The finished file looks like this:

var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
app.configure('development', function(){
app.get('/', routes.index);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));

With these minor changes the code base is ready to copy over and be deployed to AppFog. To do this requires a few steps and some command line action. Navigate to the AppFog Control Panel and click Update Source Code. You’ll find instructions there on how to pull down the code that is already on the site and how to edit that code. The first thing you’ll need to do is to get the af CLI, which is basically a Cloud Foundry VMC wrapped for AppFog, thus af.

gem install af

You may need to “sudo install af” depending on how you have your machine setup. Once that is installed, login with the new gem:

af login

Click the Download Source Code button on the AppFog Control Panel. Open up the code, put it into a directory you’d prefer and copy the contents of the Github repo over the top of this, replacing the app.js and everything else. Now navigate to the directory using bash and then with af type the following command to update the site with the new content.

af update mywallofcode

Your results come back while the site is updated and restarted with the new contents.

$ af update mywallofcode
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (156K): OK
Push Status: OK
Stopping Application 'mywallofcode': OK
Staging Application 'mywallofcode': OK
Starting Application 'mywallofcode': OK

Considering you can push the applications you want against several IaaS providers, AppFog’s capabilities really open up a lot of options that aren’t available elsewhere. This is especially useful when setting up a geographically dispersed site. This will grow even more as additional IaaS provider data points of presence are added.

Well, that should get your code up and running. Hope you enjoyed Part 2. Until next time, happy noding and coding.

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.


Published at DZone with permission of Leigh Shevchik, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}