Skip to content

Commit 72fa8e9

Browse files
author
codingChewie
committed
Using Data and Route Parameters
1 parent 7191b48 commit 72fa8e9

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

data/flashcardData.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"data": {
3+
"title": "JavaScript Flashcards",
4+
"cards": [
5+
{
6+
"question": "What language are Express apps written in?",
7+
"hint": "It starts with a \"J\"",
8+
"answer": "JavaScript"
9+
},
10+
{
11+
"question": "What is one way a website can store data in a user's browser?",
12+
"hint": "They are delicious with milk",
13+
"answer": "Cookies"
14+
},
15+
{
16+
"question": "What is a common way to shorten the response object's name inside middleware?",
17+
"hint": "It has the same abbreviation as \"resolution\"",
18+
"answer": "res"
19+
},
20+
{
21+
"question": "How many different values can booleans have?",
22+
"hint": "Think: binary",
23+
"answer": "2"
24+
},
25+
{
26+
"question": "Which HTML element can contain JavaScript?",
27+
"hint": "It starts with an \"s\"",
28+
"answer": "<script>"
29+
}
30+
]
31+
}
32+
}

routes/cards.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
const express = require('express')
22
const router = express.Router()
33
const eyes = require('eyespect')
4+
const { data } = require('../data/flashcardData.json')
5+
const { cards } = data
46

5-
router.get('/', (req, res) => {
6-
eyes.inspect(res.locals, 'res.locals')
7+
router.get('/:id', (req, res) => {
8+
res.render('card', {
9+
prompt: cards[req.params.id].question,
10+
hint: cards[req.params.id].hint,
11+
})
12+
})
13+
14+
// Sample testing req.params
15+
router.get('/:foo/:bar', (req, res) => {
16+
eyes.inspect(req.params, 'req.params') // http://expressjs.com/en/api.html#req.params
717

818
res.render('card', {
9-
prompt: `Who is buried in Grant's tomb?`,
19+
prompt: cards[req.params.foo].question,
20+
hint: cards[req.params.bar].hint,
1021
})
1122
})
1223

0 commit comments

Comments
 (0)