Skip to content

Instantly share code, notes, and snippets.

@xjamundx
Created April 11, 2011 13:22
Show Gist options
  • Save xjamundx/913506 to your computer and use it in GitHub Desktop.
Save xjamundx/913506 to your computer and use it in GitHub Desktop.
Express JS Locals
// using the res.local syntax
app.all('*', function(req, res, next) {
db.logs.findOne(function(err, logs) {
if (err) return console.log("Error: ", err);
res.local('count', logs.count);
res.local('edit', false);
next();
});
});
// alternatively the res.locals syntax
app.all('*', function(req, res, next) {
db.logs.findOne(function(err, logs) {
if (err) return console.log("Error: ", err);
res.locals({
'count': logs.count,
'edit': false
});
next();
});
});
// finally, as the second argument to a render call
app.get('/reviews/add', function(req, res) {
res.render('view.html', {
review: {
rating: 0,
title: "This is the place.",
article: "This is your review.",
images: [],
slug: "your-url"
}
});
});
// in the first two cases you can confirm which variables
// have been set simply by inspect the _locals variable
console.log("Locals: ", res._locals);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment