Skip to content

Instantly share code, notes, and snippets.

@MetaThis
Created August 25, 2011 22:03
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MetaThis/1172108 to your computer and use it in GitHub Desktop.
Save MetaThis/1172108 to your computer and use it in GitHub Desktop.
Simple example of a Node.js proxy to CouchDB GET requests
var http = require('http'),
request = require('request'), // request module from https://github.com/mikeal/request
url = require('url');
http.createServer(function (req, res) {
var href = url.parse(req.url,true).href;
request('http://127.0.0.1:5984' + href).pipe(res);
}).listen(1337);
// now try something like http://127.0.0.1:1337/your_db_name/_all_docs/?limit=10
@rhyolight
Copy link

Is there any way we can make this more complicated? It is too easy for me.

@thinkt4nk
Copy link

++more complicated

@MetaThis
Copy link
Author

@rhyolight - Sure...rewrite it in Java. ;)

@mikeal
Copy link

mikeal commented Aug 25, 2011

actually, you can pipe the req to request and then it'll handle puts and cache headers properly :)

@mvolkmann
Copy link

Even simpler ... well, less noisy, if you write it in CoffeeScript.

@mikeal
Copy link

mikeal commented Aug 25, 2011

@mvolkmann yeah, this is too simple, let's add a compiler to it :P

@isaacs
Copy link

isaacs commented Aug 25, 2011

TOO VERBOSE AND LONG!!

http.createServer(function (req, res) {
  request(url.resolve('http://127.0.0.1:5984', req.url)).pipe(res);
}).listen(1337);

@mikeal
Copy link

mikeal commented Aug 25, 2011

in 0.5.x you can do

http.createServer(function (req, res) {
  req.pipe(request(url.resolve('http://127.0.0.1:5984', req.url))).pipe(res)
}).listen(1337)

@mvolkmann
Copy link

I love JavaScript, but after using CoffeeScript for the past two weeks, JavaScript just looks so noisy. I don't have to worry about compiling though. I have "coffee -cwo ..." running so all the files get compiled in the background. BTW mikeal, thanks so much for everything you've done for the Node community! I use your modules often!

@MetaThis
Copy link
Author

@isaacs @mikeal Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment