2015-04-22 118 views
4

我想設置路由使用Express.js和Mongoose.js使用本教程我的Node.js應用程序 - https://thinkster.io/mean-stack-tutorial/ 當我和捲曲測試,它給了我這個消息:404錯誤與簡單快速路由

(捲曲腳本是: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; 

而我的路線/ I ndex.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正在運行,所以我真的不知道發生了什麼。讓我知道是否有任何其他信息會有所幫助。在此先感謝您的幫助。

+0

你可以檢查node_modules文件夾並驗證是否安裝了cookie分析器? – tpie

+0

是的,當您啓動服務器時,安裝了Cookie解析器 – user1985569

+0

,您正在運行什麼?你使用快遞發電機來啓動這個項目嗎? – tpie

回答

0

你需要明確之前定義貓鼬:

var mongoose = require('mongoose'); 

require('./models/Posts'); 
require('./models/Comments'); 

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

var express = require('express'); 
var path = require('path'); 
... 
0

我按照相同的教程,並得到了同樣的錯誤stucked,然後我剛剛重新啓動節點服務器,它工作得很好。