超文本传输协议


🌐 HTTP

源代码: lib/http.js

要使用 HTTP 服务器和客户端,必须 require('node:http')

🌐 To use the HTTP server and client one must require('node:http').

Node.js 中的 HTTP 接口旨在支持该协议的许多传统上难以使用的特性。特别是大规模的、可能分块编码的消息。该接口小心设计,从不缓冲整个请求或响应,因此用户能够流式传输数据。

🌐 The HTTP interfaces in Node.js are designed to support many features of the protocol which have been traditionally difficult to use. In particular, large, possibly chunk-encoded, messages. The interface is careful to never buffer entire requests or responses, so the user is able to stream data.

HTTP 消息头由类似如下的对象表示:

🌐 HTTP message headers are represented by an object like this:

{ 'content-length': '123',
  'content-type': 'text/plain',
  'connection': 'keep-alive',
  'host': 'example.com',
  'accept': '*/*' } 

键会被转换为小写。值不会被修改。

🌐 Keys are lowercased. Values are not modified.

为了支持所有可能的 HTTP 应用,Node.js 的 HTTP API 属于非常底层的接口。它仅处理流操作和消息解析。它会将消息解析为头部和主体,但不会解析实际的头部内容或主体内容。

🌐 In order to support the full spectrum of possible HTTP applications, the Node.js HTTP API is very low-level. It deals with stream handling and message parsing only. It parses a message into headers and body but it does not parse the actual headers or the body.

有关重复标题的处理方式,请参见 message.headers

🌐 See message.headers for details on how duplicate headers are handled.

rawHeaders 属性中保留了接收到的原始头信息,它是一个 [key, value, key2, value2, ...] 的数组。例如,之前的消息头对象可能有如下的 rawHeaders 列表:

🌐 The raw headers as they were received are retained in the rawHeaders property, which is an array of [key, value, key2, value2, ...]. For example, the previous message header object might have a rawHeaders list like the following:

[ 'ConTent-Length', '123456',
  'content-LENGTH', '123',
  'content-type', 'text/plain',
  'CONNECTION', 'keep-alive',
  'Host', 'example.com',
  'accepT', '*/*' ]