Setting up the project

You need to set up your directory layout and add some views in the server before you can start working with the Node.js SDK and express+jade itself.

Creating the project directory layout

Create a project directory named beer and subdirectories for the views and other components:

$ mkdir beer 
$ cd beer 
$ mkdir views 
$ mkdir views/beer 
$ mkdir views/brewery 
$ mkdir static 
$ mkdir static/js 
$ mkdir static/css

To verify the directory structure you just created, you can list all the subdirectories in the project directory:

$ find . -type d
./static
./static/js
./static/css
./views
./views/brewery
./views/beer

To make the application look pretty, it incorporates jQuery and Twitter Bootstrap. You can either download the libraries and put them in their appropriate css and js directories (under static), or clone the project repository and use it from there. If you followed the quick start steps, you already have the files in your beersample-node directory. Either way, make sure you have the following files in place:

  • static/css/beersample.css
  • static/css/bootstrap.min.css (the minified Twitter Bootstrap library)
  • static/css/bootstrap-responsive.min.css (the minified responsive layout classes from Bootstrap)
  • static/js/beersample.js
  • static/js/jquery.min.js (the jQuery JavaScript library)

At this point you have a bare bones web application configured that has all the dependencies included.

Setting up the Views

The beer-sample bucket comes with a small set of predefined views, but the application requires some additional views. This is also a good chance to explore the view management possibilities available through the Couchbase Server administration console.

The application lists beers and breweries by name, so it also needs predefined views for those types of documents. Open the administration console and click Views. Select beer-sample from the drop-down list to switch to the correct bucket. Now click on Development Views and then Create Development View to define your first view. You need to give it the name of both the design document and the actual view. Insert the following names:

  • Design Document Name: _design/dev_beer

  • View Name: by_name

Next, define the map and optional reduce functions. This example doesn't use reduce functions at all, but you can play around with a reduce function to see what happens. Insert the following map function (that’s JavaScript) and click Save.

function (doc, meta) {
  if(doc.type && doc.type == "beer") {
    emit(doc.name, null);
  }
}

Every map function takes the full document, doc, and its associated metadata, meta, as the arguments. Your code can inspect the data in these arguments and emit a result when you want to have it in your index. This map function emits the name of the beer (doc.name) when the document both has a type field and the type is beer. In this case, a value is not needed, so the function just emits null. It’s always advisable to keep the index as small as possible. Resist the urge to include the full document by using emit(meta.id, doc) because it increases the size of your view indexes. If you need to access the full document, you can call db.get(result.key) to get the individual document for a single row. The resulting retrieval of the document might be slightly out of sync with your view, but it will be fast and efficient.

Now you need to define a view for the breweries. You already know how to do this—here is all the information you need to create a brewery view:

  • Design Document Name: _design/dev_brewery

  • View Name: by_name

  • Map Function:

    function (doc, meta) {
      if(doc.type && doc.type == "brewery") {
        emit(doc.name, null);
      }
    }

The final step is to push the design documents in production. While the design documents are in development, the index is applied only to a deterministic subset of the data. Because you want to have the index on the whole data set, click the Publish button on both design documents (and accept any pop-up windows that warn you about overriding the old design documents).