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