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 Randomize Cards

Dehn Hunsworth
Dehn Hunsworth
7,189 Points

cant get server running

Been following the code pretty exact i believe but I am unable to even get the server going. error message that appears in terminal not much help.

Here is the message:

[nodemon] starting `node app.js`
C:\Users\The Huns\Desktop\Web-Dev\Team Treehouse\flashcardsExpress\node_modules\path-to-regexp\index.js:63
  path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))
                                                ^

TypeError: Cannot read property 'length' of undefined
    at pathtoRegexp (C:\Users\The Huns\Desktop\Web-Dev\Team Treehouse\flashcardsExpress\node_modules\path-to-regexp\index.js:63:49)
    at new Layer (C:\Users\The Huns\Desktop\Web-Dev\Team Treehouse\flashcardsExpress\node_modules\express\lib\router\layer.js:45:17)
    at Function.use (C:\Users\The Huns\Desktop\Web-Dev\Team Treehouse\flashcardsExpress\node_modules\express\lib\router\index.js:464:17)
    at Function.<anonymous> (C:\Users\The Huns\Desktop\Web-Dev\Team Treehouse\flashcardsExpress\node_modules\express\lib\application.js:220:21)
    at Array.forEach (<anonymous>)
    at Function.use (C:\Users\The Huns\Desktop\Web-Dev\Team Treehouse\flashcardsExpress\node_modules\express\lib\application.js:217:7)
    at Object.<anonymous> (C:\Users\The Huns\Desktop\Web-Dev\Team Treehouse\flashcardsExpress\app.js:16:5)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
[nodemon] app crashed - waiting for file changes before starting...

and the cards.js where I had been making the most recent changes:

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}?side=question`);
});

router.get("/:id", (req, res) => {
   const { side } = req.query;
   const { id } = req.params;
   const text = cards[id][side];
   const { hint } = cards[id];

   const templateData = { id, text };

   if (side === "question") {
      templateData.hint = hint;
      templateData.sideToShow = "answer";
   } else if (side === "answer") {
      templateData.sideToShow = "question";
   }

   res.render("card", templateData);
});

module.exports = router;

Sure it is something simple but i have been looking at it for hours and my head hurts... Thanks.

Dehn Hunsworth
Dehn Hunsworth
7,189 Points

Deleted a variable from my app.js that was just a test and unused and still no server but the message has changed.

:\Users\The Huns\Desktop\Web-Dev\Team Treehouse\flashcardsExpress\node_modules\express\lib\application.js:210
    throw new TypeError('app.use() requires a middleware function')
    ^

TypeError: app.use() requires a middleware function
    at Function.use (C:\Users\The Huns\Desktop\Web-Dev\Team Treehouse\flashcardsExpress\node_modules\express\lib\application.js:210:11)
    at Object.<anonymous> (C:\Users\The Huns\Desktop\Web-Dev\Team Treehouse\flashcardsExpress\app.js:15:5)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
[nodemon] app crashed - waiting for file changes before starting...

and the 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").default;
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 port 3000.");
});