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

Brian Patterson
Brian Patterson
19,588 Points

getting this error RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined when going to cards.

I am running the following code in the cards.js file.

const express = require('express');
const router = express.Router();
const { data } = require('../data/flashcardData.json');
//the above is the same as:
//const  data  = require('../data/flashcardData.json').data;
const cards = data.cards;

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

module.exports = router;

and I am getting the above error. What does it mean? Below is it the error code in the console.

at ServerResponse.writeHead (_http_server.js:197:11)
    at ServerResponse._implicitHeader (_http_server.js:188:8)
    at write_ (_http_outgoing.js:644:9)
    at ServerResponse.end (_http_outgoing.js:767:5)
    at ServerResponse.send (/Users/briankaty1/Dropbox/JavaScript/flashcards/node_modules/express/lib/response.js:221:10)
    at done (/Users/briankaty1/Dropbox/JavaScript/flashcards/node_modules/express/lib/response.js:1004:10)
    at Object.exports.renderFile (/Users/briankaty1/Dropbox/JavaScript/flashcards/node_modules/pug/lib/index.js:422:12)
    at View.exports.__express [as engine] (/Users/briankaty1/Dropbox/JavaScript/flashcards/node_modules/pug/lib/index.js:465:11)
    at View.render (/Users/briankaty1/Dropbox/JavaScript/flashcards/node_modules/express/lib/view.js:135:8)
    at tryRender (/Users/briankaty1/Dropbox/JavaScript/flashcards/node_modules/express/lib/application.js:640:10)

2 Answers

Check your pug file for example if you still pass all the required variables, for example

if you comment out colors variable, pug cannot create list of colors and in my case failed with ERR_HTTP_INVALID_STATUS_CODE

res.render('card', {
    prompt: "Who is buried in Grant's tomb?",
    hint: 'Think about whose tomb it is.',
    //colors,
  });
extends layout.pug

block content
  section#content
//    ul
//      each color in colors
//        li= color
    h2= prompt
    if hint
      p
        i Hint: #{hint}
    else
      p (There is no hint.)

What URL are you typing into your browser? I was getting this error because I accidentally typed localhost:3000/cards/id into my browser instead of localhost:3000/cards/1

But yeah, I would check your URL path

I am getting the same error but my url is correct. It would be really nice if you responded saying how you fixed it for the benefit of others.