简体   繁体   English

为什么Express / node.js中的路由器返回404

[英]Why router in Express/node.js return 404

I just generated Express app and added custom route (/configuration). 我刚刚生成了Express应用,并添加了自定义路由(/配置)。 But if I try to open http://localhost:3000/configuration , server returns 404 Not Found error. 但是,如果我尝试打开http:// localhost:3000 / configuration ,服务器将返回404 Not Found错误。 I checked the code and don't understand where is error. 我检查了代码,不知道哪里出了错误。

app.js 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 is similar) route / 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;

This code is the problem 此代码是问题

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

Also the current Url which is generated by your application is http://localhost:3000/configuration/configuration . 您的应用程序生成的当前URL也是http:// localhost:3000 / configuration / configuration If you make a query on this then it will work. 如果对此进行查询,那么它将起作用。 Now if you wan to use it with http://localhost:3000/configuration . 现在,如果您想与http:// localhost:3000 / configuration一起使用 then you need to remove on path from anywhere. 那么您需要从任何地方删除路径。 May be you can change in main file like this 也许您可以像这样在主文件中进行更改

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

How can it be used for error handling. 如何将其用于错误处理。 Remove it and add this code to catch any error in application. 删除它并添加此代码以捕获应用程序中的任何错误。

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

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

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