http2.createServer([options][, onRequestHandler])


  • options <Object>
    • maxDeflateDynamicTableSize <number> 设置用于压缩头字段的最大动态表大小。默认值: 4Kib
    • maxSettings <number> 设置每个 SETTINGS 帧的最大设置条目数。允许的最小值是 1默认值: 32
    • maxSessionMemory<number> 设置 Http2Session 允许使用的最大内存。该值以兆字节为单位,例如 1 表示 1 兆字节。允许的最小值为 1。这是基于额度的限制,已有的 Http2Stream 可能会导致超过该限制,但在超出限制时,新的 Http2Stream 实例将被拒绝。当前 Http2Stream 会话的数量、头部压缩表的当前内存使用量、待发送的数据以及未确认的 PINGSETTINGS 帧都会计入当前限制。默认值: 10
    • maxHeaderListPairs <number> 设置最大页眉条目数。这类似于 node:http 模块中的 server.maxHeadersCountrequest.maxHeadersCount。最小值为 4默认值: 128
    • maxOutstandingPings <number> 设置未确认 ping 的最大数量。默认值: 10
    • maxSendHeaderBlockLength <number> 设置序列化、压缩后的头块允许的最大尺寸。尝试发送超过该限制的头部将导致触发 'frameError' 事件,并且流将被关闭和销毁。虽然此设置将最大允许尺寸应用于整个头块,但 nghttp2(内部 http2 库)对每个解压后的键/值对有 65536 的限制。
    • paddingStrategy <number> 用于确定 HEADERSDATA 帧填充量的策略。默认值: http2.constants.PADDING_STRATEGY_NONE。可选值包括:
      • http2.constants.PADDING_STRATEGY_NONE:未应用填充。
      • http2.constants.PADDING_STRATEGY_MAX:应用的最大填充量由内部实现决定。
      • http2.constants.PADDING_STRATEGY_ALIGNED:尝试应用足够的填充,以确保包括9字节头部在内的总帧长度是8的倍数。对于每一帧,允许的最大填充字节数由当前流量控制状态和设置决定。如果这个最大值小于为确保对齐所需的计算值,则使用最大值,并且总帧长度不一定对齐到8字节。
    • peerMaxConcurrentStreams <number> 设置远程节点的最大并发流数,就好像已经收到 SETTINGS 帧一样。如果远程节点为 maxConcurrentStreams 设置了自己的值,将会被覆盖。默认值: 100
    • maxSessionInvalidFrames <integer> 设置在会话关闭之前允许的最大无效帧数。 默认值: 1000
    • maxSessionRejectedStreams <integer> 设置在会话关闭之前,允许被拒绝的创建流的最大数量。每次拒绝都会关联一个 NGHTTP2_ENHANCE_YOUR_CALM 错误,该错误应告知对端不要打开更多流,因此继续打开流被视为对端行为异常的标志。默认值: 100
    • settings <HTTP/2 Settings Object> 连接时发送给远程端的初始设置。
    • remoteCustomSettings <Array> 整数值数组决定了设置类型,这些设置类型包含在接收到的 remoteSettings 的 CustomSettings 属性中。有关允许的设置类型的更多信息,请参阅 Http2Settings 对象的 CustomSettings 属性。
    • Http1IncomingMessage <http.IncomingMessage> 指定用于 HTTP/1 回退的 IncomingMessage 类。适用于扩展原始的 http.IncomingMessage默认: http.IncomingMessage
    • Http1ServerResponse <http.ServerResponse> 指定用于 HTTP/1 回退的 ServerResponse 类。适用于扩展原始的 http.ServerResponse默认值: http.ServerResponse
    • Http2ServerRequest <http2.Http2ServerRequest> 指定要使用的 Http2ServerRequest 类。可用于扩展原始的 Http2ServerRequest默认值: Http2ServerRequest
    • Http2ServerResponse <http2.Http2ServerResponse> 指定要使用的 Http2ServerResponse 类。可用于扩展原始的 Http2ServerResponse默认值: Http2ServerResponse
    • unknownProtocolTimeout <number> 指定服务器在发出 'unknownProtocol' 时应等待的超时时间(以毫秒为单位)。如果在此时间内套接字尚未被销毁,服务器将销毁它。默认值:10000
    • ...:可以提供任何net.createServer()选项。
  • onRequestHandler <Function> 参见 兼容性接口
  • 返回:<Http2Server>

返回一个 net.Server 实例,该实例创建并管理 Http2Session 实例。

🌐 Returns a net.Server instance that creates and manages Http2Session instances.

由于目前没有已知的浏览器支持 未加密的 HTTP/2,因此在与浏览器客户端通信时必须使用 http2.createSecureServer()

🌐 Since there are no browsers known that support unencrypted HTTP/2, the use of http2.createSecureServer() is necessary when communicating with browser clients.

import { createServer } from 'node:http2';

// Create an unencrypted HTTP/2 server.
// Since there are no browsers known that support
// unencrypted HTTP/2, the use of `createSecureServer()`
// is necessary when communicating with browser clients.
const server = createServer();

server.on('stream', (stream, headers) => {
  stream.respond({
    'content-type': 'text/html; charset=utf-8',
    ':status': 200,
  });
  stream.end('<h1>Hello World</h1>');
});

server.listen(8000);const http2 = require('node:http2');

// Create an unencrypted HTTP/2 server.
// Since there are no browsers known that support
// unencrypted HTTP/2, the use of `http2.createSecureServer()`
// is necessary when communicating with browser clients.
const server = http2.createServer();

server.on('stream', (stream, headers) => {
  stream.respond({
    'content-type': 'text/html; charset=utf-8',
    ':status': 200,
  });
  stream.end('<h1>Hello World</h1>');
});

server.listen(8000);