2017-01-10 18 views
0

Я пытаюсь загрузить видео с клиента android/IOS на сервер NodeJS. Он отлично работает для небольших видеороликов, но когда я пытаюсь загрузить видео, скажем, более 50 МБ, он выдает ошибка тайм-аута сервера.Ошибка таймаута сервера при загрузке видео с Android/IOS на сервер Node

Одно из возможных решений, на мой взгляд, заключается в увеличении предела таймаута сервера, но это не похоже на правильное решение. Есть ли способ загрузить видео с Android без каких-либо ограничений?

Вот код, который я использую.

exports.create = function(req, res) { 
    req.setTimeout(0); 

    var storage = multer.diskStorage({ 
     destination: function (req, file, cb) { 
      cb(null, config.uploads.videoUpload.dest); 
     }, 
     filename: function (req, file, cb) { 
      let extArray = file.mimetype.split("/"); 
      let extension = extArray[extArray.length - 1]; 
      cb(null, Date.now()+ '.' +extension); 
     } 
    }); 

    var upload = multer({ 
     storage: storage, 
     limits: config.uploads.videoUpload.limits, 
     fileFilter: function (req, file, cb) 
     { 
      if (file.mimetype !== 'image/jpeg' && file.mimetype !== 'video/mp4') 
      { 
       return res.status(400).send({ 
        message: 'Only video files are allowed!', 
        error: true 
       }); 
      } 
      cb(null, true); 
     } 
    }).single('video_file'); 

    if (user) 
    { 
     // upload function with a callback 
     upload(req, res, function(uploadError) { 
      if (uploadError) 
      { 
       return res.status(400).send({ 
        message: 'Error occurred while uploading Video', 
        error: true 
       }); 
      } 
      else 
      { 
       return res.status(200).send({ 
        message: 'Video uploaded Successfuly!', 
        error: false 
       }); 
      } 
     }); 
    } 
    else 
    { 
     res.status(400).send({ 
      message: 'User is not signed in', 
      error: true 
     }); 
    } 
}; 
+1

Проверьте, если это http://stackoverflow.com/questions/25332561/node-js-express-large-body-for-bodyparser помогает. –

ответ

1

Этот тип ошибки часто связана с конфигурацией сервера или сети, а не код, так что стоит проверить этот конфиг также, если возможно, пытаясь с рабочего файла ноу загрузить пример на том же сервере также ,

Для Мулера подхода узла, следующий код проверяется и, безусловно, работать для больших загрузок видео между Android и сервером:

// POST: video upload route 
// multer approach 
var multer = require('multer'); 
app.use(multer({ 

    //Set dstination directory 
    dest: path.resolve(__dirname, 'public', 'uploaded_videos'), 

    //Rename file 
    rename: function (fieldname, filename) { 
     //Add the current date and time in ISO format, removing the last 'Z' character this usually 
     //includes 
     var dateNow = new Date(); 
     return filename + "_" + dateNow.toISOString().slice(0,-1) 
    }, 

    //Log start of file upload 
    onFileUploadStart: function (file) { 
     console.log(file.originalname + ' is starting ...') 
    }, 

    //Log end of file upload 
    onFileUploadComplete: function (file) { 
     console.log(file.originalname + ' uploaded to ' + file.path) 
     done=true; 
    } 

})); 

router.post('/web_video_upload', function(req, res) { 
    //Log the request details 
    //Debug console.log(req.body); 
    //Debug console.log(req.files); 

    //Send a resposne 
    res.send('Video Uploading'); 
    console.dir(req.files); 
});