Setting up PouchDB with CouchDB

PouchDB is an awesome little library that lets you replicate your local database to other remote databases like CouchDB. Because of Access-control-allow-origin, setting up a locally hosted project isn’t immediately obvious. I’ll give a walkthrough on how to setup PouchDB with CouchDB in the simplest terms possible.

First you need to install CouchDB, which is pretty straightforward. Just download the binary for windows here, or if you’re on a mac use this guide. Now run:

sudo -i -u root couchdb -b

Or for windows go to the CouchDB/bin folder and run couchdb.bat

And you should see a welcoming message telling you that it’s time to relax (it’s not time to relax yet). Once you get CouchDB setup, you’ll need a CORS proxy to allow access to your CouchDB database from a different domain. The easiest and simplest one I found is a node package called corsproxy. Simply run:

npm install -g corsproxy

And now you have your corsproxy. Run it by typing corsproxy. Alright, you have all the necessary requirements for setting up PouchDB and CouchDB replication. Normally in your javascript file you’d replicate like this:

Pouch.replicate('idb://myapp', 'http://localhost:5984/myapp', myCallback)

Where 'idb://myapp' is your local database and 'http://localhost:5984' is your CouchDB database. However since your app is being hosted on a different port you will get a Access-control-allow-origin error. Change that remote database to now point to the corsproxy you setup which has a default port of 9292:

Pouch.replicate('idb://myapp', 'http://localhost:9292/localhost:5984/myapp', myCallback)

Kazam! There you go, replicating PouchDB to/from a remote CouchDB! Now it’s time to relax.

About these ads

3 thoughts on “Setting up PouchDB with CouchDB

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s