2017-08-27 102 views
3

我有這樣的代碼,其中所述客戶端文件上載到服務器通過一個AJAX POST請求,然後將服務器的上傳文件到雲(cloudinary ),並在上傳完成後響應AJAX請求。AJAX淨:: ERR_EMPTY_RESPONSE等待響應2分鐘後 - 的node.js服務器

問題當文件上載需要更長的時間超過2分鐘(ⅰ定時它,因爲直到該錯誤的請求的開始時)發生。

一切工作正常,如果上傳只需不到2分鐘,而這需要更長的時間超過2分鐘上傳的,沒有問題的錯誤後完成。但客戶在2分鐘時收到空的答覆。

服務器端代碼:

router.route('/posts').post(middleware.isLoggedIn, function (req, res) { 
    var form = new multiparty.Form() 
    form.parse(req, function (err, fields, files) { 
    if (err) return err 
    cloudinary.v2.uploader.upload(files.content[0].path, { resource_type: 
    'auto' }, function (err, result) { 
     if (err) return err 
     console.log(result) 
     res.json({ result: result }) 
    }) 
}) 

客戶端代碼:

function newPost (type, title, content) { 
    if (type === 'image') { 
    $('#newImageForm').addClass('loading') 
    } else if (type === 'video') { 
    $('#newVideoForm').addClass('loading') 
    } else if (type === 'gif') { 
    $('#newGifForm').addClass('loading') 
    } 
    var form = new FormData() 
    form.append('content', content) 
    form.append('type', type) 
    form.append('title', title) 
    $.ajax({ 
    type: 'POST', 
    url: '/posts', 
    data: form, 
    processData: false, 
    contentType: false, 
    timeout: 0, 
    success: function (response) { 
     if (type === 'image') { 
     $('#newImageForm').removeClass('loading') 
     $('#newImageForm').fadeOut() 
     $('#imageTitle').val('') 
     $('#image').val('') 
     } else if (type === 'video') { 
     $('#newVideoForm').removeClass('loading') 
     $('#videoTitle').val('') 
     $('#video').val('') 
     $('#newVideoForm').fadeOut() 
     } else if (type === 'gif') { 
     $('#newGifForm').removeClass('loading') 
     $('#gifTitle').val('') 
     $('#gif').val('') 
     $('#newGifForm').fadeOut() 
     } 
     successMessage(response._id) 
    }, 
    error: function (XMLHttpRequest, textStatus, errorThrown) { 
     errorMessage() 
    } 
    }) 
} 

回答