CouchBase provides full standalone community build, best option if you don’t want to deal with building and deploying on your Mac. Pairing that up with XAMPP and you’ll be able to write the client side that communicates to the apache web server which then proxies to CouchDB. After installing XAMPP two files need to be edited to ease the local deployment.
[sourcecode]
XAMPP/etc/http.conf - Add the ProxyPass configuration.
XAMPP/etc/extra/httpd-userdir.conf - Add the user directory to point to the default dart folder location.
[/sourcecode]
Fire up CouchDB and Apache, then open chrome http://127.0.0.1/couchdb and you should see the following.
From here we can use XMLHttpRequest provided in the dart:html library to make requests to the Apache web server which proxy to CouchDB. One issue I ran into was DOMException INVALID_STATE_ERR when trying to set the headers. You must call open() before your able to call setRequestHeader().
[sourcecode lang=”java”]
// Basic http methods
getHttp(var uri) {
XMLHttpRequest c = connect();
c.open(‘GET’, uri, false);
c.setRequestHeader(‘Accept’, ‘application/json’);
c.send();
return c.responseText;
}
postHttp(var uri, var body) {
XMLHttpRequest c = connect();
c.open(‘POST’, uri, false);
c.setRequestHeader(‘Content-type’, ‘application/json’);
c.send(body);
return c.responseText;
}
putHttp(var uri, [var body]) {
XMLHttpRequest c = connect();
c.open(‘PUT’, uri, false);
if (body is String && body.length > 0) {
c.setRequestHeader(‘Content-type’, ‘application/json’);
c.send(body);
} else {
c.send();
}
return c.responseText;
}
deleteHttp(var uri) {
XMLHttpRequest c = connect();
c.open(‘DELETE’, uri, false);
c.send();
return c.responseText;
}
[/sourcecode]
From there stubbing out a simple interface for dart with default implementation was easy.
The CouchDBWrapperImpl does wrapping for calling to the CouchDB using the default keyword helps to hide the implementation from the interface in a factory type way.
Opening up chrome to http://127.0.0.1/<username> provides access into the users dart directory.
Clicking on CouchDBExample.html should run the sample code.
The interfacing with CouchDB was simple and quick, a more elegant solution would be a full client implementation that lets you modify objects more indirectly. Sample code on github.