Express JS 404中的新路由错误

时间:2021-02-28 20:36:10

Trying to add a new route to this Express app using the Express app generator. this is the app.js file:

尝试使用Express应用程序生成器向此Express应用程序添加新路由。这是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 routes = require('./routes/index');
var users = require('./routes/users');
var parse = require('./routes/parse');

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

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found - from app.js');
  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;

The lines i added are:

我添加的行是:

 var parse = require('./routes/parse');
   app.use('/parse', parse);

Then in the /routes directory i added parse.js that contains:

然后在/ routes目录中我添加了包含以下内容的parse.js:

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


router.get('/parse', function(req, res, next) {
  res.send('return from parse.js');
});

module.exports = router;

I can hit the / and the /users resource using a browser and i get the expected result. But when i try to hit /parse i get this 404:

我可以使用浏览器点击/和/ users资源,我得到了预期的结果。但是当我尝试命中/解析时,我得到这个404:

Not Found - from app.js

找不到 - 来自app.js

404

Error: Not Found - from app.js
    at /home/bitnami/parsecsvapi/app.js:32:13
    at Layer.handle [as handle_request] (/home/bitnami/parsecsvapi/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:312:13)
    at /home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:330:12)
    at next (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:271:10)
    at /home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:618:15
    at next (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:256:14)
    at Function.handle (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:176:3)
    at router (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:46:12)

1 个解决方案

#1


9  

Try /parse/parse in the URL

尝试/解析/解析URL

If you would like to use /parse, please use / instead of /parse in /routes/parse.js

如果你想使用/ parse,请在/routes/parse.js中使用/而不是/ parse

Please refer /routes/index.js & /routes/users.js.

请参考/routes/index.js和/routes/users.js。

TL;DR;

//app.js
var parse = require('./routes/parse');
app.use('/parse', parse);

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

// routes/parse.js
// the URL pattern will be used in conjunction with how its is being used
// in this case it would be '/parse' (from app.js) + '/parse' (as used in the next line)
// resulting in '/parse/parse' 
router.get('/parse', function(req, res, next) {
  res.send('return from parse.js');
});

module.exports = router;

#1


9  

Try /parse/parse in the URL

尝试/解析/解析URL

If you would like to use /parse, please use / instead of /parse in /routes/parse.js

如果你想使用/ parse,请在/routes/parse.js中使用/而不是/ parse

Please refer /routes/index.js & /routes/users.js.

请参考/routes/index.js和/routes/users.js。

TL;DR;

//app.js
var parse = require('./routes/parse');
app.use('/parse', parse);

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

// routes/parse.js
// the URL pattern will be used in conjunction with how its is being used
// in this case it would be '/parse' (from app.js) + '/parse' (as used in the next line)
// resulting in '/parse/parse' 
router.get('/parse', function(req, res, next) {
  res.send('return from parse.js');
});

module.exports = router;