不能POST / api节点

我无法从AngularJs表单向我的MongoDB数据库发布响应。 这里是代码片段

控制器

angular.module('myapp') .controller('AddCtrl', function($scope, $alert, Event) { $scope.addEvent = function() { Event.save({ eventName: $scope.eventName }).$promise .then(function() { $scope.eventName = ''; $scope.addForm.$setPristine(); $alert({ content: 'Event has been added.' }); }) .catch(function(response) { $scope.eventName = ''; $scope.addForm.$setPristine(); $alert({ content: response.data.message }); }); }; }); 

表单模板(html)

 <div class="container"> <div class="panel panel-default"> <div class="panel-heading">Add Events</div> <div class="panel-body"> <form class="form" method="post" ng-submit="addEvent()" name="addForm"> <div class="form-group" ng-class="{ 'has-error' : addForm.eventName.$invalid && addForm.eventName.$dirty }"> <label class="control-label">Name</label> <input class="form-control" type="text" name="eventName" ng-model="eventName" placeholder="Select Event Name Here" required autofocus> <div class="help-block text-danger" ng-if="addForm.eventName.$dirty" ng-messages="addForm.eventName.$error"> <div ng-message="required">Event name is required.</div> </div> </div> <button class="btn btn-primary" type="submit" ng-disabled="addForm.$invalid">Add</button> </form> </div> </div> </div> 

app.post()方法

 app.post('/api/events', function (req, res, next) { var event = new Event(); // create a new instance of the Event model event.name = req.body.eventName; // set the event name (comes from the request) event.save(function(err) { if (err) res.send(err); res.json({ message: 'Event Added!' }); }); }); 

事件模式

 var eventSchema = new Schema({ name: String }); var Event = mongoose.model('Event', eventSchema); 

我似乎无法弄清楚为什么我得到POST / API /事件404错误?

编辑:添加GET / api /事件路由了

 app.get('/api/events', function(req, res, next) { Event.find(function(err, events) { if (err) res.send(err); res.json(events); }); }); app.get('/api/events/:id', function(req, res, next) { Event.findById(req.params.id, function(err, event) { if (err) return next(err); res.send(event); }); }); 

浏览器日志:

 Error: [$injector:unpr] Unknown provider: EventProvider <- Event http://errors.angularjs.org/1.3.0-beta.17/$injector/unpr?p0=EventProvider%20%3C-%20Event at http://localhost:8080/vendor/angular.js:78:12 at http://localhost:8080/vendor/angular.js:3894:19 at Object.getService [as get] (http://localhost:8080/vendor/angular.js:4027:39) at http://localhost:8080/vendor/angular.js:3899:45 at getService (http://localhost:8080/vendor/angular.js:4027:39) at invoke (http://localhost:8080/vendor/angular.js:4059:13) at Object.instantiate (http://localhost:8080/vendor/angular.js:4079:23) at http://localhost:8080/vendor/angular.js:7460:28 at link (http://localhost:8080/vendor/angular-route.js:913:26) at nodeLinkFn (http://localhost:8080/vendor/angular.js:6841:13) <div ng-view="" class="{{pageClass}} ng-scope"> 

看起来你正试图与angular度前端控制器分享你的mongoose模型。 他们彼此独立。

我修改了angular controller ,使用$http provider/api/events端点发送POST请求。

 angular.module('myapp') .controller('AddCtrl', function($scope, $alert, $http) { $scope.addEvent = function() { $http.post('/api/events', { eventName: $scope.eventName }).success(function(response) { $scope.eventName = ''; $scope.addForm.$setPristine(); $alert({ content: 'Event has been added.' }); }).error(function(response) { $scope.eventName = ''; $scope.addForm.$setPristine(); $alert({ content: response.data.message }); }); }; }); 

回答我自己的问题:

我创build了一个工厂服务

 angular.module('myapp') .factory('Event', function($resource) { return $resource('/api/events/:_id'); }); 

现在工作正常。