使用AXIOS发送大块POST请求时是否可以检测到即时请求 [英] Is it possible to detect an immediate when sending a chunky POST request with AXIOS

查看:174
本文介绍了使用AXIOS发送大块POST请求时是否可以检测到即时请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Web客户端中使用AXIOS,以便将文件作为上传文件发布到Express后端.由于文件的大小和客户端用户的带宽都是可变的,因此完成POST请求可能需要一定的时间.在后端,一些逻辑适用,并且请求被立即拒绝.

I am using AXIOS in a web client in order to POST a file to the express backend as an upload. Since both the file's size and the client user's bandwidth is variable, it may take a certain amount of time for the POST request to finish. On the backend some logic applies and the request is promptly rejected.

问题在于客户端仅在请求完成后才接收响应,这可能是几秒钟.

The problem is that the client receives the response only after the request is finished, which can be several seconds.

我已经测试了这不是后端的问题,因为无论采用哪种技术,将其发布到Web中任意启用了post的url时的行为都是相同的.这是这种情况的(过度)简化示例.

I have already tested that it is not the backend's fault, as the behavior is the same when POSTing to any arbitrary post-enabled url in the web, regardless of the technology. Here is an (over)simplified example of the case.

这是后期动作.注意对任意启用后的URL的推荐请求.它的行为完全相同:

Here's the post action. Notice the commended request to the arbitrary post-enabled url. It behaves exactly the same:

try{
    console.log("posting....")
    const res = await axios.post("http://localhost:4000/upload", formData)
    // const res = await axios.post("https://github.com/logout", formData)
    console.log("result:")
    console.log(res)
}catch(err){
    console.error(err)
}

演示演示后端路线:

app.post("/upload", (req, res) => {
    console.log("Rejecting...")
    res.status(403).send()
    console.log("Rejected.")
    return
})

出于测试目的,我选择了3.7Mb文件,并将浏览器的带宽降低到Fast 3G预设.

For testing purposes I choose a 3.7Mb file, and throttle down my browsers bandwidth to the Fast 3G preset.

后端立即输出:

拒绝... 被拒绝.

Rejecting... Rejected.

请求待处理约43秒,然后返回403错误:

Whereas the request is pending for about 43 seconds before returning the 403 error:

我在这里缺少明显的东西吗?如此常见的功能使我怀疑这是正确的处理方式.如果确实如此,那么我们是否有关于Express线程在此期间是否处于活动状态的信息,还是仅仅是给客户带来了不便?

Am I missing something obvious here? It is such a common functionality, it makes me doubt that this is the correct way to be handled. And if it really is, do we have any information on whether express's thread is active during that time, or is it just a client inconvenience?

提前谢谢!

推荐答案

似乎首先发送响应标头,然后然后手动销毁请求即可达到目的:

It seems that first sending the response headers and then manually destroying the request does the trick:

app.post("/upload", (req, res) => {
    console.log("Rejecting...")
    res.status(403).send("Some message")
    return req.destroy()
})

AXIOS请求将保持待处理状态,直到仅上传当前块,然后立即导致正确的状态和消息.在速度较快的3g例子中,待处理时间从43s减少到900ms.

The AXIOS request stays pending until just the current chunk is uploaded, and then immediately results in the correct status and message. In the throttled down fast 3g example, pending time went down from 43s to 900ms.

此解决方案是通过反复试验而出现的,因此它可能不是最佳实践.

Also, this solution emerged through trial and error, so it can possibly not be the best practice.

如果有的话,我仍然会对面向AXIOS的解决方案感兴趣.

I would still be interested in an AXIOS oriented solution, if one exists.

这篇关于使用AXIOS发送大块POST请求时是否可以检测到即时请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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