【问题标题】:Why does Chrome send so many HTTP requests?为什么 Chrome 会发送这么多 HTTP 请求?
【发布时间】:2019-07-13 16:43:44
【问题描述】:

我正在运行仅使用 HTTP 模块的准系统 Nodejs 服务器。我已经创建了一个 HTTP 服务器,并且正在监听套接字连接和请求。我注意到,当我使用 chrome 并转到 localhost 时,连接了三个套接字,并且向“/”发出了两个请求。我知道,使用其他一些网络服务器,如果 Chrome 没有收到快速响应(大约 5 秒),我已经看到多次请求相同的内容,但我立即发送响应并且 Chrome 仍在连接/请求多个次。

这是预期的吗?如果是,我应该处理重复的请求吗?

我的相关代码

    let server = http.createServer();
    server.listen({
        host: host,
        port: port
    });
    server.on('connection', function(socket){
         // gets printed 3 times
         console.log('connection')
    });
    server.on('request', function(request, response){
        // gets printed two times
        console.log('hi')
        // yet chrome only receives one response (seemingly)
        response.end('hi')
    });

编辑:解决了一半。现在我正在打印request.url,我看到了 /favicon.ico

所以有 2 个请求,但仍然有 3 个套接字连接。我猜每个请求都在一个新的套接字上?

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    所有单独的图像、css 和 javascript 肯定会发出 http 请求。毫无疑问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 2017-09-10
      相关资源
      最近更新 更多