[英]POST http://localhost:3000/upload 500 (Internal Server Error)
I have an error when I send data from client side (angular.js) to server side (node.js). 从客户端(angular.js)向服务器端(node.js)发送数据时出现错误。 I created a form that the user insert data then my controller get the data and sent it to the server to save the data in mongoose and s3(amazon).
我创建了一个表单,供用户插入数据,然后我的控制器获取数据并将其发送到服务器,以将数据保存在mongoose和s3(amazon)中。 I must say that it works fine - I mean I can save all the information I need, I can see it in my DB and I can see the image on s3
我必须说它可以正常工作-我的意思是我可以保存所有需要的信息,可以在数据库中看到它,也可以在s3上看到图像
but, I get an error : POST http://localhost:3000/upload 500 (Internal Server Error)
但是,我收到一个错误:
POST http://localhost:3000/upload 500 (Internal Server Error)
my html form: 我的html表格:
<html ng-app="mymed">
<head>
<title>insert</title>
</head>
<body ng-controller="tryController">
<main>
<body>
<form class="form-group" enctype="multipart/form-data" method="POST" action="http://localhost:3000/upload">
<label for="inputEmail3" class="col-sm-2 control-label">Title:</label>
<input class="form-control" type="text" placeholder="Title" ng-model="Title" name="Title" required></input>
</div>
<div class="col-sm-10">
<br>
<input type="file" name="file" accept="image/*" ng-modle="file"></input>
</div>
</div>
<input type="submit" class="btn btn-default" name="send" ng-click="createInsert()"></input>
</form>
.....
<script src="js/appController.js"></script>
my controller: 我的控制器:
mymedical.controller('tryController',['$scope','$http','$cookies', function($scope,$http,$cookies){
$scope.createInsert = function(){
var data = {};
data.Title = $scope.Title;
data.file = $scope.file;
$http.post('http://localhost:3000/upload', JSON.stringify(data)).then() //callback
}
}]);
sever side: 切断侧:
exports.saveDatatry=function(request, response){
console.log(request.body);
var file = request.files.file;
var hash = hasher();
var stream = fs.createReadStream(file.path)
var mimetype = mime.lookup(file.path);
console.log(stream);
var req;
if (mimetype.localeCompare('image/jpeg')
|| mimetype.localeCompare('image/pjpeg')
|| mimetype.localeCompare('image/png')
|| mimetype.localeCompare('image/gif')) {
req = client.putStream(stream, hash+'.png',
{
'Content-Type': mimetype,
'Cache-Control': 'max-age=604800',
'x-amz-acl': 'public-read',
'Content-Length': file.size
},
function(err, result) {
var savePerTry = new personal({
Title: request.body.Title,
file: req.url
});
savePerTry.save(function(error, result) {
if (error) {
console.error(error);
} else {
console.log("saved!");
response.redirect('http://localhost:8080/tryinsert.html');
};
})
});
} else {
console.log(err);
}
}
What I need to do? 我需要做什么? Thanks,
谢谢,
Here's your problem: 这是您的问题:
function(err, result) {
var savePerTry = new personal({
Title: request.body.Title,
file: req.url
});
Every time you write something like: 每次您编写如下内容:
function(err, result) {
then the very next line should be: 那么下一行应该是:
if (err) {
// ...
}
When you don't handle the errors you will get 500 internal server errors and you will not know what's wrong. 当您不处理错误时,将收到500个内部服务器错误,并且您将不知道出了什么问题。
Always handle errors. 始终处理错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.