Today I’m going to show you how to build a simple application that uses Nodejs, Mongoose, Express and MongoDB that will persist some data and get it back. It will also have a simple configuration file so I can configure my routes in Express. For those that don’t know, Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment and Express is a Sinatra inspired web development framework for node.js.

I’m installing and running this on Ubuntu 11.x. If your using OS X you might need to make some changes. With a clean Ubuntu 11.x I installed Node.js and NPM (Node Package Manager).

Installing Node (the current version as of the date of this article is v.0.6.6, use an apt package if you prefer or download it from the Nodejs website). Below are the instructions to build it from the source copied from GitHub:

Theo code is pretty simple. We bring in the required libraries, declare the schema and ensure at least one entry is in MongoDB before starting the server. Also, you could very easily do this without Express since this is such a trivial example.

If your web browser refuses to open the content simply change this line of code:

res.contentType('application/json');

To:

res.contentType('text/plain');

– just remember to change this back if you do want to truly pass back JSON results to a target aware listener.