POST http:// localhost:3000 / upload 500(内部服务器错误)

时间:2017-03-24 12:17:17

标签: javascript angularjs node.js html5 amazon-s3

当我从客户端(angular.js)向服务器端(node.js)发送数据时出错。 我创建了一个表单,用户插入数据然后我的控制器获取数据并将其发送到服务器以将数据保存在mongoose和s3(亚马逊)中。 我必须说它工作正常 - 我的意思是我可以保存我需要的所有信息,我可以在我的数据库中看到它,我可以在s3上看到图像

但是,我收到错误:POST http://localhost:3000/upload 500 (Internal Server Error)

我的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>

我的控制员:

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
        }
}]);

服务方:

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);
        }

}

我需要做什么? 谢谢,

1 个答案:

答案 0 :(得分:1)

这是你的问题:

        function(err, result) {
          var savePerTry = new personal({
              Title: request.body.Title,
              file: req.url
          });

每次你写下这样的话:

        function(err, result) {

那么下一行应该是:

            if (err) {
              // ...
            }

如果您没有处理错误,您将收到500个内部服务器错误,而您不会知道错误。

始终处理错误。