Now I introduce how to use Javascript and CouchDB to develop traditional C/S application.
At first, we install couchdb and apache http server on computer. Because it uses cross-domain request, and the couchdb's http response header cannot be modified (I use couchbase and don't know where to modify http headers), I use apache http server to proxy couchdb.
Secondly, add the below code to httpd.conf.
<VirtualHost *:80>
<Proxy http://localhost:5984>
Order deny, allow
Allow from all
</Proxy>
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^OPTIONS
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers Content-Type
Header set Access-Control-Allow-Methods "GET, POST, PUT, OPTIONS, DELETE"
Header set Access-Control-Max-Age 3600
ProxyRequests off
ProxyPreserveHost Off
AllowEncodeSlashes On
KeepAlive Off
ProxyPass / http://localhost:5984 nocanon
ProxyPassReverse / http://localhost:5984
</VirtualHost>
Now start your httpd, and visit http://localhost/_utils to create a database named "test".
And third, create a test.html and import jquery script and add the below code to create a document in couchdb.
<script>
$.ajax({
url: 'http://localhost/test',
type: 'POST',
contentType: 'application/json',
data: '{"firstname": "hello", "lastname": "world"}'
});
</script>
And all are done. Open your test.html via file protocol with browser, and then see the created document in couchdb. If all are ok, you could write any ui applications and run it to access couchdb database directly without deploying your application on any web server.
Enjoy it.
No comments:
Post a Comment