Ryan
Ryan

Reputation: 29

node.js express route.get() error

I am using express-generator and installed the dependencies, everything else is as is, except this app.js file I've updated and posted here.

I'm getting the error when running npm start. I've posted the error and app.js file. It's probably something basic, I'm a newb. Thanks so much.

Error message

    ryan@\Ryan:~/Desktop/node/frameworks/expressapi$ npm start

> [email protected] start /home/ryan/Desktop/node/frameworks/expressapi
> node ./bin/www


/home/ryan/Desktop/node/frameworks/expressapi/node_modules/express/lib/router/route.js:196
        throw new Error(msg);
              ^
Error: Route.get() requires callback functions but got a [object Undefined]
    at Route.(anonymous function) [as get] (/home/ryan/Desktop/node/frameworks/expressapi/node_modules/express/lib/router/route.js:196:15)
    at EventEmitter.app.(anonymous function) [as get] (/home/ryan/Desktop/node/frameworks/expressapi/node_modules/express/lib/application.js:481:19)
    at Object.<anonymous> (/home/ryan/Desktop/node/frameworks/expressapi/app.js:45:5)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/ryan/Desktop/node/frameworks/expressapi/bin/www:7:11)
npm ERR! weird error 8
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian

npm ERR! not ok code 0

Server code

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

var products = [
    {
      id: 0,
      name: 'watch',
      description: 'tell time with this amazing watch',
      price: 30.00
    },
    {
      id: 1,
      name: 'sandals',
      description: 'walk in comfort with these sansdals',
      price: 10.00
    },
    {
      id: 2,
      name: 'watch',
      description: 'protect your eyes!',
      price: 25.00
    }
];

app.get('/', routes.index);

app.get('/products', function(req, res) {
  res.json(products);
});

app.get('/products/:id', function(req, res) {
  if (req.params.id > (products.length - 1) || req.params.id < 0) {
    res.statusCode = 404;
    res.end('NotFound');
  }
  res.json(products[req.params.id]);
});

app.post('/products', function(req, res) {
  if (typeof req.body.name === 'undefined') {
    res.statusCode = 400;
    res.end('a product name is required');
  }
  products.push(req.body);
  res.send(req.body);
});

app.put('/products/:id', function(req, res) {
  if (req.params.id > (products.length - 1) || req.params.id < 0) {
    res.satusCode = 404;
    res.end('not found');
  }
  products[req.params.id] = req.body;
  res.send(req.body);
});

app.delete('/products/:id', function(req, res) {
  if (req.params.id > (products.length - 1) || req.params.id < 0) {
    res.statusCode = 400;
    res.end('not found for that id');
  }
  products.splice(req.params.id, 1);
  res.json(products);
});

if i delete line 45 like some have recommended i get the error

ryan@\Ryan:~/Desktop/node/frameworks/expressapi$ npm start

> [email protected] start /home/ryan/Desktop/node/frameworks/expressapi
> node ./bin/www


/home/ryan/Desktop/node/frameworks/expressapi/bin/www:16
app.set('port', port);
    ^
TypeError: Object #<Object> has no method 'set'
    at Object.<anonymous> (/home/ryan/Desktop/node/frameworks/expressapi/bin/www:16:5)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
npm ERR! weird error 8
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian

npm ERR! not ok code 0

Upvotes: 0

Views: 2074

Answers (2)

mixophrygian
mixophrygian

Reputation: 659

It looks like you're requiring ./routes/index, and then trying to call routes.index in app.get('/'). Try only requiring './routes' initially.

Upvotes: 0

Chris Anderson
Chris Anderson

Reputation: 8505

This error tells me that you're not passing in a proper express middleware function: at Object.<anonymous> (/home/ryan/Desktop/node/frameworks/expressapi/app.js:45:5)

I pasted your code into a text editor and I got this line of code for 45:5: app.get('/', routes.index);

My guess is that you've deleted the file for routes.index (var routes = require('./routes/index');)

Delete this line of code on 45:5 and see if you succeed/get a different error message.

FYI - You have a lot of spelling mistakes that may lead to issues like line 70: res.satusCode = 404; has statusCode misspelled, but that's not your current issue, I believe.

Upvotes: 1

Related Questions