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 trialcodingchewie
8,764 PointsHow to solve all `/cards` possible bad route requests?
In the section videos Andrew mentioned error handling for bad requests and I was curious to know how to handle all bad route request for /cards
if this is the appropriate approach:
router.get('/:id', (req, res) => {
const { side } = req.query
let { id } = req.params
if (typeof id !== 'number') id = randomizer(cards)
if (!side) return res.redirect(`/cards/${id}?side=question`)
const name = req.cookies.username
const text = cards[id][side]
const { hint } = cards[id]
const templateData = { id, text, name }
if (side === 'question') {
templateData.hint = hint
templateData.sideToShow = 'answer'
}
if (side === 'answer') templateData.sideToShow = 'question'
res.render('card', templateData)
})
function randomizer(cards) {
const numberOfCards = cards.length
return Math.floor(Math.random() * numberOfCards)
}