Skip to content

Instantly share code, notes, and snippets.

@klaemo
Created February 18, 2012 16:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klaemo/1860089 to your computer and use it in GitHub Desktop.
Save klaemo/1860089 to your computer and use it in GitHub Desktop.
express route conditional response
app.get('/users', function(req, res){
if (req.xhr) {
// respond with the each user in the collection
// passed to the "user" view
res.partial('user', users);
} else {
// respond with layout, and users page
// which internally does partial('user', users)
// along with other UI
res.render('users', { users: users });
}
});
@klaemo
Copy link
Author

klaemo commented Feb 18, 2012

how could this be accomplished in express 3.0?

@tj
Copy link

tj commented Feb 18, 2012

you would rely on features of the template engine you're using, and passing an additional local variable. ex maybe res.render('users', { users: users, partial: req.xhr }) and then do what you need to in the template, or simply render a "user" template and have "users" utilize that

@klaemo
Copy link
Author

klaemo commented Feb 18, 2012

yep, that works :) thank you for the quick response! coulda thought of that myself, though...

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