Using CloudFoundry with Node.JS and MongoDB

This isn’t so much a question, just a bit of extra documentation for people. There’s not a lot of clear examples on how to make a simple connection from CloudFoundry to Mongo DB when you’re using NodeJS, so I thought I’d stick up this example using the simple to use MongoDB mongoskin wrapper. The MongoDB native driver for Node is a bit awkward when you’re using authentication, so I skipped to the mongoskin layer, which is almost identical.

In your application directory, you need to a file called app.js containing the following code:

var mongodb = require(‘mongoskin’);
var env = JSON.parse(process.env.VCAP_SERVICES);
var mongo = env[‘mongodb-1.8’][0][‘credentials’];
var mongourl = “mongo://” + mongo.username + “:” + mongo.password + “@” + mongo.hostname + “:” + mongo.port + “/” + mongo.db + “?auto_reconnect=true”;
console.log(mongourl);
var db = new mongodb.db( mongourl ); //This is the connection

var http = require(‘http’)
port = Number(process.env.VCAP_APP_PORT || 3000),
host = process.env.VCAP_APP_HOST || ‘localhost’;

http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
var currentdate = new Date();

db.test.insert({ daterecord:  currentdate }, {}); //This is the insert

res.end(‘Welcome to Cloud Foundry!\n’ +
host + ‘:’ + port + ‘\n’ +
require(‘util’).inspect(process.env, false, null) + ‘\n’ + fullresult + ‘\n’);
}).listen(port, host);
console.log(‘Server running at http://’ + host + ‘:’ + port + ‘/’);
You also need a subdirectory called node_modules containing the mongoskin module. Using the latest version (1.0.3 of npm) to do this, run:
mkdir node_modules
npm install mongoskin
then run
vmc push appname
When asked if you want to bind a service, say Yes, then pick MongoDB.
The username, password, etc, are picked up automatically in the app.js file.
All the script actually does is insert a new record each time you visit the page, containing the current date, then dumps out all the environment variables to screen, so you can see where the database details are gathered from.
Hope this helps some people, if you have any problems with the code please leave me a comment on here!

Creating your own Personal Twitter Archive

I’ve seen enough company failures and changes in terms of service to always like a backup plan for when a service goes missing (plus working on system migrations tends to make you think the worse of data).

As a pretty heavy user of Twitter, I’ve been thinking more about what happens if the service goes bad, or if they simply never fix their broken search function.

To help with both, I’ve written a fairly hacky pair of node.js scripts which collect any mentions of you, and any tweets you send, using the Twitter Search API, and uploaded it to github.

The 2 files are pta_server.js and pta.js.

pta_server.js is meant to be run in the background using something like forever, and connects to Twitter Search, gathers any new tweets, and stores them in a MongoDB data store.

pta.js is a very basic web interface, which connects to the MongoDB data store and retrieves all collected tweets, ordered by date.

Hopefully it’ll come in useful, and I plan on improving both the functionality and the documentation soon. If you want to give it a try, check it out and let me know what you think.

Cloud Foundry trademark and competitive services

This afternoon on Twitter, I saw an interesting interchange between @mchmarny and @samj

from @mchmarny:

VMware’s Cloud Foundry is to PaaS what OpenStack is to IaaS

and in reply from @samj:

@scottsanchez @mchmarny: a nit — you don’t see “rackspace @openstack“, nor should you see “vmware @cloudfoundry“.

But that got me thinking along the lines of my earlier post about cloudfoundry.com compared to cloudfoundry.org.

The trademark for “Cloud Foundry” belongs to VMware, and their legal page states:

Cloud Foundry, VMware, the VMware “boxes” logo and design are registered trademarks or trademarks (the “Marks”) of VMware, Inc. in the United States and/or other jurisdictions.  You are not permitted to use the Marks without the prior written consent of VMware.

I’m not a lawyer, but to me that pretty clearly states that you couldn’t fire up a competitive service to cloudfoundry.com using the cloudfoundry.org software, and mention “Cloud Foundry” on your service. This compares to comprehensive OpenStack’s trademark policy, which has a significant section on “Permitted uses”, including one part:

If you may use the OpenStack Word Mark to describe that your solution is built on or with the OpenStack technology formally released on the www.openstack.org website (using genuine OpenStack code base), you must state that your product is “Built With the OpenStack™ technology.”

While there’s plenty of time for VMware to make cloudfoundry.org the PaaS layer to OpenStack’s IaaS layer, there’s also plenty of work for VMware to do before that initial statement is true.