Created
February 6, 2015 03:18
-
-
Save CaptainYarb/4cd7f99072d327b2c092 to your computer and use it in GitHub Desktop.
RethinkDB Auto Connect: Express Middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(app){ | |
app.express.use(function(req, res, next){ | |
// initialise rethink connection | |
if(!app.rethinkdb){ | |
return next(); | |
} | |
app.rethinkdb.connect(function(err, r, conn, rethinkNext){ | |
if(err){ | |
return res.error('DB Error', err).end(); | |
} | |
req.on('end', function(){ | |
rethinkNext(); | |
}); | |
req.rethinkdb = { | |
r: r, | |
conn: conn, | |
next: rethinkNext | |
}; | |
return next(); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment