首页 文章

运行nodejs服务器时为什么会出现此错误?

提问于
浏览
0

运行nodejs Server时出现此错误

Error: Not Found
    at C:\wamp\www\scope-leads-node-master\MyApp\app.js:30:13
    at Layer.handle [as handle_request] (C:\wamp\www\scope-leads-node-master\MyApp\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\wamp\www\scope-leads-node-master\MyApp\node_modules\express\lib\router\index.js:312:13)
    at C:\wamp\www\scope-leads-node-master\MyApp\node_modules\express\lib\router\index.js:280:7
    at Function.process_params (C:\wamp\www\scope-leads-node-master\MyApp\node_modules\express\lib\router\index.js:330:12)
    at next (C:\wamp\www\scope-leads-node-master\MyApp\node_modules\express\lib\router\index.js:271:10)
    at C:\wamp\www\scope-leads-node-master\MyApp\node_modules\express\lib\router\index.js:618:15
    at next (C:\wamp\www\scope-leads-node-master\MyApp\node_modules\express\lib\router\index.js:256:14)
    at Function.handle (C:\wamp\www\scope-leads-node-master\MyApp\node_modules\express\lib\router\index.js:176:3)
    at router (C:\wamp\www\scope-leads-node-master\MyApp\node_modules\express\lib\router\index.js:46:12)
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')));
    app.use('/', routes);
    app.use('/users', users);
    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
      var err = new Error('Not Found');
      err.status = 404;
      next(err);
    });
    // error handlers
    // development error handler
    // will print stacktrace
    if (app.get('env') === 'development') {
      app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
          message: err.message,
          error: err
        });
      });
    }
    // production error handler
    // no stacktraces leaked to user
    app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
        message: err.message,
        error: {}
      });
    });
    module.exports = app;

3 回答

  • 0

    最后,它正在发挥作用 . 我仍然在http://localhost:3000看到"not found"错误 . 但一切都很好 . 我用另一台电脑来做这件事 . 刚刚做了以下几点 .

    npm install express
    
    npm install
    
    npm start
    

    nodejs,git服务器应该已经安装在我们的机器上 .

  • 2

    嗯,这是你在你写的中间件中传递的错误(第30行):

    app.use(function(req, res, next) {
      var err = new Error('Not Found');
      err.status = 404;
      next(err);
    });
    

    此代码只是在每个HTTP请求上传递错误,您应该将其注释掉 .

  • 2

    我也得到了同样的错误,并在搜索答案时找到了这个帖子 .

    在这里,我分享你应该解决这个错误的方式 .

    1)首先注释掉处理404错误的行 .

    // app.use(function (req, res, next) {
    //    var err = new Error('Not Found');
    //    err.status = 404;
    //    next(err);
    // });
    

    2)现在重新加载你的页面,你会看到类似的东西

    无法获得/(某些路线)

    3)理解:404或Not Found错误消息是标准HTTP响应代码,用于指示客户端能够与给定服务器通信,但服务器无法找到所请求的内容 . 它现在说缺少的资源在路线“/(某些路线)” .

    4)处理错误

    如果它是一个网页,问题是“/(某些路由)”在〜/ routes /目录的JS文件中找不到 . 您可能忘记处理该路线或可能是一个错字或其他东西 . 妥善处理 .

    如果它是资源,请确保资源存在于项目的上述路径中 .

相关问题