繁体   English   中英

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

[英]node.js express route.get() error

我正在使用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

该错误告诉我您没有传递适当的表达中间件功能: at Object.<anonymous> (/home/ryan/Desktop/node/frameworks/expressapi/app.js:45:5)

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

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

在45:5删除此行代码,看看是否成功/获得其他错误消息。

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

看来您需要./routes/index ,然后尝试在app.get('/')调用routes.index 最初尝试仅要求'./routes'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM