tags:

views:

1254

answers:

5

I'm trying to use CouchDB with HTML/standalone REST architecture. That is, no other app server other than CouchDB and ajax style javascript calling CouchDB.

It looks like cross scripting is a problem. I was using Cloudkit/Tokyo Cabinet before and it seems like the needed callback function was screwing it up in the URL.

Now I'm trying CouchDB and getting the same problem.

Here are my questions:

1) Are these problems because the REST/JSON store like CouchDB or CloudKit is running on a different port from my web page? They're both run locally and called from "localhost".

2) Should I let CouchDB host my page and serve the HTML?

3) How do I do this? The documentation didnt seem so clear...

Thanks, Alex

+1  A: 

I can't help thinking you need some layer between the presentation layer (HTML) and the model (CouchDB).

That way you can mediate requests, provide additional facilities and functionality. At the moment you seem to be rendering persisted objects direct to the presentation layer, and you'll have no facility to change or extend the behaviour of your system going forward.

Adopting a model-view-controller architecture will insulate your model from the presentation layer and give you some flexibility going forwards.

(I confess I can't advise on your cross-site-scripting issues)

Brian Agnew
Brian- that's actually the point of this architecture. I think with Ajax, the controller layer starts to move client side. The controller (and work of updating the view) are done in javascript on the page. So it makes REST/JSON calls to the model.Done completely, there does need to be a authorization/security piece on the model side, but I'm prototyping stuff now.Anyone know about the HTML in CouchDB pience?
alxross
+1  A: 

I think one way is thorugh mod_proxy in Apache. It forward the request from Apache to Couchdb so may solve the cross scripting issue.

# Configuration file for proxy
ProxyVia ON

ProxyPass /couchdb http://<<couchdb host>>:5984/sampleDB
ProxyPassReverse /couchdb http://<<couchdb host>>:5984/sampleDB
Andric
Aha- thanks. That's a good idea to help with the cross scripting. Do you (or any one else) know how to get CouchDB to serve the HTML. It's supposed to be able to do that and I've seen references to doing it "in the design doc" but I haven't been able to see exactly how...
alxross
You can just have a look at "couchapps" in the open. Eg. http://github.com/jchris/toast/ is very small and understandable. Set up couchapp and execute a `couchapp push .`. Essentially, you just upload your HTML as an attachment for the design doc, and then hit http://127.0.0.1:5984/toast/_design/toast/index.html.
karmi
+3  A: 

There are huge advantages to having CouchDB serve/generate your HTML.

For one thing, the pages (which are HTTP resources) are tied to the data or to the queries on the data and CouchDB knows when to update the etag when the page has changed. This means that if you stick nginx in front of CouchDB and say "cache stuff" you get all the free caching you would normally need to build yourself.

I would push for nginx > apache in front of CouchDB because Apache isn't all that great at handling concurrent connections and nginx + erlang (CouchDB) are great at it.

Also, you can write these views in JavaScript which are documented well in the CouchDB book http://books.couchdb.org/relax/ or in Python using my view server http://github.com/mikeal/couchdb-pythonviews which isn't really documented at all yet but I'll be getting to it soon :)

I hope that view servers in other languages start implementing the new features in the view server protocol as well so that anyone can write standalone apps in CouchDB.

mikeal
The problem with this plan, though, is that aparently nginx doesn't support Etag; only modified date, etc. http://markmail.org/message/4q3umqpvz5ngunw2 and http://nginx.org/pipermail/nginx/2009-April/011382.html
Taxilian
yeah, lately i've seen a lot more complaints in general about nginx as an rproxy so i've been more interested lately in solutions that use Varnish and other caching specific rproxies.
mikeal
+6  A: 

There is a simple answer: store static HTML as attachments to CouchDB documents. That way you can serve the HTML directly from the CouchDB.

There is a command-line tool to help you do this, called CouchApp

The book Mikeal linked to also has a chapter (Managing Design Documents) on how to use CouchApp to do this.

J Chris A
+3  A: 

3) you can use CouchDB shows to generate HTML (or any content type)

mykhal