0

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.

MattMS
  • 1,106
  • 1
  • 16
  • 32
  • Are you wanting to use a single _rewrite endpoint to access multiple databases (which is what it looks like here)? I'm not sure how PouchDB Server handles it, but CouchDB doesn't allow that by default. Otherwise, the key thing to remember is that all "to" values are relative to the design doc which contains the `rewrites` key, so `"to": "_show/index.html"` – BigBlueHat Mar 04 '16 at 20:34
  • I'm happy for the rewrite rules to be in separate databases. I really just need a full document example of a rewrite because I'm having no luck implementing the partial snippets shown in the documentation. – MattMS Mar 05 '16 at 07:21
  • Could you explain a bit more about what your trying to accomplish? It looks like your possibly overusing database divisions and show functions. Would love to know more so I can help better! :) – BigBlueHat Mar 05 '16 at 13:29
  • My goal is to serve a website (HTML) from CouchDB without another server, like Nginx, but still with nice URLs (no `_design` or `_show`). I figured I could use databases to define paths and use rewrites to put an `index.html` in each of them. Maybe I'm expecting too much of CouchDB? – MattMS Mar 06 '16 at 01:56
  • No. That's totally doable! And it's why the rewrites system is there in the first place. ^_^ I'll put more in an actual answer that you can "accept" (rashly assuming you'll like it...) ;) – BigBlueHat Mar 07 '16 at 22:07

1 Answers1

1

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!

BigBlueHat
  • 2,355
  • 25
  • 30
  • Awesome, thank you! I was missing the `vhosts` part, so it couldn't find the rewrite. I also had to upload the `index.html` instead of giving an `_attachments` object (I assume that's what you meant by faux json). Maybe worthwhile updating the answer with those requirements? – MattMS Mar 10 '16 at 04:28
  • My bad. You do need to base64 encode the value of `data` even when it's plain next: http://docs.couchdb.org/en/1.6.1/api/document/common.html#creating-multiple-attachments It seems you used Futon or Fauxton to upload--which is great!--it likely PUTs the file directly http://docs.couchdb.org/en/1.6.1/api/document/attachments.html#put--db-docid-attname – BigBlueHat Mar 10 '16 at 22:08
  • @MattMS, I updated the answer to match. Hope that clears up any confusion! Cheers. – BigBlueHat Mar 12 '16 at 16:19