简体   繁体   English

Node.js强大的文件上传在服务器上运行缓慢

[英]Node.js formidable file upload working slow on server

I am trying to send files as form data along with some fields using http post request in angular.js and receiving file in app.post in node.js. 我正在尝试使用angular.js中的http post请求将文件作为表单数据以及一些字段发送出去,并在node.js中的app.post中接收文件。 The file sending works fine on localhost. 文件发送在本地主机上工作正常。 As they say formidable uploads files at 500 mb/sec speed but on server when I am trying to send a file of 5 to 10 mb it takes 40 to 80 seconds. 正如他们所说的那样,以500 mb / sec的速度上传文件非常强大,但是在服务器上,当我尝试发送5到10 mb的文件时,它需要40到80秒。 Please check is there any problem in my implementation. 请检查我的实施中是否有任何问题。 I am using nginx and pm2 on server. 我在服务器上使用nginx和pm2。

Node.js code: Node.js代码:

// route for uploading audio asynchronously
app.post('/v1/uploadAudio', function(req, res) {
    var userName, useravatar, hasfile, ismusicfile, isType, showMe, DWimgsrc, DWid, msgtime;
    var imgdatetimenow = Date.now();
    var form = new formidable.IncomingForm({
        uploadDir: __dirname + '/public/app/upload/music',
        keepExtensions: true
    });


    form.on('end', function() {
        res.end();
    });
    form.parse(req, function(err, fields, files) {
        console.log("files : ", files);
        console.log("fields : ", fields);
        var data = {
            username: fields.username,
            userAvatar: fields.userAvatar,
            repeatMsg: true,
            hasFile: fields.hasFile,
            isMusicFile: fields.isMusicFile,
            istype: fields.istype,
            showme: fields.showme,
            dwimgsrc: fields.dwimgsrc,
            dwid: fields.dwid,
            serverfilename: baseName(files.file.path),
            msgTime: fields.msgTime,
            filename: files.file.name,
            size: bytesToSize(files.file.size)
        };
        var audio_file = {
            dwid: fields.dwid,
            filename: files.file.name,
            filetype: fields.istype,
            serverfilename: baseName(files.file.path),
            serverfilepath: files.file.path,
            expirytime: imgdatetimenow + (120000)
        };
        files_array.push(audio_file);
        ios.sockets.emit('new message music', data);
    });
});

AngularJS code: AngularJS代码:

// =========================================== Audio Sending Code =====================
$scope.$watch('musicFiles', function() {
    $scope.sendAudio($scope.musicFiles);
});

//  opens the sent music file on music_icon click on new window
$scope.openClickMusic = function(msg) {
    $http.post($rootScope.baseUrl + "/v1/getfile", msg).success(function(response) {
        if (!response.isExpired) {
            window.open($rootScope.baseUrl + '/' + response.serverfilename, "_blank");
        } else {
            var html = '<p id="alert">' + response.expmsg + '</p>';
            if ($(".chat-box").has("p").length < 1) {
                $(html).hide().prependTo(".chat-box").fadeIn(1500);
                $('#alert').delay(1000).fadeOut('slow', function() {
                    $('#alert').remove();
                });
            }
        }
    });
}

// recieving new music message
$socket.on("new message music", function(data) {
    if (data.username == $rootScope.username) {
        data.ownMsg = true;
        data.dwimgsrc = "app/images/spin.gif";
    } else {
        data.ownMsg = false;
    }
    if ((data.username == $rootScope.username) && data.repeatMsg) {
        checkMessegesMusic(data);
    } else {
        $scope.messeges.push(data);
    }
});

// replacing spinning wheel in sender message after music message delivered to everyone.
function checkMessegesMusic(msg) {
    for (var i = ($scope.messeges.length - 1); i >= 0; i--) {
        if ($scope.messeges[i].hasFile) {
            if ($scope.messeges[i].istype === "music") {
                if ($scope.messeges[i].dwid === msg.dwid) {
                    $scope.messeges[i].showme = true;
                    $scope.messeges[i].serverfilename = msg.serverfilename;
                    $scope.messeges[i].filename = msg.filename;
                    $scope.messeges[i].size = msg.size;
                    $scope.messeges[i].dwimgsrc = "app/images/musicplay_icon.png";
                    break;
                }
            }
        }
    };
}

