Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript http模块上的node.js response.writeHead_Javascript_Node.js_Http - Fatal编程技术网

Javascript http模块上的node.js response.writeHead

Javascript http模块上的node.js response.writeHead,javascript,node.js,http,Javascript,Node.js,Http,我正在实现自己的http模块。 在阅读官方node.js http模块api时,我无法理解以下几点: 如果用户正在使用response.writeHead(statusCode、[reasonPhrase]、[headers])函数,那么这些头应该立即写入套接字,还是应该首先作为对象的成员保存?然后只在.end()函数之后写入 当用户不使用writeHead()时,应该使用的隐式头的含义是什么?他们应该提前吗?如果用户没有设置它们呢?行为应该是什么?谢谢 我找到了第一个问题的答案,但仍然不理解第

我正在实现自己的http模块。 在阅读官方node.js http模块api时,我无法理解以下几点:

  • 如果用户正在使用
    response.writeHead(statusCode、[reasonPhrase]、[headers])
    函数,那么这些头应该立即写入套接字,还是应该首先作为对象的成员保存?然后只在
    .end()
    函数之后写入
  • 当用户不使用
    writeHead()
    时,应该使用的隐式头的含义是什么?他们应该提前吗?如果用户没有设置它们呢?行为应该是什么?谢谢

  • 我找到了第一个问题的答案,但仍然不理解第二个问题

    The first time response.write() is called, it will send the buffered header information and the first body to the client. The second time response.write() is called, Node assumes you're going to be streaming data, and sends that separately. That is, the response is buffered up to the first chunk of body.
    
    答复:

  • 您写入响应的任何内容,无论是带有
    writeHead
    的标头还是带有
    write
    的正文,都会被缓冲并发送。您可以看到,它们使用套接字缓冲区。在发送之前,它们只能保存固定数量的数据。需要记住的重要事实是,在开始编写正文之前,只能设置标题。如果这样做,http服务器本身将为您设置一些头

  • 隐式头是您没有专门编写但仍被发送的头。通过响应请求而不设置任何头来设置一个简单的http服务器。然后在浏览器中打开站点,查看请求标题。将有像日期,服务器,主机等头自动添加到每个请求,而用户的意愿


  • 所以实际上我找到了第一个问题的答案,所以我只留下了第二个问题