Last Updated: February 25, 2016
· jelle

Couchapp + Vhost = How to access API!?

Building a couchapp which you want to make accesible through a vhost(custom url)? You'll be obviously doing this by making a _rewrite document and pointing your vhost to it.

But! Wait! How am I able to access the CouchDb API now, now I'm not on the same (sub)domain anymore? How will I access the API and not interfere with the cross-domain policy?

Here's the trick:
Set 'secure_rewrites' to false* in your Futon configuration settings, it's filed under 'httpd'. It's set to true by default. Default on Cloudant is false, Iriscouch says true.

Now set your _rewrite doc to path traverse up to the root. An example:

  { // rewriting / to index.html
  { // rewriting /api to root api
    "from": "api/*",
    "to":   "../../../*" // make sure it's the right depth

Now send your couchapp's http request to /api and your done!

*Warning: You're making the whole database public through this settings. Make sure the security permissions of all databases are set right!