Skip to content

Commit

Permalink
full proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
jo committed Apr 30, 2013
1 parent 1ae50b8 commit 5236af8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 27 additions & 7 deletions lib/couch-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,42 @@ 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;
options.url = options.url || 'http://localhost:5984';
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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 5236af8

Please sign in to comment.