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 Build a REST API With Express Building API Routes in Express Building the Question Routes

Error in node_modules?

After running node index.js I got an error: TypeError: Cannot read property 'push' of undefined at Function.route(C:Users\Brinka\node_modules\express\lib\router'index.js: 502:13) at Function.proto(anonymous function) [as get] at Function.route(C:Users\Brinka\node_modules\express\lib\router'index.js: 502:92) at Object.<anonymous> (C:\Users\Brinka\project\routes.js:6:8)

On the line 6, I got:

router.get("/", function(req, res) {
  //Return all the questions
  res.json({ response: "You sent it "});
}); //strps away /questions

my index.js file:

'use strict';

var express = require("express");
var app = express();
var routes = require("./routes");

var jsonParser = require("body-parser").json;

app.use(jsonParser());

app.use("/questions", routes); //only for requests that start with /questions


var port = process.env.PORT || 3000;

app.listen(port, function() {
  console.log("Express server is listening on port", port);
});

my routes.js file:

'use strict';

var express = require("express");
var router = express.Router;

router.get("/", function(req, res) {
  //Return all the questions
  res.json({ response: "You sent it "});
}); //strps away /questions


router.post("/", function(req, res) {
  //Return all the questions
  res.json({ response: "You posted it ",
  body: req.body
});
}); //strps away /questions


router.get("/:id", function(req, res) {
  //Return all the questions
  res.json({ response: "You got it for me ",
  body: req.body
});
}); //strps away /questions



module.exports = router;

4 Answers

akak
akak
29,445 Points

You need to call Router

// change
var router = express.Router; 

// to
var router = express.Router();

cheers!

thanks a ton! I am using atom, do you know whether there are any packages that can check these kind of errors? Maybe node module?

akak
akak
29,445 Points

This kind of error could not be checked since you may want to pass express.Router somewhere, not always call it. Code linter doesn't know your intention - both express.Router and express.Router() are correct code. Your use case requires to call the function instead of just assigning it to variable, but that's something you have to remember :)

:)

Thanx akak I also got help...