【问题标题】:Why router in Express/node.js return 404为什么 Express/node.js 中的路由器返回 404
【发布时间】:2018-01-20 15:50:55
【问题描述】:

我刚刚生成了 Express 应用并添加了自定义路由 (/configuration)。但是如果我尝试打开http://localhost:3000/configuration,服务器会返回 404 Not Found 错误。我检查了代码,不明白哪里出错了。

app.js

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 index = require('./routes/index');
var config_page = require('./routes/configuration');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

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('/', index);
app.use('/configuration', config_page);

// 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 handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

routes/configuration.js(routes/index.js 类似)

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

/* GET configuration page. */
router.get('/configuration', function(req, res, next) {
  res.render('configuration', { title: 'My App | Configuration' });
});

module.exports = router;

【问题讨论】:

    标签: javascript node.js express routing http-status-code-404


    【解决方案1】:

    这段代码有问题

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

    您的应用程序生成的当前 URL 也是 http://localhost:3000/configuration/configuration。如果您对此进行查询,那么它将起作用。现在,如果您想将它与 http://localhost:3000/configuration 一起使用。那么你需要从任何地方删除路径。也许您可以像这样在主文件中进行更改

    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('/', index);
    app.use('/',config_page); //-----------> this is the line you need to change
    

    它如何用于错误处理。删除它并添加此代码以捕获应用程序中的任何错误。

    process.on('uncaughtException', function (err) {
      // This should not happen
      logger.error("Pheew ....! Something unexpected happened. This should be handled more gracefully. I am sorry. The culprit is: ", err);
    });
    

    【讨论】:

    • 现在它返回 'Cannot GET /configuration'
    • 我相信“Cannot GET /xyz”消息是默认的 404 消息。也不确定这个答案会如何改变任何东西,只要错误处理在最后它不应该干扰。我很确定 404 错误无论如何都不算​​作未捕获的异常
    • 我刚刚注意到,如果您将所有路由存储在一个文件中(例如 index.js),那么一切正常。但是如果为每个页面在 routes 中创建一个单独的 .js,那么服务器开始为除主页面之外的所有页面返回 404 错误。看不懂Express中路由器的逻辑……
    • @mxsin,我更新了 mu 答案。它应该适合你。
    猜你喜欢
    • 2017-08-23
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2016-12-08
    • 2013-09-28
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多