Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js文件上载在服务器上运行缓慢_Javascript_Angularjs_Node.js_Nginx_Pm2 - Fatal编程技术网

Javascript Node.js文件上载在服务器上运行缓慢

Javascript Node.js文件上载在服务器上运行缓慢,javascript,angularjs,node.js,nginx,pm2,Javascript,Angularjs,Node.js,Nginx,Pm2,我正在尝试使用angular.js中的http post请求和node.js中的app.post中的接收文件,将文件作为表单数据以及一些字段发送。文件发送在本地主机上工作正常。正如他们所说,强大的上传文件速度为500 mb/秒,但在服务器上,当我试图发送一个5到10 mb的文件时,需要40到80秒。请检查我的执行中是否存在任何问题。 我在服务器上使用nginx和pm2 Node.js代码: // route for uploading audio asynchronously app.post(

我正在尝试使用angular.js中的http post请求和node.js中的app.post中的接收文件,将文件作为表单数据以及一些字段发送。文件发送在本地主机上工作正常。正如他们所说,强大的上传文件速度为500 mb/秒,但在服务器上,当我试图发送一个5到10 mb的文件时,需要40到80秒。请检查我的执行中是否存在任何问题。 我在服务器上使用nginx和pm2

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代码:

// =========================================== 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);
            });
        }
    }
};
/===========================================================================================================================音频发送代码=====================
$scope.$watch('musicFiles',function(){
$scope.sendAudio($scope.musicFiles);
});
//打开“在音乐上发送音乐文件”图标,单击“新建”窗口
$scope.openClickMusic=函数(msg){
$http.post($rootScope.baseUrl+“/v1/getfile”,msg).success(函数(响应){
如果(!response.isExpired){
open($rootScope.baseUrl+'/'+response.serverfilename,“_blank”);
}否则{
var html='

'+response.expmsg+'

'; 如果($(“.chat box”).has(“p”).length<1){ $(html).hide().prependTo(“.chat box”).fadeIn(1500); $('#alert')。延迟(1000)。淡出('slow',function(){ $(“#警报”).remove(); }); } } }); } //接收新的音乐信息 $socket.on(“新消息音乐”,函数(数据){ if(data.username==$rootScope.username){ data.ownMsg=true; data.dwimgsrc=“app/images/spin.gif”; }否则{ data.ownMsg=false; } if((data.username=$rootScope.username)&&data.repeatMsg){ 检查音乐(数据); }否则{ $scope.messeges.push(数据); } }); //在音乐信息发送给所有人后,更换发送者信息中的旋转轮。 功能检查音乐(msg){ 对于(变量i=($scope.messeges.length-1);i>=0;i--){ if($scope.messeges[i].hasFile){ if($scope.messeges[i].istype==“音乐”){ if($scope.messeges[i].dwid==msg.dwid){ $scope.messeges[i].showme=true; $scope.mesges[i].serverfilename=msg.serverfilename; $scope.mesges[i].filename=msg.filename; $scope.mesges[i].size=msg.size; $scope.messeges[i].dwimgsrc=“app/images/musicplay_icon.png”; 打破 } } } }; } //下载音乐文件(如果服务器上存在),否则返回错误消息 $scope.downloadMusic=功能(ev,elem){ var search_id=elem.id; 对于(变量i=($scope.messeges.length-1);i>=0;i--){ if($scope.messeges[i].hasFile){ if($scope.messeges[i].istype==“音乐”){ if($scope.messeges[i].dwid==search\u id){ $http.post($rootScope.baseUrl+“/v1/getfile”,$scope.messeges[i]).success(函数(响应){ 如果(!response.isExpired){ 变量linkID=“#”+搜索id+“A”; $(linkID)。查找('i')。单击(); 返回true; }否则{ var html='

'+response.expmsg+'

'; 如果($(“.chat box”).has(“p”).length<1){ $(html).hide().prependTo(“.chat box”).fadeIn(1500); $('#alert')。延迟(1000)。淡出('slow',function(){ $(“#警报”).remove(); }); } 返回false; } }); 打破 } } } }; } //验证“音乐文件”功能的文件类型 $scope.validateMP3=函数(文件){ if(file.type==“audio/mp3”| | file.type==“audio/mpeg”){ 返回true; }否则{ var html='

选择MP3。

'; 如果($(“.chat box”).has(“p”).length<1){ $(html).hide().prependTo(“.chat box”).fadeIn(1500); $('#alert')。延迟(1000)。淡出('slow',function(){ $(“#警报”).remove(); }); } 返回false; } } //发送新的“音乐文件”功能 $scope.sendAudio=函数(文件){ if(files&&files.length){ $scope.isFileSelected=true; 对于(var i=0;i