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
Mary Paul
13,792 PointsStill getting "undefined" errors on my /cards page
Section five, video one of Express basics course
All the other pages work, but /cards hangs and gives an undefined error- it's also not hitting an error page, just hanging and giving me errors in console:
RangeError: Invalid status code: undefined at ServerResponse.writeHead (http_server.js:194:11) at ServerResponse._implicitHeader (_http_server.js:185:8) at write (_http_outgoing.js:632:9) at ServerResponse.end (_http_outgoing.js:751:5)
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', err);
});
app.listen(3000, () => {
console.log('The application is running on localhost:3000!');
});
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: 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;
cards.js
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.render('card', {prompt: "Who is buried in grant's tomb?" });
});
module.exports = router;
card.pug
extends layout.pug
block content
section#content
h2= prompt
if hint
p
i Hint: #{hint}
else
p No hint for you.
error.pug
extends layout
block content
h1= error.message
h2= error.status
pre= error.stack
2 Answers
Mary Paul
13,792 PointsSpoiler alert: It was that stupid cards.pug file.
What proved it to me was renaming cards "cards2", creating a new cards page, and pasting the hello.pug code in there. It worked, proving that file was the problem child. Then it was fussing with the code until the indents were perfect. What a nuisance! I wish more had been mentioned about this being a frequent issue with pug.
Adam Beer
11,314 PointsI am not sure but what's going on when you delete the "name:" into the index.js. For example:
res.render('index', { name });
Mary Paul
13,792 PointsSame error. No change.
Adam Beer
11,314 PointsPlease help, which video is this?
Mary Paul
13,792 PointsHi Adam, It's section 5 video 1 (I added a note to the top of my entry). Just fixed the problem, however.
Adam Beer
11,314 PointsThanks, I did not see it.
Adam Beer
11,314 PointsAdam Beer
11,314 PointsThis is the Express Basic course? Which video?