How To Setup and Test Node.js, CouchDB and MongoDB
Introduction
A project using a combination of Node.js and CouchDB can solve some pretty difficult problems with ease. Since originally writing this, I decided to use MongoDB instead of CouchDB and have now added in the instructions. I am attempting to solve one that involves consolidating several conflicting data sources into one document orientated database while providing a web interface for reading and writing.
Article Scope
This article will detail how I setup Node.js, CouchDB and MongoDB on Ubuntu Server Edition and preformed a few experiments with the system. The scope of this article does not include how you should do it or go into any detail as to how you could do it. It will only detail how to replicate what I did.
Server Setup
Download and install VMware Player. I installed it on Windows 7.
Download Ubuntu Server Edition iso. I got version 10.10.
Create a new virtual machine on VMware player but do not start the install already. Simply select the “I will install the operation system later” option since we don’t want VMware to configure our install for us. I gave the installation a 10GB hard drive and 128MB of memory.
Start the virtual machine and mount the ubuntu ISO to the cd drive and install it. When asked what software to install I selected LAMP, OpenSSH, PostgreSQL, Samba and Tomcat Java Servers.
Install CouchDB, MongoDB & Node.js
CouchDB is easy to install since ubuntu has added it to their software repositories.
sudo apt-get install couchdb
MongoDB is just as easy
sudo apt-get install mongodb
Node.js is a bit trickier. The easiest way it to get it from the git repository. First install git if you haven’t already got it.
sudo apt-get install git
Then get the node.js source from git.
git clone git://github.com/ry/node.git
cd node
./configure
The configure script told me I was missing g++ and openssl. The openssl one was a bit tricky since I needed openssl, libssl-dev and pkg-config installed.
sudo apt-get install g++
sudo apt-get install openssl
sudo apt-get install libssl-dev
sudo apt-get install pkg-config
When you make ./configure happy, its time to make.
make
Now make a coffee because it takes a little while to compile.
sudo make install
One more thing, felixge has created “a thin node.js idiom based module for CouchDB’s REST API that tries to stay close to the metal”. Lets make life easier and install that.
cd ~/node
git clone git://github.com/felixge/node-couchdb.git
cd /usr/local/lib/node
sudo ln -s ~/node/node-couchdb couchdb
If everything worked correctly, we now have CouchDB and Node.js installed along with the node-couchdb library.
Tests Node.js
Lets check that the installs worked. The command node will take you to its console. Try these commands.
var sys = require('sys');
sys.puts('hello world');
This should simply print ‘hello world’. Pretty easy. Now lets make sure that the node-couchdb library works.
couchdb = require('couchdb');
This should print out some JSON if everything works.
Test CouchDB
CouchDB is a RESTful so to test CouchDB, we need a web browser. Installed lynx which is a command line web browser.
sudo apt-get install lynx
Now navigate to the default CouchDB page.
lynx http://localhost:5984/
It should simply display welcome and the version number in JSON.
Test MongoDB
MongoDB has a nice console so lets use that.
mongo
db.foo.save( { test : 'hello world' } )
db.foo.find()
This should print out something along the lines of { “_id” : ObjectId(“4cb82f963af8744e5572e430″), “test” : “hello world” }
Simple Website
Create a file called example.js and put the following example from node.js into it:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8000, "");
console.log('Server running at publicly on port 8000');
Now start the server
node example.js
You can now navigate to http://locahost:8000 and it will write Hello World. Unless you didn’t install apache, you probably wont be able to bind to port 80.
Coming soon: Testing Node.js and CouchDB to its full potential.
double happiness symbol,
Nice post!!! Thank you very much for sharing.
Many thanks for your amazing publish, wonderful web page to! It makes be wish to get a weblog. What software do you need to get started? I hear a whole lot about this WordPress?!
To be a Newbie, I’m always searching online for articles that can assist me. Thanks a ton
pleasecan ithank you for adding me to your site,i have addedyou to mine,i will post here again in the next fewweeks
Howdy just wanted to give you a quick heads up. The words in your article seem to be running off the screen in Firefox. I’m not sure if this is a format issue or something to do with web browser compatibility but I figured I’d post to let you know. The layout look great though! Hope you get the issue resolved soon. Many thanksBirmingham Roofing Contractors, 3506 Inverness Landing, Birmingham, AL 35242 – (205) 301-2113