API調用我建設使用意味着reddit的式的Web應用程序如下this Thinkster tutorial。404在Thinkster MEAN堆棧教程
教程指示使用curl
創建一個新的職位。
C:\root>curl --data "title=test&link=http://test.com" http://localhost:3000/posts
不過,我得到這個錯誤響應:
<h1>Not Found</h1>
<h2>404</h2>
<pre>Error: Not Found
at C:\root\app.js:30:13
at Layer.handle [as handle_request] (C:\root\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\root\node_modules\express\lib\router\index.js:312:13)
at C:\root\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\root\node_modules\express\lib\router\index.js:330:12)
at next (C:\root\node_modules\express\lib\router\index.js:271:10)
at C:\root\node_modules\express\lib\router\index.js:618:15
at next (C:\root\node_modules\express\lib\router\index.js:256:14)
at Function.handle (C:\root\node_modules\express\lib\router\index.js:176:3)
at router (C:\root\node_modules\express\lib\router\index.js:46:12)</pre>
我從這些文件看,但所有的引用都只是帶我發生了什麼錯誤.. 。
這是部分代碼
個/routes/index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = 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);
})
})
/models/Posts.js:
var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
title: String,
link: String,
upvotes: {type: Number, default: 0},
comments: [{type: mongoose.Schema.Types.ObjectId, ref: 'Comment'}]
});
mongoose.model('Post', PostSchema);
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 index = 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('/', index);
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 handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
var mongoose = require('mongoose');
require('./models/Posts');
require('./models/Comments');
mongoose.connect('mongod:localhost/news');
我覺得我所做的一切是正確的,除了我不確定將其他路線放在路線索引文件中的位置,以及類似的app.js
。但是這對我來說似乎可以忽略不計。我進行了所有我能想到的調試,但都卡住了。
還應當指出的是,本教程可能不適合在Windows機器上使用。我必須安裝curl
並做一些其他的事情,這些事情在Linux或Mac上可能不是必需的。我正在使用Windows 10.
我是從這個問題的計提和刪除我的答案,我不知道是什麼原因造成的問題。我給你的建議是要更多地瞭解你的程序中發生的事情,而不是僅僅關注應用程序生成器和教程,而不知道它們的工作方式或工作方式。另外,當人們要求你的服務器啓動的代碼(在你的app.js中)時,最好不要花19個小時來提供它。祝你好運解決您的問題。 – jfriend00
@ jfriend00我只是想獲得一個簡單的API工作。我想我會在學習本教程時學習。我還應該怎樣學習這些東西? –
如果你不知道你的代碼在做什麼以及它是如何工作的,那麼當某些事情停止工作時,你將不知道如何修復或調試。我個人從一個更簡單的手工構建應用程序開始,並逐個添加了一些內容,因爲我瞭解它們的工作方式以及它們如何吸引應用程序。我沒有從一個由路由器和數據庫生成器創建的多文件應用程序開始,直到我理解了Express中請求路由的基礎知識。你已經嘗試了10個步驟,不知道9個之前的任何一個工作。 – jfriend00