不能 POST /api/
Cannot POST /api/
我正在尝试使用 AngularJS 和 NodeJS 的 mongoose 模块将 Object
文件发送到服务器。我确实收到 Object
,但由于 Cannot POST /api/appointment/new
错误,它没有发送到服务器。
这是我的 HTML
文件:
<div class="row">
<div class="col-sm6 col-sm-offset-3">
<div class="row">
<strong>Date:</strong>
<label>
<input type="text" type="date" ng-model="new.date" class="form-control">
</label>
<strong>Name Surname:</strong>
<label>
<input type="text" ng-model="new.nameSurname" class="form-control">
</label>
</div>
<div class="row">
<strong>Case:</strong>
<label>
<input type="text" ng-model="new.description" class="form-control">
</label>
<strong>Contact number:</strong>
<label>
<input type="tel" ng-model="new.contactNumber" class="form-control">
</label>
<button ng-click='createAppointment()'>Create appointment!</button>
</div>
</div>
</div>
我的server.js文件:
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var app = express();
var newAppointment = require('./server/controllers/newAppointment.js');
mongoose.connect('mongodb://localhost:27017/dental');
app.use(bodyParser.json());
//noinspection JSUnresolvedVariable
app.use('/app', express.static(__dirname + '/app'));
//noinspection JSUnresolvedVariable
app.use('/node_modules', express.static(__dirname + '/node_modules'));
app.get('/', function(req, res){
res.sendfile('index.html');
});
//New appointment function
app.post('api/appointment/new', newAppointment.create);
app.listen('3000', function() {
console.log("Listening on :3000")
});
服务器控制器:
var mongoose;
mongoose = require('mongoose');
var appointment;
appointment = require('../datasets/appointment.js');
module.exports.create = function(req, response) {
console.log(req.body)
};
应用控制器:
(function() {
//noinspection JSUnresolvedFunction
angular.module('dentalAdviser')
.controller('appointmentController', ['$scope', '$state', '$http', function ($scope, $state, $http) {
$scope.createAppointment = (function() {
console.log($scope.new);
$http.post('api/appointment/new', $scope.new).success(function (response) {
}).error(function (error) {
console.log(error);
})
});
}])
}());
和 Object
的数据集:
var mongoose = require('mongoose');
module.export = mongoose.model('appointment', {
nameSurname: String,
date: String,
description: String,
contactNumber: String
});
这个:
app.post('api/appointment/new', newAppointment.create);
大概应该是:
app.post('/api/appointment/new', newAppointment.create);
我正在尝试使用 AngularJS 和 NodeJS 的 mongoose 模块将 Object
文件发送到服务器。我确实收到 Object
,但由于 Cannot POST /api/appointment/new
错误,它没有发送到服务器。
这是我的 HTML
文件:
<div class="row">
<div class="col-sm6 col-sm-offset-3">
<div class="row">
<strong>Date:</strong>
<label>
<input type="text" type="date" ng-model="new.date" class="form-control">
</label>
<strong>Name Surname:</strong>
<label>
<input type="text" ng-model="new.nameSurname" class="form-control">
</label>
</div>
<div class="row">
<strong>Case:</strong>
<label>
<input type="text" ng-model="new.description" class="form-control">
</label>
<strong>Contact number:</strong>
<label>
<input type="tel" ng-model="new.contactNumber" class="form-control">
</label>
<button ng-click='createAppointment()'>Create appointment!</button>
</div>
</div>
</div>
我的server.js文件:
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var app = express();
var newAppointment = require('./server/controllers/newAppointment.js');
mongoose.connect('mongodb://localhost:27017/dental');
app.use(bodyParser.json());
//noinspection JSUnresolvedVariable
app.use('/app', express.static(__dirname + '/app'));
//noinspection JSUnresolvedVariable
app.use('/node_modules', express.static(__dirname + '/node_modules'));
app.get('/', function(req, res){
res.sendfile('index.html');
});
//New appointment function
app.post('api/appointment/new', newAppointment.create);
app.listen('3000', function() {
console.log("Listening on :3000")
});
服务器控制器:
var mongoose;
mongoose = require('mongoose');
var appointment;
appointment = require('../datasets/appointment.js');
module.exports.create = function(req, response) {
console.log(req.body)
};
应用控制器:
(function() {
//noinspection JSUnresolvedFunction
angular.module('dentalAdviser')
.controller('appointmentController', ['$scope', '$state', '$http', function ($scope, $state, $http) {
$scope.createAppointment = (function() {
console.log($scope.new);
$http.post('api/appointment/new', $scope.new).success(function (response) {
}).error(function (error) {
console.log(error);
})
});
}])
}());
和 Object
的数据集:
var mongoose = require('mongoose');
module.export = mongoose.model('appointment', {
nameSurname: String,
date: String,
description: String,
contactNumber: String
});
这个:
app.post('api/appointment/new', newAppointment.create);
大概应该是:
app.post('/api/appointment/new', newAppointment.create);