0
我使用Node和Express开发了一个REST API。我正在尝试执行列表项目。每个todo
列表项都包含一个id和文本属性。我想要做的是有一个GET
路由,这将允许我通过传递id作为路由参数来获取特定的todo
列表项。我尝试了几次尝试,但无法看到我做错了什么。我的代码是:Express GET Route not working
我的数据库
var mongoose = require('mongoose');
module.exports = mongoose.model('Todo', {
text : String,
done : Boolean
});
// get all todos list items
app.get('/api/todoo', function(req, res) {
// use mongoose to get all todos in the database
Todo.find(function(err, todos) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.json(todos); // return all todos in JSON format
});
});
//Getting a todo list by ID
app.get('/api/todoo/:todo_id', function(req, res) {
Todo.find({id : req.params.todo_id},
function(err, todo) {
if (err)
res.send(err);
res.json(todos);
});
});
有什么问题?你遇到了什么错误? –
你把这个代码,如果不是,那么你应该 Module.exports =应用程序; –