If it is possible, how would I achieve the following URL rewrites using PouchDB Server?
At /index.html
, display the HTML output of /index/_design/index/_show/index.html
.
At /my_database/index.html
, display /my_database/_design/my_database/_show/index.html
.
My aim is to use PouchDB (and eventually CouchDB) as a stand-alone web server.
I am struggling to translate the rewrite documentation into working code.
Apache CouchDB uses an HTTP API and (consequently) can be used as a static web server--similar to Nginx or Apache HTTPD, but with the added bonus that you can also use MapReduce views, replication, and the other bits that make up Apache CouchDB.
Given just the core API you could store an entire static site as attachments on a single JSON document and serve each file from it's own URL. If that single document is a _design
document, then you get the added value of the rewriter.
Here's an example faux JSON document that would do just that:
{
"_id": "_design/site",
"_attachments": {
"index.html": {
"content_type": "text/html",
"data": "..."
},
"images/logo.png": {
"content_type": "image/png",
"data": "..."
},
"rewrites": [
{
"from": "/",
"to": "index.html"
}
]
}
The actual value of the "data": "..."
would be the base64 encoded version of the file. See the Creating Multiple Attachments example in the CouchDB Docs.
You can also use an admin UI for CouchDB such as Futon or Fauxton--available at http://localhost:5984/_utils
--both of which offer file upload features. However, those systems will require that the JSON document exist first and will PUT
the attachment into the database directly.
Once that's completed, you can then setup a virtual host entry in CouchDB (or Cloudant) which points to the _rewrite
endpoint within that design document. Like so:
[vhosts]
example.com = /example-com/_design/site/_rewrite/
If you're not hosting on port 80, then you'll need to request the site at http://example.com:5984/
.
Using a _show
function (as in your example) is only necessary if you're wanting to transform the JSON into HTML (or different JSON, XML, CSV, etc). If you only want static hosting, then the option above works fabulously. ^_^
There are also great tools for creating these documents. couchapp.py and couchdb-push are the ones I use most often and both support the CouchApp filesystem mapping "spec".
Hope that helps!