<commit_before>const express = require('express')
const router = express.Router()
const { data } = require('../data/cards.json')
const { cards } = data

router.get('/:id', (req, res) => {
  const name = req.cookies.username
  const { id } = req.params
  const { side } = req.query
  const text = cards[id][side] // this combines the id param query (question or answer) to set value of text to Q or A.
  const { hint } = cards[id]
  const templateData = { id, text }

  if (name) {
    if (side === 'question') {
      templateData.hint = hint
      templateData.sideToShow = 'answer'
      templateData.sideToShowDisplay = 'Answer'
    } else if (side === 'answer') {
      templateData.sideToShow = 'question'
      templateData.sideToShowDisplay = 'Question'
    }
    res.render('card', templateData)
  } else {
    res.redirect('/hello')
  }
})

module.exports = router
<commit_msg>handle random card and no query.<commit_after>const express = require('express')
const router = express.Router()
const { data } = require('../data/cards.json')
const { cards } = data

router.get('/', (req, res) => {
  let questionNum = cards.length
  let randomCard = Math.floor(Math.random() * questionNum)
  res.redirect(`/cards/${randomCard}?side=question`)
})

router.get('/:id', (req, res) => {
  const name = req.cookies.username
  const { id } = req.params
  let { side } = req.query
  const text = cards[id][side] // this combines the id param query (question or answer) to set value of text to Q or A.
  const { hint } = cards[id]
  const templateData = { id, text }

  if (side === null || side === undefined) {
    res.redirect(`/cards/${id}?side=question`)
    return // need to return here else express tries to send the render too leading to an error.
    // Error: Can't set headers after they are sent. redir sends 302, render tries to send 200.
  }

  if (name) {
    if (side === 'question') {
      templateData.hint = hint
      templateData.sideToShow = 'answer'
      templateData.sideToShowDisplay = 'Answer'
    } else if (side === 'answer') {
      templateData.sideToShow = 'question'
      templateData.sideToShowDisplay = 'Question'
    }
    res.render('card', templateData)
  } else {
    res.redirect('/hello')
  }
})

module.exports = router

{'fraction_non_alphanumeric': 0.14469026548672567, 'fraction_numerical': 0.002654867256637168, 'mean_word_length': 3.187037037037037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4585338', 'n_tokens_mistral': 706, 'n_tokens_neox': 691, 'n_words': 209}