MEAN Stack 应用程序:无法获得正确的路线

MEAN Stack App: Can not get my routes right

我目前正在使用 MEAN 堆栈创建一个简单的应用程序,但我无法获得正确的路线。 localhost:3000 将我带到 404 页面。此外,当我转到 localhost:3000 时,根路径应该是 localhost:3000/#/home 并且它应该显示帖子。我认为这是我的路线的问题,因为帖子正在显示,如果我转到 localhost:3000/posts,它会以 JSON 格式列出帖子。我希望根索引显示帖子但不是 JSON 格式。

这是页面上打印出的错误。

 at /Users/ipbyrne/flapper-news-1stattempt/app.js:36:13
at Layer.handle [as handle_request] (/Users/ipbyrne/flapper-news-
1stattempt/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:312:13)
    at /Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:271:10)
    at /Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:618:15
    at next (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:256:14)
    at Function.handle (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:176:3)
    at router (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:46:12)
    at Layer.handle [as handle_request] (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:312:13)
    at /Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/lib/router/index.js:271:10)
    at SendStream.error (/Users/ipbyrne/flapper-news-1stattempt/node_modules/express/node_modules/serve-static/index.js:120:7)

这是我的 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 mongoose = require('mongoose');
require('./models/Posts');
require('./models/Comments');

mongoose.connect('mongodb://localhost/news');

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', 'ejs');

// 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;

这是我的路线文件

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Post = mongoose.model('Post');
var Comment = mongoose.model('Comment');

router.get('/posts', function(req, res, next) {
  Post.find(function(err, posts){
    if(err){ return next(err); }

    res.json(posts);
  });
});

router.post('/posts', function(req, res, next) {
  var post = new Post(req.body);

  post.save(function(err, post){
    if(err){ return next(err); }

    res.json(post);
  });
});

router.param('post', function(req, res, next, id) {
  var query = Post.findById(id);

  query.exec(function (err, post) {
    if(err){return next(err);}
    if(!post){return next(new Error('can\'t find post')); }

    req.post = post;
    return next();
  });
});
router.param('comment', function(req, res, next, id) {
  var query = Comment.findById(id);

  query.exec(function (err, comment) {
    if(err){return next(err);}
    if(!comment){return next(new Error('can\'t find comment')); }

    req.comment = comment;
    return next();
  });
});

router.get('/posts/:post', function(req, res, next) {
  req.post.populate('comments', function(err, post) {
    if(err) {return next(err);}

    res.json(req.post);
  });
});

router.put('/posts/:post/upvote', function(req, res, next) {
  req.post.upvote(function(err, post) {
    if(err) {return next(err) }

    res.json(post);
  });
});

router.put('/posts/:post/comments/:comment/upvote', function(req, 

res, next) {

  req.comment.upvote(function(err, comment) {
    if(err) {return next(err) }

    res.json(comment);
  });
});

router.post('/posts/:post/comments', function(req, res, next) {
  var comment = new Comment(req.body);
  comment.post = req.post;

  comment.save(function(err, comment) {
    if(err){return next(err); }

    req.post.comments.push(comment);
    req.post.save(function(err, post) {
      if(err){return next(err);}

      res.json(comment);
    });
  });
});
module.exports = router;

任何关于我如何解决这个问题的意见都将不胜感激。如果您还需要说出任何其他文件也可以帮助解决问题,请告诉我。

提前致谢。

您收到 404 错误,因为 Express 在 "routes" 子应用程序中找不到路线。尝试将以下内容添加到 ./routes/index:

router.get('/', function(req, res, next) {
    res.json({route: 'this is the home page'});
});

如果您想收到 html 页面,请将其放入 app.js:

app.get('/', function(req, res){
    res.render("home.html");
});