Created
February 18, 2012 16:34
-
-
Save klaemo/1860089 to your computer and use it in GitHub Desktop.
express route conditional response
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
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 }); | |
} | |
}); |
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
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
how could this be accomplished in express 3.0?