404简单的Express路由错误

我正在尝试使用Express.js和Mongoose.js在我的Node.js应用程序中使用本教程设置路由 – https://thinkster.io/mean-stack-tutorial/当我用cURLtesting时,它给了我这个消息:

(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正在运行,所以我真的不知道发生了什么事情。 让我知道,如果任何其他信息会有所帮助。 在此先感谢您的帮助。

       

网上收集的解决方案 "404简单的Express路由错误"

我相信你的curl脚本应该包括在网站上的引用:

curl –data“title = test&link = http://test.com ”“ http:// localhost:3000 / posts ”

您需要在明文之前定义mongoose:

 var mongoose = require('mongoose'); require('./models/Posts'); require('./models/Comments'); mongoose.connect('mongodb://localhost/news'); var express = require('express'); var path = require('path'); ... 

我正在按照相同的教程,并得到相同的错误stucked,然后我刚刚重新启动节点服务器,它工作得很好。