使用快速REST API编辑post

我有一个与mongoose和mongoDBangular度expression的应用程序

对于angular部分我有:

.controller('EditPostCtrl', ['$scope', '$http', '$location', '$routeParams', function($scope, $http, $location, $routeParams){ // populate the form $scope.form = {}; $http.get('/api/post/' + $routeParams.post_id) .success(function(data){ $scope.form = data; }); // save changes and redirect $scope.editPost = function(){ $http.put('/api/post/' + $routeParams.post_id, $scope.form) .success(function(data){ $location.url('/post/' + $routeParams.post_id); }); }; }]) 

那么对于快件我有一个路线:

 app.put('/api/post/:post_id', posts.editPost); exports.editPost = function(req, res){ var updatedPost = { title: req.body.title, text: req.body.text, created: req.body.created }; Post.update({ _id: req.body._id }, updatedPost, function(err, affected){ console.log('affected %d', affected); }); }; 

我启动服务器后,我可以更新post,但编辑后,我不会redirect到'/post/' + $routeParams.post_id像我声明在angular度。 我在editPost函数中需要什么?

您需要发送并回答客户端,例如,所有内容都已更新,但是没有内容被返回(204):

 exports.editPost = function(req, res){ var updatedPost = { title: req.body.title, text: req.body.text, created: req.body.created }; Post.update({ _id: req.body._id }, updatedPost, function(err, affected){ console.log('affected %d', affected); //returns with no body, but OK: //res.send(204); //returns with update entity as body: res.send(200, updatedPost); }); }; 

另请参阅Express API 。