Skip to Main Content
CouchDB: The Definitive Guide
book

CouchDB: The Definitive Guide

by J. Chris Anderson, Jan Lehnardt, Noah Slater
January 2010
Intermediate to advanced content levelIntermediate to advanced
272 pages
6h 41m
English
O'Reilly Media, Inc.
Content preview from CouchDB: The Definitive Guide

Chapter 13. Showing Documents in Custom Formats

CouchDB’s show functions are a RESTful API inspired by a similar feature in Lotus Notes. In a nutshell, they allow you to serve documents to clients, in any format you choose.

A show function builds an HTTP response with any Content-Type, based on a stored JSON document. For Sofa, we’ll use them to show the blog post permalink pages. This will ensure that these pages are indexable by search engines, as well as make the pages more accessible. Sofa’s show function displays each blog post as an HTML page, with links to stylesheets and other assets, which are stored as attachments to Sofa’s design document.

Hey, this is great—we’ve rendered a blog post! See Figure 13-1.

A rendered post

Figure 13-1. A rendered post

The complete show function and template will render a static, cacheable resource that does not depend on details about the current user or anything else aside from the requested document and Content-Type. Generating HTML from a show function will not cause any side effects in the database, which has positive implications for building simple scalable applications.

Rendering Documents with Show Functions

Let’s look at the source code. The first thing we’ll see is the JavaScript function body, which is very simple—it simply runs a template function to generate the HTML page. Let’s break it down:

function(doc, req) {
  // !json templates.post
  // !json blog ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Getting Started with CouchDB

Getting Started with CouchDB

MC Brown
Scaling CouchDB

Scaling CouchDB

Bradley Holt
Cloud Native Go

Cloud Native Go

Matthew A. Titmus

Publisher Resources

ISBN: 9780596158156Errata Page