http模块上的node.js response.writeHead

我正在实现我自己的http模块。 正如我正在阅读官方的node.js http模块API,我不明白一些事情:

  1. 如果用户正在使用response.writeHead(statusCode, [reasonPhrase], [headers])函数,那么头文件是否应该被写入套接字,或者是第一次被保存为对象的成员? 然后只写了.end()函数之后?
  2. 当用户不使用writeHead()时应该使用的隐式标题的含义是什么? 他们应该提前? 如果用户没有设置它们? 应该是什么行为? 谢谢

回答:

  1. 任何你用writeHead写入的头文件或write被caching并发送。 你看他们使用套接字缓冲区。 他们在发送之前只能保存一定数量的数据。 要记住的重要的事实是,你只能在开始写体之前设置标题。 如果你这样做,一些标题将由http服务器本身为你设置。

  2. 隐含的标题是你没有特别写但仍然被发送的标题。 设置一个简单的http服务器,通过响应请求而不设置任何头。 然后在浏览器中查看打开网站的请求标题。 会有像date,服务器,主机等标题自动添加到每个请求,而无需用户的意志。

我find了第一个问题的答案,但仍然不明白第二个问题。

 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.