标头对象
¥Headers object
标头在 JavaScript 对象上表示为自有的属性。属性键将被序列化为小写。属性值应该是字符串(如果不是,它们将被强制转换为字符串)或 Array
个字符串(以便为每个标头字段发送一个以上的值)。
¥Headers are represented as own-properties on JavaScript objects. The property
keys will be serialized to lower-case. Property values should be strings (if
they are not they will be coerced to strings) or an Array
of strings (in order
to send more than one value per header field).
const headers = {
':status': '200',
'content-type': 'text-plain',
'ABC': ['has', 'more', 'than', 'one', 'value'],
};
stream.respond(headers);
传给回调函数的标头对象将有一个 null
原型。这意味着 Object.prototype.toString()
和 Object.prototype.hasOwnProperty()
等普通 JavaScript 对象方法将不起作用。
¥Header objects passed to callback functions will have a null
prototype. This
means that normal JavaScript object methods such as
Object.prototype.toString()
and Object.prototype.hasOwnProperty()
will
not work.
对于传入的标头:
¥For incoming headers:
-
:status
标头转换为number
。¥The
:status
header is converted tonumber
. -
:status
、:method
、:authority
、:scheme
、:path
、:protocol
、age
、authorization
、access-control-allow-credentials
、access-control-max-age
、access-control-request-method
、content-encoding
、content-language
、content-length
、content-location
、content-md5
、content-range
、content-type
、date
、dnt
、etag
、expires
、from
、host
的副本,if-match
、if-modified-since
、if-none-match
、if-range
、if-unmodified-since
、last-modified
、location
、max-forwards
、proxy-authorization
、range
、referer
、retry-after
、tk
、upgrade-insecure-requests
、user-agent
或x-content-type-options
被丢弃。¥Duplicates of
:status
,:method
,:authority
,:scheme
,:path
,:protocol
,age
,authorization
,access-control-allow-credentials
,access-control-max-age
,access-control-request-method
,content-encoding
,content-language
,content-length
,content-location
,content-md5
,content-range
,content-type
,date
,dnt
,etag
,expires
,from
,host
,if-match
,if-modified-since
,if-none-match
,if-range
,if-unmodified-since
,last-modified
,location
,max-forwards
,proxy-authorization
,range
,referer
,retry-after
,tk
,upgrade-insecure-requests
,user-agent
orx-content-type-options
are discarded. -
set-cookie
始终是数组。重复项被添加到数组中。¥
set-cookie
is always an array. Duplicates are added to the array. -
对于重复的
cookie
标头,这些值用 '; 连接在一起'.¥For duplicate
cookie
headers, the values are joined together with '; '. -
对于所有其他标头,值使用 ',' 连接。
¥For all other headers, the values are joined together with ', '.
import { createServer } from 'node:http2';
const server = createServer();
server.on('stream', (stream, headers) => {
console.log(headers[':path']);
console.log(headers.ABC);
});
const http2 = require('node:http2');
const server = http2.createServer();
server.on('stream', (stream, headers) => {
console.log(headers[':path']);
console.log(headers.ABC);
});