JavaScript Express Basics Parameters, Query Strings, and Modularizing Routes Randomize Cards

my solution

Card.pug

extends layout.pug

block content
    section#content 
        h2= text
        if hint
            p
                i Hint: #{hint}
        a(href=`/cards/${id}?side=${link}`) #{link}

cards.js

const express = require('express');
const router = express.Router();
const { data } = require('../data/flashcardData.json');
const { cards } = data;

router.get('/:id', (req, res) => {
    const {side} = req.query;
    const { id } = req.params;
    const text = cards[id][side];
    let templateData = { id, text};

    if( side === 'question' ){
        templateData.hint= cards[id].hint;
        templateData.link = 'answer';
    } else {
        templateData.link = 'question';
    }



    res.render('card', templateData);
});

module.exports = router;
Trevor Maltbie
seal-mask
.a{fill-rule:evenodd;}techdegree
Trevor Maltbie
Full Stack JavaScript Techdegree Student 14,298 Points

My understand of this deconstruction is that const templateData = {id, text}; is the same as

const templateData = {
id: id,
text: text
}

So, when we do templateData.link = 'answer'` is that like adding on to the above:

const templateData = {
id: id,
text: text
link: 'answer'
}

?

Simon Coates
Simon Coates
3,413 Points

For anyone's future reference, I skipped string interpolation and just fed a url in with res.locals.url = req.baseUrl+req.path+"?side=answer"; Seemed to work at a cursory glance. I'm not sure if it makes perfect sense, but I think avoiding hardcoding a path in the view might make sense if you alter the path parameters for the middleware.