Skip to content

Instantly share code, notes, and snippets.

@sivy
Created October 18, 2010 14:49
Show Gist options
  • Save sivy/632325 to your computer and use it in GitHub Desktop.
Save sivy/632325 to your computer and use it in GitHub Desktop.
// var favProvider = new FavProvider('localhost', 27017);
var favProvider = new FavProvider();
var userProvider = new UserProvider();
function loadUser(req, res, next) {
console.log('loadUser!');
var name = 'sivy';
// You would fetch your user from the db
userProvider.findByName(name, function(error, user){
if (user) {
console.log(sys.inspect(user));
req.user = user;
next();
} else {
console.log('error!');
next(new Error('Failed to load user ' + name));
}
});
}
// Routes
app.get('/', loadUser, function(req, res){
console.log(sys.inspect(req.user));
favProvider.findAll( function (error, faves) {
var ctx = { title: "Faves!", faves: faves };
loader.load('index.html', function (error, t) {
t.render(ctx, function (error, result) {
res.send(result);
res.end();
});
});
});
});
# request to localhost:3000/
loadUser!
{ username: 'sivy'
, password_hash: '098f6bcd4621d373cade4e832627b4f6'
, reminder: 'test'
, _id: 1
, dateCreated: Mon, 18 Oct 2010 05:22:46 GMT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment