我在我的应用程序中使用ExpressJS和AngularJS。当我尝试发帖时,req.body是空的。
快速
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
routes = require('./server/routes/routes'),
var port = process.env.PORT || 8080;
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
routes(app, passport);
app.listen(port);
发布
app.post('/api/employee', function(req, res) {
var employee = req.body;
console.log(employee);
// Create employee
Employee.addEmployee(employee, function(err, employee) {
if (err)
res.send(err);
res.json(employee);
});
});
的index.html
<form class="form-signin">
<h2 class="form-signin-heading">Novo serviço</h2>
<label for="inputPhone">Telefone:</label>
<input type="text" class="form-control" ng-model="employee.phone" name="phone" placeholder="" required>
<label for="inputCpf">CPF</label>
<input type="text" class="form-control" ng-model="employee.cpf" name="cpf" placeholder="" required>
<button class="btn btn-lg btn-primary btn-block" ng-click="createEmployee()" type="submit">Confirmar Serviço</button>
</form>
AngularJS
.factory('employeeProxy', ['$resource',
function($resource) {
return $resource('/api/employee/:id', {}, {
save: {
method: 'POST',
isArray: false
},
update: {
method: 'PUT',
isArray: true
},
delete: {
method: 'DELETE',
params: {
id: '@id'
},
isArray: true
},
query: {
method: 'GET',
params: {
id: '@id'
},
isArray: false
}
})
}
])
AngularJs控制器功能
$scope.createEmployee = function() {
employeeService.createEmployee($scope.employee, function(data) {
$scope.callbackMessage = data.message;
}, function(err) {
$scope.callbackMessage = err.data.message;
});
};
AngularJS服务
self.createEmployee = function(employee, successCallback, error) {
employeeProxy.save(employee, successCallback, error);
}
我错过了什么吗?我已经疯了。我不知道发生了什么。