NodeJS And Couchbase

couchbase_node
The first thing you got to do is download and install Couchbase for your OS. 

Navigate to http://www.couchbase.com/download and download Couchbase for Mac OSX.
The install is very straight forward and easy to do and well documented.

Next we need to download NodeJS for Mac OSX. The link is http://nodejs.org/download/ and the file I downloaded was node-v0.10.32.dmg.
Double click on this file to start the installer and follow the instructions.
The install takes up 42MB on disk and installs Node is installed the following locations:

/usr/local/bin/node

node package manager was installed at

/usr/local/bin/npm

You have to make sure sure that /usr/local/bin is in your $PATH.

To do this we need to edit the bash_profile on my Mac.
This is quite simple but I will not assume that you know vi (because I barely do) and will do this step by step.

Open a terminal window on your Mac. This can be found by opening Finder > Applications > Utilities > Terminal

Type the following at the command prompt.

Shell>> vi ~/.bash_profile

This will open up the vi editor.

Press the “i” key to enter insert mode and add the following lines to the file.

PATH="/usr/local/bin:{PATH}"
export PATH

We now want to exit vi. To do this press the “esc” once to exit insert mode. The type “:wq” which tells vi the write and quit. This saves the files and returns you to the command prompt.
Now type node –version at the command prompt to see which version of node we have installed.
In this case id was v0.10.32. This also tells us that our Node installation was successful.

The last thing we need to install is the NodeJS SDK for Couchbase.
This can be found at http://www.couchbase.com/communities/all-client-libraries and is currently at version 1.2.
The NodeJS SDK uses and builds on the Couchbase C SDK, so as a result that needs to be loaded first. This is very straightforward as it is a Homebrew recipe.
(If you have not already done so, install Homebrew from http://brew.sh and follow the instructions.) At the command line type in the following:

Shell>> brew install libcouchbase

Now we have the base library installed we need to build on this with the NodeJS library. This is where I ran into my first problem, or deviation from the documented instructions provided. When I ran the command I got a npm install couchbase lot of errors back.

Shell>> npm install couchbase
npm ERR! Error: EACCES, mkdir '/Library/Frameworks/Python.framework/node_modules'
npm ERR! { [Error: EACCES, mkdir '/Library/Frameworks/Python.framework/node_modules']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/Library/Frameworks/Python.framework/node_modules',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/Library/Frameworks/Python.framework/node_modules/couchbase',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack: 
npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/writer.js:171:23',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:46:53',
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

It seems that this install needs to be run as the root user.
To do this we need to log on as root by entering sudo -s at the console.
You will be required to enter the root password, this is the same as the administrator password on your Mac.

Shell>> sudo -s
Password:
bash-3.2# npm install couchbase
/
> couchbase@2.0.0 install /Library/Frameworks/Python.framework/node_modules/couchbase
> (node-gyp rebuild 2> builderror.log) || (exit 0)
COPY ../deps/lcb/include/libcouchbase/libuv_io_opts.h
 COPY ../deps/lcb/include/libcouchbase/plugins/io/libuv/plugin-libuv.c
 ...
 ...
 SOLINK_MODULE(target) Release/couchbase_impl.node
 SOLINK_MODULE(target) Release/couchbase_impl.node: Finished
couchbase@2.0.0 node_modules/couchbase
├── bindings@1.0.0
├── jsonparse@0.0.6
├── nan@1.3.0
└── request@2.39.0 (json-stringify-safe@5.0.0, forever-agent@0.5.2, aws-sign2@0.5.0, qs@0.6.6, oauth-sign@0.3.0, stringstream@0.0.4, tunnel-agent@0.4.0, node-uuid@1.4.1, mime-types@1.0.2, tough-cookie@0.12.1, http-signature@0.10.0, hawk@1.1.1, form-data@0.1.4)

Exit from the root bash session by typing exit

We now have all the software components we need so we are now ready to write a bit of code.

What we will do is access data in a data bucket called beer-sample. This is a default data set that comes with Couchbase.

Create a directory called MyNodeApps. Open a file called MyFirstApp.js using your favourite editor. I will be using vi.

Shell>> mkdir MyNodeApps
Shell>> cd MyNodeApps
Shell>> vi MyFirstApp.js

Enter the following into your editor and save the file.

var http = require("http");
// load the Couchbase driver and connect to the cluster
var couchbase = require('couchbase');
var cluster = new couchbase.Cluster();
var bucket = cluster.openBucket('beer-sample');
//get a record from the database and output the results
bucket.get('new_holland_brewing_company-sundog', function(err, result) {
console.log(result);
 });

To run the file type the following on the command prompt

Shell>> node MyFirstApp.js
{ cas: { '0': 50331648, '1': 2687572239 },
 value: 
 { name: 'Sundog',
 abv: 5.25,
 ibu: 0,
 srm: 0,
 upc: 0,
 type: 'beer',
 brewery_id: 'new_holland_brewing_company',
 updated: '2010-07-22 20:00:20',
 description: 'Sundog is an amber ale as deep as the copper glow of a Lake Michigan sunset. Its biscuit malt give Sundog a toasty character and a subtle malty sweetness. Sundog brings out the best in grilled foods, caramelized onions, nutty cheese, barbecue, or your favorite pizza.',
 style: 'American-Style Amber/Red Ale',
 category: 'North American Ale' } }

You have now created a simple NodeJS application that connects to Couchbase and retrieve a record from the database.

  • Ask Question