J
?
+

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!

Hide
blog comments powered by Disqus

About This Blog

Just your average hacker blog… with a twist. Why go to great lengths to decorate each article when I'm just talking about code? Simply because epic open–source projects demand that one talk about them with a matching scale of epicness. Or at least they should, and that's my aim here.

To those curious about me, apart from being a web developer and designer, I'm also a vampire. For the stalkers, Fail Whale calls me kuroi_kenshi, Octocat calls me hostsamurai and mere–mortals call me Lou.

Creative Commons License

Contact Me