Miki Yarkoni
posted this on March 24, 2011 12:49
Cloud Foundry supports deploying Node.js apps today. We currently run Node.js version 0.4.12.
When deploying any application to vCloudLabs, the directory from which you push the app via vmc push, needs to include all packages and dependencies that are needed to run your application, and your application itself. Node.JS applications are no different.
NPM, or the Node Package Manager has become popular as a way to add software packages to your Node.js application.
Generally, utilizing npm will allow your application to run locally, but it will not work correctly when pushed to Cloud Foundry.
Below is a simple hello world app that uses express (http://expressjs.com), installed via npm.
NOTE: The main Node.js file needs to be named app.js when pushing to Cloud Foundry.
A couple of things to note.
Running this application locally is straightforward:
> node app.js
To run this application sucessfully on Cloud Foundry, we need to do a few simple steps.
The package.json file is listed below..
{
"name":"simple",
"version":"0.0.1",
"dependencies":{
"express":""
}
}
After running the 'npm install' command, our directory will now look like this..
> ls
app.js node_modules/ package.json
All of the dependecies for expressjs will be included under node_modules.
Last, we need to make a few small changes to our app.js (bolded)
====== app.js ======
require.paths.unshift('./node_modules')
var app = require('express').createServer();
app.get('/', function(req, res){
res.send('hello world test using express and npm');
});
app.listen(process.env.VCAP_APP_PORT || 3000);
==================
That's it, you are ready to execute via 'vmc push node_simple'...
Note: When running 'vmc push' you may get the following error:
Error: Can't deploy application containing links ...
This is a known bug that is being fixed in vmc. The workaround for now is to run the following from the node.js app root:
> rm -r node_modules/.bin/