HTTP
¥Stability: 2 - Stable
源代码: lib/http.js
此模块包含客户端和服务器,可以通过 require('node:http')
(CommonJS)或 import * as http from 'node:http'
(ES 模块)导入。
¥This module, containing both a client and server, can be imported via
require('node:http')
(CommonJS) or import * as http from 'node:http'
(ES module).
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', '*/*' ]