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

Kevin Becerra
Kevin Becerra
14,243 Points

Only able to get page to load by using whole route when I should be able to use '/:id'

This is the app.js and card.js I know the file structure is correct I don't know what would be causing this

card.js

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


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

   module.exports = router;

App.js

   const express = require('express');
   const bodyParser = require('body-parser');
   const cookieParser = require('cookie-parser');
   const app = express();

   app.use(cookieParser());

   app.use(bodyParser.urlencoded({ extended: false }));

   app.set('view engine', 'pug');


   const mainRoutes = require('./routes');
   const cardRoutes = require('./routes/cards');

   app.use(mainRoutes);
   app.use(cardRoutes);


   app.use((req, res, next) => {
       const err = new Error('URL not found');
       err.status = 404;
       next(err);
   });


   app.use((err, req, res, next) => {
       res.locals.error = err;
       res.status(err.status);
       res.render('error');
   });

1 Answer

Zimo Dong
seal-mask
.a{fill-rule:evenodd;}techdegree
Zimo Dong
Full Stack JavaScript Techdegree Student 10,755 Points

instead of app.use(cardRoutes); try app.use('/cards', CardRoutes); //the path param means that every route in our cards.js start with /cards