头部名称常量


🌐 Header name constants

HTTP2_HEADER_* 常量提供了 HTTP/2 伪头和已知 HTTP 头名称的名称。使用这些字符串常量是可选的。例如,http2.constants.HTTP2_HEADER_CONTENT_TYPE 等于 'content-type'。对于接受常规头名称的 API,http2.constants.HTTP2_HEADER_CONTENT_TYPE、'content-type' 和 'Content-Type' 有相同的效果;Node.js 会将名称序列化为小写。

🌐 The HTTP2_HEADER_* constants provide names for HTTP/2 pseudo-headers and known HTTP header names. Using these string constants is optional. For example, http2.constants.HTTP2_HEADER_CONTENT_TYPE is equal to 'content-type'. For APIs that accept regular header names, http2.constants.HTTP2_HEADER_CONTENT_TYPE, 'content-type', and 'Content-Type' have the same effect; Node.js serializes the name in lower-case.

常规的头部常量可以在兼容性 API 中使用,只要相应的字面头部名称被接受。在兼容性 API 的请求处理器中,建议使用 request.method、request.authority、request.scheme 和 request.url 来对应相应的伪头。其他传入的伪头仍可以通过 request.headers 获取。可以通过 response.statusCode 或传递给 response.writeHead() 的 statusCode 参数来设置响应状态。将 HTTP2_HEADER_STATUS(':status')传给 response.setHeader() 或放在 response.writeHead() 的 headers 对象中会抛出 ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED。HTTP2_HEADER_PROTOCOL 是请求伪头,不能在响应中发送。

🌐 Regular header constants can be used with the compatibility API wherever the corresponding literal header name is accepted. In compatibility API request handlers, prefer request.method, request.authority, request.scheme, and request.url for the corresponding pseudo-headers. Other incoming pseudo-headers remain available through request.headers. Set response status through response.statusCode or the statusCode argument to response.writeHead(). Passing HTTP2_HEADER_STATUS (':status') to response.setHeader() or in response.writeHead()'s headers object throws ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED. HTTP2_HEADER_PROTOCOL is a request pseudo-header and cannot be sent in a response.

传入的头部对象键都是小写的,所以在作为对象属性访问它们时,使用常量或小写字面量。使用常量不会改变头部验证,而且常量的存在并不意味着在每个 HTTP/2 环境下头部都是有效的。具体关于头部大小写和验证的内容,请参见 HTTP/2 头部对象 和 标头名称和值中的无效字符处理。

🌐 Incoming header object keys are lower-case, so use a constant or a lower-case literal when accessing them as object properties. Using a constant does not change header validation, and the availability of a constant does not imply that the header is valid in every HTTP/2 context. See HTTP/2 Headers Object and Invalid character handling in header names and values for details about header casing and validation.