Skip to content

Instantly share code, notes, and snippets.

@toddbluhm
Created July 11, 2013 06:09
Show Gist options
  • Select an option

  • Save toddbluhm/5972922 to your computer and use it in GitHub Desktop.

Select an option

Save toddbluhm/5972922 to your computer and use it in GitHub Desktop.

Revisions

  1. toddbluhm created this gist Jul 11, 2013.
    8 changes: 8 additions & 0 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    var forward = require('./middleware/forward.js'); //reverse proxy
    var nano = require('connect-nano');

    // instantiate `app` et al

    //After app.use(express.cookieParser());
    app.use(nano('https://username.cloudant.com')),
    app.use(forward(/\/db\/(.*)/));
    16 changes: 16 additions & 0 deletions forward.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    //npm connect-nano must be installed
    var nano = require('connect-nano');

    module.exports = function (pattern, host) {
    return function (req, res, next) {
    if(req.url.match(pattern)) {
    var db_url = req.url.match(pattern)[1]
    , db = db_url.split('/')[0]
    , db_path = db_url.replace(db,'').substr(1);
    console.log(db_url);
    req.pipe(req.nano.request({db:db, path:db_path, method:req.method.toLowerCase()})).pipe(res);
    } else {
    next();
    }
    };
    };