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 trialSean Flanagan
33,235 PointsCannot GET /cards
Hi. This is what I get when I try to run my /cards page.
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((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!");
});
My cards.js:
const express = require("express");
const router = express.Router();
const {data} = require("../data/flashcardData.json");
const {cards} = data;
router.get("/:id", (req, res) => {
res.render("card", {
prompt: cards[req.params.id].question,
hint: cards[req.params.id].hint
});
});
module.exports = router;
My 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");
}
res.render("index", {name: name});
});
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");
});
//router.use((req, res, next) => {
// const err = new Error("Not Found");
// err.status = 404;
// next(err);
//});
module.exports = router;
My flashcardData.json:
{
"data": {
"title": "JavaScript Flashcards",
"cards": [
{
"question": "What language are Express apps written in?",
"hint": "It starts with a \"J\"",
"answer": "JavaScript"
},
{
"question": "What is one way a website can store data in a user's browser?",
"hint": "They are delicious with milk",
"answer": "Cookies"
},
{
"question": "What is a common way to shorten the response object's name inside middleware?",
"hint": "It has the same abbreviation as \"resolution\"",
"answer": "res"
},
{
"question": "How many different values can booleans have?",
"hint": "Think: binary",
"answer": "2"
},
{
"question": "Which HTML element can contain JavaScript?",
"hint": "It starts with an \"s\"",
"answer": "<script>"
}
]
}
}
Can anyone please tell me where I've gone wrong? Cheers!
4 Answers
Natalie Cluer
18,898 Pointsyep! :id is just a placeholder for the actual id number of the card
Natalie Cluer
18,898 PointsHi Sean! I think the problem is that your card route is set up to answer a get request to cards/:id, not cards/.
Sean Flanagan
33,235 PointsHi Natalie! I think I can see where you're going with this. I tried cards/0 and the question and hint appeared, then when I did cards/1, the question and hint for that card appeared. So I don't type in cards/, followed not by :id literally, but by the id number?
Sean Flanagan
33,235 PointsI get it now. Thanks again!