Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics Parameters, Query Strings, and Modularizing Routes Card Template

Brendan Moran
Brendan Moran
14,052 Points

Why not use res.locals?

Why not use res.locals instead of declaring all those variables, and putting them into a new object literal, which is yet another variable assignment, to then pass in to res.render? It seems more concise and readable to me to use res.locals and assign things in there. Not a rhetorical question, really wondering if there is some benefit to doing it this way or if it is just down to programmer preference.

My code:

router.get('/:id', (req, res) => {
  const { id } = req.params;
  const { side } = req.query; //will refer to question or answer in card data

  res.locals = {
    text: cards[id][side],
    hint: cards[id].hint,
  };
  res.render('card');
});

Personally, this makes a lot more sense to me, unless there is something objectively better about the way Andrew is doing it that I should know about?