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 Serving Static Files in Express Adding Static Assets to the App

Julianna Kahn
Julianna Kahn
20,701 Points

I added "return" as specified and although the code performs as expected, I continue to get error messages in the consol

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} ) });

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

if ( !side ) {
    return res.redirect(`/cards/${id}?side=question`);
}
const name = req.cookies.username;
const text = cards[id][side];
const { hint } = cards[id];

const templateData = { id, text, name };

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;

index.pug file extends layout.pug

block content section#content p a(href='/cards') Begin!

Error on Begin link: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (http_outgoing.js:455:11) at ServerResponse.header (/Users/julianna/Desktop/flashcards/node_modules/express/lib/response.js:771:10) at ServerResponse.send (/Users/julianna/Desktop/flashcards/node_modules/express/lib/response.js:170:12) at done (/Users/julianna/Desktop/flashcards/node_modules/express/lib/response.js:1008:10) at Object.exports.renderFile (/Users/julianna/Desktop/flashcards/node_modules/pug/lib/index.js:421:12) at View.exports._express as engine at View.render (/Users/julianna/Desktop/flashcards/node_modules/express/lib/view.js:135:8) at tryRender (/Users/julianna/Desktop/flashcards/node_modules/express/lib/application.js:640:10) at Function.render (/Users/julianna/Desktop/flashcards/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/Users/julianna/Desktop/flashcards/node_modules/express/lib/response.js:1012:7

card.pug file extends layout.pug

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

Error on Next card link: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (http_outgoing.js:455:11) at ServerResponse.header (/Users/julianna/Desktop/flashcards/node_modules/express/lib/response.js:771:10) at ServerResponse.send (/Users/julianna/Desktop/flashcards/node_modules/express/lib/response.js:170:12) at done (/Users/julianna/Desktop/flashcards/node_modules/express/lib/response.js:1008:10) at Object.exports.renderFile (/Users/julianna/Desktop/flashcards/node_modules/pug/lib/index.js:421:12) at View.exports._express as engine at View.render (/Users/julianna/Desktop/flashcards/node_modules/express/lib/view.js:135:8) at tryRender (/Users/julianna/Desktop/flashcards/node_modules/express/lib/application.js:640:10) at Function.render (/Users/julianna/Desktop/flashcards/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/Users/julianna/Desktop/flashcards/node_modules/express/lib/response.js:1012:7)

1 Answer

Simon Coates
Simon Coates
4,967 Points

If anyone looks at this at some future point, I copied the code across and was unable to duplicate the error for debugging purposes. The only glitches were missing tick quotes (which crashes things when starting the server) and the first redirect not including the side parameter, resulting in an extra redirect. Maybe the server didn't reset between code alterations. But otherwise, it seems to run okay for me and I was able to test hitting both the redirects.