Couchapp Quickie: Sending Email(again) with Pretty URLs
Recently, I ran into a little snag while trying to send emails using the new externals API in CouchDB 1.1.×. The old API made that task somewhat simple, from a pretty URL point-of-view. All you had to do was write a rewrite rule like the following:
{"from": "/contact", "to": "../../_mailer", "method": "POST"}
However, doing the same using the new API would require you to rewrite that rule as:
{"from": "/contact", "to": "../../../_mailer", "method": "POST"}
See what I did there? Unless you have the secure rewrites option set to “false”, this will throw an error. If turning off secure rewrites is not an option for you, don’t fret, there’s a better solution.
Attack of the Virtual Hosts
Remember setting up the vhost rule in the article about sending emails? We’ll be taking that a step further by setting a rule that links /contact to your external process. First set up your external process:
[os_daemons]
mailer = /usr/bin/node /etc/couchdb/local.d/async_mailer/mailer.js
[mailer]
port = 9000
[httpd_global_handlers]
_mailer = {couch_httpd_proxy, handle_proxy_req, <<"http://127.0.0.1:9000">>}
And now for the vhost rule:
[vhosts] my_site.com = /db/_design/db/_rewrite/ my_site.com/contact = /_mailer
Restart CouchDB and try sending out an email. Enjoy!