markpollack edited this page Apr 11, 2011 · 4 revisions

Useful Tips for Node.js

Getting started:

  • Write an application in app.js
  • Use process.env to get the connection properties you need, e.g. var port = process.env.VCAP_APP_PORT || 8124; var service_info = JSON.parse(process.env.VCAP_SERVICES);
  • Push it up to the cloud vmc push mynodeapp --path .

Here's a basic app that prints the system environment (so you can see what is available to bind to):

var http = require('http'), url = require('url'), sys = require('sys');

var port = process.env.VCAP_APP_PORT || process.env['app.port'] || 8124;

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    sys.puts(new Date() + " request processed");

console.log('Server listening on port '+port);

You can ask this app to GET any resource and it will just return the system environment (as JSON).

Node.js module for CloudFoundry

You can get this helper module here