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

Sean Paulson
Sean Paulson
5,047 Points

My solution using pugjs

So I'm sure I'm ignorant but his solution seems a bit over engineered. Anyway check out line 12 of cards.js thats the main difference. let me know what you think. Would this break easy if a client told me to change something?


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];
    const { hint } = cards[id];
    //sets showside to the opposite of side. 
    let showside = side === "question" ? "answer" : "question";

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

module.exports = router;


extends layout.pug

block content
    h2= text
    if side == "question"
        i Hint: #{hint}
    a(href=`http://localhost:3000/cards/${id}?side=${showside}`) #{side}