3
你好我正在使用MEAN棧進行簡單的應用程序。而我得到這個錯誤http.js:691拋出新的錯誤(「可以\」噸設置頭髮送之後「。)Node.js:http.js:691拋出新的錯誤('在發送後不能設置標題'。)
下面是代碼: server.js
// set up
var express = require('express');
var app = express();
var mongoose = require('mongoose');
// mongodb connection and app configuration
mongoose.connect('mongodb://localhost/test');
app.configure(function() {
app.use(express.static(__dirname + '/app'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
});
// define model
var Employee = mongoose.model('Employee', {
firstName : String,
lastName : String,
photoUrl : String
});
// routes ==========================
// api
// GET
app.get('/api/employees', function(req, res) {
console.log("GET");
Employee.find(function(err, employees) {
if(err) {
res.send(err);
}
res.json(employees);
});
});
// Get by ID
app.get('/api/employees/:employeeId', function(req, res) {
Employee.findById(req.params.employeeId, function(err, employee) {
if(err) {
res.send(err);
}
res.json(employee);
});
Employee.find(function(err, employees) {
if(err) {
res.send(err);
}
res.json(employees);
});
});
// POST
app.post('/api/employees', function(req, res) {
Employee.create({
firstName : req.body.firstName,
lastName : req.body.lastName,
photoUrl : req.body.photoUrl
}, function(err, employee) {
if(err) {
res.send(err);
}
Employee.find(function(err, employees){
if(err) {
res.send(err);
}
res.json(employees);
});
});
});
// DELETE
app.delete('/api/employees/:employee_id', function(req, res) {
Employee.remove({_id : req.params.employee_id}, function (err, employee) {
if(err) {
res.send(err);
}
Employee.find(function(err, employees) {
if(err) {
res.send(err);
}
res.json(employees);
})
});
});
// application
app.get('*', function(req, res) {
res.sendfile('./app/index.html');
});
// start listening (start app)
app.listen(8080);
console.log("App listening on port 8080");
而且還: app.js角路線
var employeesApp = angular.module('employeesApp', [
'ngRoute',
'EmployeesControllers',
'employeeAppAnimations'
]);
employeesApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/employees', {
templateUrl: 'partials/employees_list.html',
controller: 'EmployeesCtrl'
}).
when('/employees/:employeeId', {
templateUrl: 'partials/employees_details.html',
controller: 'EmployeeDetailsCtrl'
}).
otherwise({
redirectTo: '/employees'
});
}]);
非常感謝,它幫助了我很多:) – nikolakoco