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 Randomize Cards

Logic in routes Vs in templates?

I found it was much easier to implement login in the pug. But I feel that this is not good long term? My code :

//card.pug
block content
    section#content
        h2= text
        if side ==  'question' 
            -const url = `${id}?side=answer`;
            a(href= url) Answer
            if hint
                p 
                    i Hint: #{hint}
        else 
            -const url = `${id}?side=question`;
            a(href= url) Question
...
router.get('/:id', (req, res) => {
const { side } = req.query;
const { id } = req.params;
const text = cards[id][side];
const { hint } = cards[id];

const templateData = { text, side, hint, id };
console.log(templateData)
    res.render('card', templateData);
});
...

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hi Thomas Karageorgiadis,

Nice solution you got here! As always with coding there's a whole lot of different solutions to a certain challenge. I think you've found another great way of handling this. As for your question regarding if it'd be good long term I don't see to much of a problem and think it's mostly a matter of personal preference.

The only thing you'll want to keep in mind is the scalability of your app. If this template would become much larger or the logic becomes more complicated because of some new features for example your app might be easier to maintain when the logic happens in the route.

Hope this answers your question, keep up the awesome work coder! 🔥