Express JS 404中的新路由错误

时间:2016-03-25 17:41:04

标签: node.js express

尝试使用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;

我添加的行是:

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

然后在/ 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;

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

未找到 - 来自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 个答案:

答案 0 :(得分:10)

/parse/parse

中尝试URL

如果您想使用/parse,请在/

中使用/parse代替/routes/parse.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;