Node.js v18.12.1 documentation


Table of contents

HTTP#

Stability: 2 - Stable

Source Code: lib/http.js

To use the HTTP server and client one must require('node:http').要使用HTTP服务器和客户端,必须需要require('node:http')

The HTTP interfaces in Node.js are designed to support many features of the protocol which have been traditionally difficult to use. Node.js中的HTTP接口旨在支持协议的许多传统上难以使用的特性。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 message headers are represented by an object like this:HTTP消息头由如下对象表示:

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

Keys are lowercased. 键是小写的。Values are not modified.不会修改值。

In order to support the full spectrum of possible HTTP applications, the Node.js HTTP API is very low-level. 为了支持所有可能的HTTP应用程序,Node.js HTTP API非常低级。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.它将消息解析为头和正文,但不解析实际的头或正文。

See message.headers for details on how duplicate headers are handled.有关如何处理重复标头的详细信息,请参阅message.headers

The raw headers as they were received are retained in the rawHeaders property, which is an array of [key, value, key2, value2, ...]. 收到的原始标头保留在rawHeaders属性中,该属性是一个由[key, value, key2, value2, ...]组成的数组。For example, the previous message header object might have a rawHeaders list like the following:例如,前面的消息头对象可能有一个rawHeaders列表,如下所示:

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