我目前正在使用MEAN堆栈创建一个简单的应用程序,但我无法正确使用我的路由。 localhost:3000将我带到404页面。另外当我去localhost:3000时,根路径应该是localhost:3000 /#/ home,它应该显示帖子。我认为这是我的路线的问题,因为帖子出现了,如果我去localhost:3000 /帖子它列出了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;
如何解决此问题的任何意见将不胜感激。如果您还需要其他任何文件可以帮助解决问题,请告诉我。
提前致谢。
答案 0 :(得分:0)
您收到404错误,因为Express无法在“路线”子应用中找到该路线。尝试将以下内容添加到./routes/index:
pg_dump -U postgres mydb > "C:\Users\MyName\Desktop\backup"
如果您希望收到html页面,请将其放入app.js:
router.get('/', function(req, res, next) {
res.json({route: 'this is the home page'});
});