We have a new wiki. The migration is not 100% complete. You can help out by moving pages across. This wiki will exist for as long as there are pages left.

The official documentation has moved to http://docs.couchdb.org — The transition is not 100% complete, but http://docs.couchdb.org should be seen as having the latest info. In some cases, the wiki still has some more or older info on certain topics inside CouchDB.

You need to be added to the ContributorsGroup to edit the wiki. But don't worry! Just email any Mailing List or grab us on IRC and let us know your user name.

Advanced Shows and Lists: Throwing Redirects

Throw a 404 error

To throw a 404 from inside a _show or _list func .. the easiest way is:

That will be caught by the top level loop thing and turned into a nice response.

Return a redirect

There's no top level catcher thing for redirects, so you can't *throw a redirect*, you have to 'return' it.

To do a redirect, there's a library function that will do it for you:

You can use the path lib to help get some neat urls, have a look at vendor/couchapp/lib/path.js source ..

What this actually does is the equivalent of:

The 'code' is the http response code, the http response would be something like:

Case study, Making a Redirect Throwable

If you're deep in a lib/mystuff.js func, I use this sort of style for a certain checker function.

In this example I have different product lines in my rewrites, of the form:

The trouble is if someone uses the wrong url for the type of product, I want to put them right, this will help keep the search engine optimization good too. So if someone goes:

It will call the _show/camera method with the id of a document that has {type: 'dvr'}. Which is bad because I use a different template but also bad because SEO will freak out if it ends up in google.

To fix it I did the following:

In my _show or _list func I put:

I know people will yell at me for puting too much on one line. But I'm using this in all my _show and _list funcs, I want it to be nice and short.

In my *lib/rewritehelper.js* file I check the type and all that and if it's wrong I throw an exception, which the _show func catches and returns as is. Here's the full code for my lib function so far:

So the cool thing here is the 'throw' statements, if we're totally in the wrong place, I want to get out ASAP.

Advanced_Shows_and_Lists_Throwing_Redirects (last edited 2011-11-23 15:11:44 by 62)