// download music file if it exists on server else return error message
$scope.downloadMusic = function(ev, elem) {
    var search_id = elem.id;
    for (var i = ($scope.messeges.length - 1); i >= 0; i--) {
        if ($scope.messeges[i].hasFile) {
            if ($scope.messeges[i].istype === "music") {
                if ($scope.messeges[i].dwid === search_id) {
                    $http.post($rootScope.baseUrl + "/v1/getfile", $scope.messeges[i]).success(function(response) {
                        if (!response.isExpired) {
                            var linkID = "#" + search_id + "A";
                            $(linkID).find('i').click();
                            return true;
                        } else {
                            var html = '<p id="alert">' + response.expmsg + '</p>';
                            if ($(".chat-box").has("p").length < 1) {
                                $(html).hide().prependTo(".chat-box").fadeIn(1500);
                                $('#alert').delay(1000).fadeOut('slow', function() {
                                    $('#alert').remove();
                                });
                            }
                            return false;
                        }
                    });
                    break;
                }
            }
        }
    };
}

// validate file type to 'music file' function
$scope.validateMP3 = function(file) {
    if (file.type == "audio/mp3" || file.type == "audio/mpeg") {
        return true;
    } else {
        var html = '<p id="alert">Select MP3.</p>';
        if ($(".chat-box").has("p").length < 1) {
            $(html).hide().prependTo(".chat-box").fadeIn(1500);
            $('#alert').delay(1000).fadeOut('slow', function() {
                $('#alert').remove();
            });
        }
        return false;
    }
}

// sending new 'music file' function
$scope.sendAudio = function(files) {
    if (files && files.length) {
        $scope.isFileSelected = true;
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            var dateString = formatAMPM(new Date());
            var DWid = $rootScope.username + "dwid" + Date.now();
            var audio = {
                username: $rootScope.username,
                userAvatar: $rootScope.userAvatar,
                hasFile: $scope.isFileSelected,
                isMusicFile: true,
                istype: "music",
                showme: false,
                dwimgsrc: "app/images/musicplay_icon.png",
                dwid: DWid,
                msgTime: dateString
            }

            $socket.emit('send-message', audio, function(data) { // sending new image message via socket
            });
            var fd = new FormData();
            fd.append('file', file);
            fd.append('username', $rootScope.username);
            fd.append('userAvatar', $rootScope.userAvatar);
            fd.append('hasFile', $scope.isFileSelected);
            fd.append('isMusicFile', true);
            fd.append('istype', "music");
            fd.append('showme', false);
            fd.append('dwimgsrc', "app/images/musicplay_icon.png");
            fd.append('dwid', DWid);
            fd.append('msgTime', dateString);
            fd.append('filename', file.name);
            $http.post('/v1/uploadAudio', fd, {
                transformRequest: angular.identity,
                headers: {
                    'Content-Type': undefined
                }
            }).then(function(response) {
                // console.log(response);
            });
        }
    }
};

I've used Formidable on a couple of side projects, and when uploading to localhost, I do see the 500mb/sec quoted capability (depending on physical hardware of the computer). 我在几个辅助项目中使用了Formidable,并且在上载到localhost时,确实看到了500mb / sec的引用能力(取决于计算机的物理硬件)。

However, when uploading a file over the internet, you are subject to the bandwidth limitations of your ISP upload speed as well as the download speed of your server. 但是,通过Internet上传文件时,您会受到ISP上传速度和服务器下载速度的带宽限制。

You report that a 10MB file takes ~80 seconds to upload to the server. 您报告一个10MB的文件需要大约80秒才能上传到服务器。 That's about 125KBps (or around 1 megabit/second) which seems fairly reasonable for a home/office ISP upload speed (depending on region of the world). 这大约是125KBps(或大约1兆比特/秒),对于家庭/办公室ISP的上传速度(取决于世界的地区)来说,这似乎是相当合理的。

A good way to eliminate your home/office network performance from the troubleshooting equation would be to write a node.js script that uploads a file several times and calculates an average speed. 从故障排除方程式中消除家庭/办公室网络性能的一种好方法是编写一个node.js脚本,该脚本会多次上传文件并计算平均速度。 Run that test file on your local computer, then try again from a different server in the cloud. 在本地计算机上运行该测试文件,然后从云中的其他服务器重试。

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

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