简体   繁体   English

上载文件时Openshift NodeJS应用上的错误网关502

[英]Bad Gateway 502 on Openshift NodeJS app when uploading a file

i've got an Application based on NodeJS and AngularJS and pushed it to openshift. 我有一个基于NodeJS和AngularJS的应用程序,并将其推向openshift。 But everytime i try to upload something, i get the following error: 但是每次我尝试上传某些内容时,都会出现以下错误:

POST http://www.domain.de/api/upload/file 502 (Bad Gateway) 

Angular sends the data like this: Angular发送数据如下:

$scope.newFile = function() {
    $scope.id = $scope.group._id;
    var fd = new FormData();
    var file = $scope.files[0];
    fd.append('file', file);
    if (file.type!="application/pdf"){
        mvNotifier.error("Nur PDF Dateien sind akzeptiert.");
        return;
    }       
    $http.post('/api/upload/file', fd, {
        transformRequest: angular.identity,
        headers:{'Content-Type': undefined}
    })
    .success(function(d) {
        var data = {
            name: file.name,
            description: $scope.descriptionfile
        }
        mvNotifier.notify("Bis hier hin klappt alles");
        console.log("sucess on uploading ");

        mvFactory.POST(data, mvGroup, {_place:"file", _id:$scope.id}).then(function(data) {
            $scope.newfile=false;
            $scope.group.files.push({name:file.name, description:$scope.descriptionfile});
            mvNotifier.notify("Datei hochgeladen");
        }, function(reason) {
            mvNotifier.error("reason");
        })
    })
    .error(function(data,status,header) {
        mvNotifier.error("Upload hat nicht funktioniert.")
        console.log("data", data);
        console.log("status", status);
        console.log("header", header);
    })

And the Server routes to a file using busboy to save it: 然后服务器使用busboy将其路由到文件以保存该文件:

uploadFile: function(req,res) {
    console.log("req",req.files);
    if (process.env.OPENSHIFT_DATA_DIR!= undefined) {
        var cPath = process.env.OPENSHIFT_DATA_DIR;
    } else {
        var cPath = path.resolve('..', 'data');
    }
    var busboy = new Busboy({ headers: req.headers });
    req.pipe(busboy);
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        var wPath = cPath + '/uploads/documents';
        file.pipe(fs.createWriteStream(wPath + '/' + filename));

        file.on('end', function() {
            console.log('File [' + fieldname + '] Finished');
        });
    });
    busboy.on('finish', function() {
        console.log('Done parsing form!');
    });
    res.status(200).end();

}

On Localhost everything is working just fine and the data is saved to the server. 在Localhost上,一切正常,数据已保存到服务器。 but i get the response of bad gateway and this header: 但是我得到网关错误和此标头的响应:

<title>502 Bad Gateway</title>
</head><body>
<h1>Bad Gateway</h1>
<p>The proxy server received an invalid response from an upstream server.<br />

Can someone help me please? 有人能帮助我吗?

I've resolved the issue. 我已经解决了这个问题。 I post it so that someone who has the problem can solve it to. 我发布它,以便有问题的人可以解决它。

The Web load balancer at openshift is HAproxy and has issues with the upload because the response of the server wasn't identical with the request because of the content type. openshift上的Web负载均衡器是HAproxy,并且由于内容类型服务器的响应与请求不同,因此上载有问题。 I switched from $http from angular to an XHR. 我从$ http从angular切换到XHR。 That solved the issue and works fine. 那解决了问题并且运作良好。 The content-type i didn't set at all. 我根本没有设置内容类型。 now it works fine. 现在工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM