How-to deploy your Node.js app on anynines

I wrote a small Node.js app to test deploying with Node.js and MongoDB on anynines. The application enables visitors to browse quotes and add their own to the database (what could possibly go wrong?). It’s not likely it will ever win a startup competition, but it IS a neat way to demonstrate deploying a JavaScipt app* on anynines.

You can find the example application at

Getting started with Node.js

Make sure you have Node.js installed and MongoDB setup on your developer machine. Let’s verify your Node.js installation and start the MongoDB server:
$ node -v
$ mongod

Clone the repository to take a look at the application and run it locally:
$ git clone
$ cd nodejs_mongodb_example
$ node app.js

Visit https://localhost:3000 in your browser to see the application in full glory.

Adding the database

If you’re thinking about binding one of our supported services (like Redis, MongoDB or RabbitMQ for instance) to your application, the access information / credentials (usually: host, port, user, password) to this service will be provided in the environment variables.
For the MongoDB credentials and Mongoose initialization, please refer to our db.js:
Db.prototype.read_mongodb_url_from_env = function() {
try {
var vcap_services = JSON.parse(process.env.VCAP_SERVICES);
mongo_url = vcap_services['mongodb-2.0'][0].credentials.url;
return mongo_url;
} catch (err) {
console.log("An error occurred while loading the MongoDB credentials from the env:", err)
console.log("Please ensure that you have bound a MongoDB service instance to the application!")
throw err

Defining our package and bunch of dependencies is what we will do in our package.json:

“dependencies”: {
“express”: “~4.0.0”,
“static-favicon”: “~1.0.0”,
“morgan”: “~1.0.0”,
“cookie-parser”: “~1.0.1”,
“body-parser”: “~1.0.0”,
“debug”: “~0.7.4”,
“hogan-express”: “*”,
“mongoose”: “~3.8.8”

The application

With our little application we can create, show, index and delete quotes. Show me the code, you say? You can check out quotes controller on GitHub!

Our quote data model looks somewhat like the following:
var mongoose = require('mongoose')
, Schema = mongoose.Schema;

var QuoteSchema = new Schema({
author: {type : String, trim: true},
quote: {type : String, trim: true}

QuoteSchema.methods = {
to_s: function () {
var str = + ” : ” + this.quote
return str

var Quote = module.exports = mongoose.model(‘Quote’, QuoteSchema)

To get an idea of what our routing table looks like, take a look at our routes.js.

Run locally

Adjust the database connection credentials in the file and source it’s contents. This is needed to insert the VCAP_SERVICES json hash into the application shell’s environment. This simulates the process of anynines app containers.
DEBUG=* bin/www

You probably want to add a .cfignore before you deploy your application. Make sure you add node_modules to this file.

Deploy your app on anynines

Using the Ruby cli (v5)

Install the Ruby CLI:
$ gem install a9s

Copy over the example manifest and rename all occurrences of app_name with the desired application name.
cp manifest.yml.v5 manifest.yml
cf push

Using the Go cli (v6)

Install the Cloud Foundry go CLI and edit your deployment manifest:
cp manifest.yml.v6 manifest.yml
vim manifest.yml

Exchange all concurrences of app_name with your desired application identifier. Create a MongoDB service:
cf create-service mongodb 100 mongodb-app_name

And deploy your application:
cf push

… and marvel at the beauty of our Node.js app!

For further information on how to deploy applications to anynines please refer to our documentation.

* You might recognize I followed the MVC (Model-View-Controller) pattern – what can I say… design patterns for the win!

Leave a Reply

Your email address will not be published.