等待响应2分钟后的ajax net :: ERR_EMPTY_RESPONSE-node.js服务器 [英] ajax net::ERR_EMPTY_RESPONSE after waiting for response for 2 mins - node.js server

查看:190
本文介绍了等待响应2分钟后的ajax net :: ERR_EMPTY_RESPONSE-node.js服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,客户端通过AJAX POST请求将文件上传到服务器,然后服务器将文件上传到云(云),并在上传完成后响应AJAX请求.

I have this code where the client uploads a file to the server through an AJAX POST request and then the server uploads that file to a cloud (cloudinary), and responds to the AJAX request after the upload is completed.

当文件上载时间超过2分钟(从请求开始直到出现错误,我对它进行计时)时,就会发生此问题.

The problem occurs when the file upload takes longer than 2 minutes ( i timed it, since the beginning of the request till the error occurs).

如果上传时间少于2分钟,则一切工作正常,并且耗时超过2分钟的上传在错误发生后即已完成,没有问题.但客户会在2分钟后收到空响应.

Everything is working fine if the upload takes less than 2 minutes, And uploads that take longer than 2 minutes are completed after the error with no problem. but the client receives the empty response at the 2 minute mark.

服务器端代码:

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()
    }
  })
}

推荐答案

听起来像是您正在运行nodejs请求的内部超时. https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback

Sounds like you are running into the internal timeout of nodejs request. https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback

尝试使用req.setTimeout(10000);将其设置为更高的值,或者使用req.setTimeout(0)

Try setting it to something higher with req.setTimeout(10000); or disable it with req.setTimeout(0)

这篇关于等待响应2分钟后的ajax net :: ERR_EMPTY_RESPONSE-node.js服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