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

Mitchell Wintrow
Mitchell Wintrow
4,380 Points

getting 404 error when I use `'/'` instead of `'/cards'` and `'/cards:id'` doesn't work nor does `cards[req.params.id]`

I discovered another error that prevents me from moving on from this video. I had to change the app.use('/cards', cardRoutes); to app.use(cardRoutes);. Now my new problem is this:

When I have my cards.js file set up like this:

const express = require('express');
const router = express.Router();
const {data} = require('../data/flashcardData.json');
const {cards} = data;

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

module.exports = router;

it works and I can view the cards page finally. But in the video Andrew said we can just change router.get('/cards', (req, res) => { to router.get('/', (req, res) => { and it will still work. When I change mine to just have '/' it stops working and I get a 404 error so I left it as '/cards' and then it works again. So I followed along in the next video but as soon as I tried doing what he said: '/cards:id' and prompt: cards[req.params.id].question, hint: cards[req.params.id].hint then it stopped working and I got a 404 error again when I typed this into the url: localhost:3000/cards/0.

Can anyone please help me to understand what is going on? Andrews videos are really confusing. I wish Guil was teaching this stuff because I'm really interested in Node and Express and all the server side stuff, and Guil is meticulous and doesn't skip over anything or assume we know something. He just gives it all to us, simple, concise, and meticulous with the details.

Instead of '/cards:id', have you tried '/cards/:id' with localhost:3000/cards/0? If you don't use a / before the :id, the route would match localhost:3000/cards0 instead.

2 Answers

0yzh 󠀠
0yzh 󠀠
17,276 Points

Hey Mitchell Wintrow , when you change

router.get('/cards', (req, res) => { /*...*/ })

to

router.get('/', (req, res) => { /*...*/ })

make sure that in your app.js file, you also update cardRoutes to point to '/cards', see below:

// update in your app.js:
app.use('/cards', cardRoutes);

Since you're already telling express to use cardRoutes for any requests made to the '/cards' endpoint. In your cards.js you can simply use to '/'

Hope this helps!

Mitchell Wintrow
Mitchell Wintrow
4,380 Points

Hello 0yzh 󠀠 ! Thank you so much for your help. You are absolutely right, I changed it back to

// update in your app.js:
app.use('/cards', cardRoutes);

and it worked perfectly. I had removed the '/cards' and only left the cardRoutes, so when I added it back again everything works great now. I really appreciate your help. By the way, I have a funny question for you too... How are you able to post the code in your answer with the colors on the text too like in a code editor? I've been trying to figure out how to do that, because I've been using the triple back tick method, but it only makes it black and white.

0yzh 󠀠
0yzh 󠀠
17,276 Points

Mitchell Wintrow awesome! Glad it worked out for you.

RE: the code colors in my answer, I believe you just need to add the language name after the three back ticks. For example in my answer I used ```js to let it know to use javascript syntax highlighting. There's also a markdown cheat sheet you can reference at the bottom of the reply box.

Cheers!

Mitchell Wintrow
Mitchell Wintrow
4,380 Points

Thank you so much! I totally missed that little link at the bottom, but I just checked it out and that was exactly what I was looking for. Thank you again and I wish you well!