Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 :)