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 Linking Around the Application

Cory Wallace
Cory Wallace
14,313 Points

This doesnt work

I followed chalkers directions, I even copied the code from the project files into visual code, and it hangs when trying to load cards. please help:

Here's my App.js:

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

const app = express();

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

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

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

app.use(mainRoutes); app.use('/cards', cardRoutes);

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

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

app.listen(3000, () => { console.log('The application is running on localhost:3000!') });

and Cards.js:

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

router.get( '/', ( req, res ) => { const numberOfCards = cards.length; const flashcardId = Math.floor( Math.random() * numberOfCards ); res.redirect( /cards/${flashcardId}?side=question ) });

router.get('/:id', (req, res) => { const { side } = req.query; const { id } = req.params; const text = cards[id][side]; const { hint } = cards[id];

const templateData = { id, text };

if ( side === 'question' ) {
  templateData.hint = hint;
  templateData.sideToShow = 'answer';
  templateData.sideToShowDisplay = 'Answer';
} else if ( side === 'answer' ) {
  templateData.sideToShow = 'question';
  templateData.sideToShowDisplay = 'Question';
}

res.render('card', templateData);

});

module.exports = router;

and Index.JS:

const express = require('express'); const router = express.Router();

router.get('/', (req, res) => { const name = req.cookies.username; if (name) { res.render('index', { name }); } else { res.redirect('/hello'); } });

router.get('/hello', (req, res) => { const name = req.cookies.username; if (name) { res.redirect('/'); } else { res.render('hello'); } });

router.post('/hello', (req, res) => { res.cookie('username', req.body.username); res.redirect('/'); });

router.post('/goodbye', (req, res) => { res.clearCookie('username'); res.redirect('/hello'); });

module.exports = router;

Here's my PUG Card:

extends layout.pug

block content section#content h2= text if hint p i Hint: #{hint} a(href=${id}?side=${sideToShow})= sideToShowDisplay

and before you ask, Yes the data file is in the correct place, and everything else was copied exactly.

3 Answers

Cory Wallace
Cory Wallace
14,313 Points

After tedious scrubbing of the Treehouse forums, I figured it out.

I did not indent properly ion my Card.pug file. Here is how I previously had it:

extends layout.pug

block content
    section#content
        h2= text
        if hint
            p
                i Hint: #{hint}
         a(href=`${id}?side=${sideToShow}`)= sideToShowDisplay

here''s how I had it after fixing it:

extends layout.pug

block content
    section#content
        h2= text
        if hint
            p
                i Hint: #{hint}
        a(href=`${id}?side=${sideToShow}`)= sideToShowDisplay

it' just barely noticable, but I had about one or two characters worth of spacing BEFORE the "a(href=${id}?side=${sideToShow})= sideToShowDisplay"

and it was breaking my whole thing.

So sad, yet so simple . . . Oh well, it's fixed now.

Shout out to this person:

nguoisechia binnary on Oct 23, 2017 You probably haven't indent correctly of every line in the PUG file.

From this thread:

(https://teamtreehouse.com/community/my-cards-page-is-not-working-all-other-pages-working-properly)

Thanks for posting your solution. It's a good reminder that syntax is important and easy to miss (especially when it's just spacing)!

Cory Wallace
Cory Wallace
14,313 Points

here is the error i'm getting in my console. It isn't helping me:

RangeError: Invalid status code: undefined at ServerResponse.writeHead (http_server.js:190:11) at ServerResponse._implicitHeader (_http_server.js:181:8) at write (http_outgoing.js:635:9) at ServerResponse.end (_http_outgoing.js:754:5) at ServerResponse.send (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\express\lib\response.js:221:10) at done (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\express\lib\response.js:1004:10) at Object.exports.renderFile (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\pug\lib\index.js:422:12) at View.exports._express [as engine] (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\pug\lib\index.js:465:11) at View.render (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\express\lib\view.js:135:8) at tryRender (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\express\lib\application.js:640:10)

Mary Paul
Mary Paul
13,792 Points

Read through everything and STILL didn't think it was the pug file. Your solution fixed it. Thanks a bunch.