Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 超时POST请求的浏览器重试行为不一致大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
由于超时,当服务器没有响应时,我偶尔会尝试重新启动POST请求.所有现代浏览器都具有针对幂等请求(GET,HEAD等)的重试逻辑,但是我无法解释POST请求发生的原因.

我正在使用@L_874_0@简单的node.js服务器测试这种情况,具有3个路由和chrome浏览器.

/       : gives a html page with jquery and code snippets to fire ajax requests
/hi     : gives a text response 'Hello'
/sleep  : request will timeout without any response

认情况下,node.js http服务器在2分钟后超时请求.

retry.js

var http = require('http');
var server = http.createServer();

server.on('request',function(req,res) {
    console.log(new Date() + ' ' + req.method + ' request on ' + req.url);

    if (req.url === '/sleep') {
        console.log('!!! sleeping');
    } else if (req.url === '/') {
        html = "$.post('/hi',{'for':'server'},function() { console.log(arguments) } ).error(function() { console.log(arguments) })";
        html += "<br><br>";
        html += "$.post('/sleep',{'for':'infinite'},function() { console.log(arguments) } ).error(function() { console.log(arguments) })";
        html += '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>';

        res.writeHead(200,{'Content-Type': 'text/html'});
        res.end(html);
    } else {
        res.writeHead(200,{'Content-Type': 'text/plain'});
        res.end('Hello');
    }
});

server.listen(2020);
console.log('server listening on port 2020');

运行

$node retry.js 
server listening on port 2020

1

Load this page in browser http://localhost:2020

Fri Mar 01 2013 12:21:59 GMT+0530 (IST) GET request on /
Fri Mar 01 2013 12:21:59 GMT+0530 (IST) GET request on /favicon.ico

2

从开发控制台,使用jquery将ajax POST请求发送到/ hi

$.post('/hi',function() { console.log(arguments) } ).error(function() { console.log(arguments) })

3

对POST进行启动/睡眠,导致2分钟后重试,4分钟后出错.

$.post('/sleep',function() { console.log(arguments) } ).error(function() { console.log(arguments) })

服务器日志显示2个请求

Fri Mar 01 2013 12:22:21 GMT+0530 (IST) POST request on /sleep
!!! sleeping
Fri Mar 01 2013 12:24:21 GMT+0530 (IST) POST request on /sleep
!!! sleeping

再次发射,错误在2分钟内没有任何重试.

Fri Mar 01 2013 12:30:01 GMT+0530 (IST) POST request on /sleep
!!! sleeping ?

在我们向/ hi(或任何其他url)发出请求之前,不会重试,导致响应.并且只有@L_874_0@后续的请求/ sleep进行重试.

在浏览器中,网络选项卡显示类似的模式

/hi - success 
/sleep - cancelled - 4 mins (retry happens)
/sleep - cancelled - 2 mins (no retry)
/sleep - cancelled - 2 mins (no retry)
/hi - success 
/sleep - cancelled - 4 mins (retry happens)
/sleep - cancelled - 2 mins (no retry)
/sleep - cancelled - 2 mins (no retry)

然我们需要设计我们的网络应用程序以容忍这些额外的请求(通过浏览器或任何其他中介),但这种不一致的浏览器重试看起来很奇怪.我在铬(v16& v24)& Firefox浏览器.

有人能帮我理解浏览器重试逻辑超出非幂等请求吗?

其他相关的stackoverflow问题

What happens when no response is received for a request? I’m seeing retries

解决方法

连接关闭之前,浏览器会重试请求(包括POST),然后再从服务器收到响应.这在 HTTP 1.1 Spec Section 8.2.4中定义.

大佬总结

以上是大佬教程为你收集整理的node.js – 超时POST请求的浏览器重试行为不一致全部内容,希望文章能够帮你解决node.js – 超时POST请求的浏览器重试行为不一致所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。