diff --git a/lib/couch-proxy.js b/lib/couch-proxy.js index 793a790..c66eb40 100644 --- a/lib/couch-proxy.js +++ b/lib/couch-proxy.js @@ -12,6 +12,24 @@ var restify = require('restify'); var request = require('request'); var url = require('url'); +exports.proxy = function(options) { + options = options || {}; + + var couch = request.defaults({ auth: options.auth }); + + function proxy(req, res) { + console.log(req.method, url.resolve(options.url, req.url)); + req.pipe(couch(url.resolve(options.url, req.url), { + method: req.method, + headers: { + 'Content-Type': req.header('Content-Type', 'application/json') + } + })).pipe(res); + } + + return proxy; +}; + exports.listen = function(options, fn) { options = options || {}; options.port = options.port || 80; @@ -19,15 +37,17 @@ exports.listen = function(options, fn) { options.auth = options.auth || {}; var server = restify.createServer(); - var couch = request.defaults({ auth: options.auth }); - server.get('/', function(req, res) { - res.send('hello, I am a couch proxy! '); - }); + server.use(exports.proxy({ + url: options.url, + auth: options.auth + })); - server.get(/^\/couch\/(.*)$/, function(req, res) { - console.log('GET ' + req.params[0]); - couch.get(url.resolve(options.url, req.params[0])).pipe(res); + // hacky hack for the lack of `all` + ['del', 'get', 'head', 'opts', 'post', 'put', 'patch'].forEach(function(verb) { + server[verb]('.*', function(req, res) { + res.send('hello, I am a couch proxy!'); + }); }); server.listen(options.port, fn); diff --git a/package.json b/package.json index 7abce6c..3c38a9d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "couch-proxy-experiment", "description": "This is an experiment of how you can build a CouchDB proxy on top of restify.", - "version": "0.1.0-10", + "version": "0.1.0-11", "homepage": "https://github.com/null2/couch-proxy-experiment", "author": { "name": "Johannes J. Schmidt",