node.js表示route.get()错误

时间:2015-10-01 00:13:17

标签: node.js express npm

我正在使用express-generator并安装了依赖项,其他所有内容都是原样,除了这个app.js文件我已经更新并发布在这里。

我在运行npm start时遇到错误。我发布了错误和app.js文件。它可能是基本的东西,我是个新手。非常感谢。

错误消息

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

> expressapi@0.0.0 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

服务器代码

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);
});

如果我删除第45行,就像有些人建议我得到错误

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

> expressapi@0.0.0 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

2 个答案:

答案 0 :(得分:1)

此错误告诉我您没有传递正确的快速中间件功能:at Object.<anonymous> (/home/ryan/Desktop/node/frameworks/expressapi/app.js:45:5)

我将您的代码粘贴到文本编辑器中,我获得了45:5的这行代码: app.get('/', routes.index);

我的猜测是你删除了routes.index(var routes = require('./routes/index');)的文件

在45:5上删除这行代码,看看你是否成功/得到了不同的错误信息。

仅供参考 - 你有很多拼写错误,可能会导致第70行出现问题:res.satusCode = 404;状态编码拼写错误,但我认为这不是你当前的问题。

答案 1 :(得分:0)

您似乎需要./routes/index,然后尝试在routes.index中致电app.get('/')。最初只需要'./routes'