node.js 快速路由器 CRUD API。不能删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32957123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Express Router CRUD API. Cannot DELETE
提问by Rodmentou
Well, I'm reading the MEAN Machine book and following it's examples. I'm trying to figure out what's wrong with my code so it won't make any DELETE request. GET, PUT and POST works as should.
好吧,我正在阅读 MEAN Machine 一书并遵循它的示例。我试图找出我的代码有什么问题,所以它不会发出任何 DELETE 请求。GET、PUT 和 POST 正常工作。
I have this code on my server.js:
我的 server.js 上有这个代码:
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization');
next();
});
var apiRouter = express.Router();
apiRouter.route('/users/:user_id')
.get( function (req, res) {
User.findById( req.params.user_id, function (err, user) {
if (err) res.send (err);
res.json(user);
});
})
.put( function (req, res) {
User.findById(req.params.user_id, function (err, user) {
if (err) res.send(err);
if (req.body.name) user.name = req.body.name;
if (req.body.username) user.username = req.body.username;
if (req.body.password) user.password = req.body.password;
user.save( function (err){
if (err) send (err);
res.json({message: 'User updated'});
});
})
.delete( function (req, res) {
User.remove({
_id: req.params.user_id
}, function (err, user) {
if (err) return res.send(err);
res.json({ message: 'Deleted' });
});
});
});
});
I have a set of users the Modulus MongoDB database and, when I try to use POSTMAN with localhost:8080/api/users/5610e5576d827dc41fb8e6e, POSTMAN says
我有一组用户使用 Modulus MongoDB 数据库,当我尝试将 POSTMAN 与 localhost:8080/api/users/5610e5576d827dc41fb8e6e 一起使用时,POSTMAN 说
Cannot DELETE /api/users/5610e5576d827dc41fb8e6e
while my Node server with Morgan says
而我的 Node 服务器与 Morgan 说
DELETE /api/users/5610e5576d827dc41fb8e6e 404
Why I'm getting a 404? What Am I doing wrong?
为什么我会收到 404?我究竟做错了什么?
回答by hassansin
You've placed closing brackets of put()in wrong place. So you're defining your deleterouter inside the put()router:
您put()在错误的位置放置了右括号。所以你在delete路由器中定义你的put()路由器:
This is your code after proper indentation:
这是适当缩进后的代码:
.put( function (req, res) {
User.findById(req.params.user_id, function (err, user) {
if (err) res.send(err);
if (req.body.name) user.name = req.body.name;
if (req.body.username) user.username = req.body.username;
if (req.body.password) user.password = req.body.password;
user.save( function (err){
if (err) send (err);
res.json({message: 'User updated'});
});
})
.delete( function (req, res) { // <===== defined inside 'put',
User.remove({
_id: req.params.user_id
}, function (err, user) {
if (err) return res.send(err);
res.json({ message: 'Deleted' });
});
});
})
So, just move the deleterouter outside of putrouter callback
所以,只需将delete路由器移到put路由器回调之外

