Skip to content

Instantly share code, notes, and snippets.

@davisp
Created November 5, 2010 23:45
Show Gist options
  • Save davisp/665045 to your computer and use it in GitHub Desktop.
Save davisp/665045 to your computer and use it in GitHub Desktop.
Configuring CouchDB (trunk) to have a node.js handler.
; Just drop this in /etc/couchdb/local.d/ and then
; start CouchDB normally.
; Here couch_node can be anything as long as it's uniq.
[os_daemons]
couch_node = /Users/davisp/tmp/couch-node.js
; This section can be named anything but you should
; probably make it follow the os_daemon name we
; used above so people realize they correlate.
[couch_node]
port = 8000
; _node is what will be used in the root of CouchDB's
; URL name space to access the handler. Eg:
; http://127.0.0.1:5984/_node in this case.
[httpd_global_handlers]
_node = {couch_httpd_proxy, handle_proxy_req, <<"http://127.0.0.1:8000">>}
#! /usr/bin/env node
// Remember to make this script executable with:
//
// chmod +x /path/to/couch-node.js
//
var http = require('http');
var sys = require('sys');
// Send a log message to be included in CouchDB's
// log files.
var log = function(mesg) {
console.log(JSON.stringify(["log", mesg]));
}
// The Node.js example HTTP server
var server = http.createServer(function (req, resp) {
resp.writeHead(200, {'Content-Type': 'text/plain'});
resp.end('Hello World\n');
log(req.method + " " + req.url);
})
// We use stdin in a couple ways. First, we
// listen for data that will be the requested
// port information. We also listen for it
// to close which indicates that CouchDB has
// exited and that means its time for us to
// exit as well.
var stdin = process.openStdin();
stdin.on('data', function(d) {
server.listen(parseInt(JSON.parse(d)));
});
stdin.on('end', function () {
process.exit(0);
});
// Send the request for the port to listen on.
console.log(JSON.stringify(["get", "couch_node", "port"]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment