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 Modular Routes

After adding the json data the question and hint are not loading in /cards

I get no errors, but the cards page will not show the question and the hint for cards[0]. cards.js

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

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

module.exports = router;

card.pug

extends layout.pug

block content
  section#content
    h2= prompt
    if hint
      p
        i Hint: #{hint}

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.post("/goodbye", (req, res) => {
  res.clearCookie("username");
  res.redirect("/hello");
})

router.get("/cards", (req, res) => {
  res.render("card");
});

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("/",);
});

module.exports = router;

app.js

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

const app = express() // returns express app

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

app.set("view engine", "pug"); // enable pug

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

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

app.use((req, res, next) => {
  console.log("one!");
  next();
});

app.use((req, res, next) => {
  console.log("two!");
  next();
});

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");
})// setup server