我正在尝试使用Express.js和Mongoose.js在我的Node.js应用程序中设置路由,使用这个教程 - 学习使用MEAN构建现代Web应用程序
当我使用cURL进行测试时,它给出了以下消息:
(curl脚本是:
(curl脚本是:
curl --data 'title=test&link=http://test.com' http://localhost:3000/#/posts
)Error: Not Found
at /Users/adamz/flapper-news3/app.js:30:13
at Layer.handle [as handle_request] (/Users/adamz/flapper-news3/node_modules/express/lib/router/layer.js:82:5)
at trim_prefix (/Users/adamz/flapper-news3/node_modules/express/lib/router/index.js:302:13)
at /Users/adamz/flapper-news3/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/Users/adamz/flapper-news3/node_modules/express/lib/router/index.js:321:12)
at next (/Users/adamz/flapper-news3/node_modules/express/lib/router/index.js:261:10)
at /Users/adamz/flapper-news3/node_modules/express/lib/router/index.js:603:15
at next (/Users/adamz/flapper-news3/node_modules/express/lib/router/index.js:246:14)
at Function.proto.handle (/Users/adamz/flapper-news3/node_modules/express/lib/router/index.js:166:3)
at router (/Users/adamz/flapper-news3/node_modules/express/lib/router/index.
以下是我的 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 mongoose = require('mongoose');
require('./models/Posts');
require('./models/Comments');
mongoose.connect('mongodb://localhost/news');
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(__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;
我的路由/Index.js如下:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Post = mongoose.model('Post');
var Comment = mongoose.model('Comment');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
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);
});
});
module.exports = router;
有什么问题吗?我直接从教程上复制粘贴了,Node.js正常运行,mongodb也在运行,所以我真的不知道出了什么问题。
如果需要其他信息,请告诉我。感谢您提前的帮助。