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 trial

JavaScript Express Basics Parameters, Query Strings, and Modularizing Routes Using Data and Route Parameters

Michael Williams
PLUS
Michael Williams
Courses Plus Student 8,059 Points

Does the URL parameter always have to be named 'id'?

Here's the code from the exercise. It wasn't clear if id could be called anything since it's being treated like a variable or if it's hardwired in as an id property.

const express = require ('express');
const router = express.Router();
const { data } = require('../data/flashcardData.json'); //equivalent of const data = require('../data/flashcardData.json').data;
const { cards } = data; //curly braces mean that cards = data.cards >> const cards = data.cards; 


router.get('/:id', (req, res) => {
    res.render('card', { 
        prompt: cards[req.params.id].question,
        hint: cards[req.params.id].hint
    });
});

module.exports = router;

2 Answers

Hi Michael,

You are right in thinking the route parameter acts like a variable. You can call it whatever you like, just make sure you update any references to it.

// Route
router.get('/:blah')

// To access blah use:
req.params.blah

Example:

router.get('/:blah', (req, res) => {
    res.render('card', { 
        prompt: cards[req.params.blah].question
        // ...
    });
});

Hope this helps :)