http2.createServer([options][, onRequestHandler])
options
<Object>maxDeflateDynamicTableSize
<number> 设置用于压缩标头字段的最大动态表大小。 默认值:4Kib
。maxSettings
<number> 设置每SETTINGS
帧的最大设置条目数。 允许的最小值为1
。 默认值:32
。maxSessionMemory
<number> 设置Http2Session
允许使用的最大内存。 该值以兆字节数表示,例如1
等于 1 兆字节。 允许的最小值为1
。 这是一个基于信用的限制,现有的Http2Stream
可能会导致超出此限制,但超过此限制时,新的Http2Stream
实例将被拒绝。 当前Http2Stream
会话数、标头压缩表的当前内存使用、当前排队等待发送的数据以及未确认的PING
和SETTINGS
帧都计入当前限制。 默认值:10
。maxHeaderListPairs
<number> 设置标头条目的最大数量。 这类似于node:http
模块中的server.maxHeadersCount
或request.maxHeadersCount
。 最小值为4
。 默认值:128
。maxOutstandingPings
<number> 设置未确认的未确认 ping 的最大数量。 默认值:10
。maxSendHeaderBlockLength
<number> 设置序列化的、压缩的标头块的最大允许大小。 尝试发送超出此限制的标头将导致触发'frameError'
事件并且流被关闭和销毁。 虽然这将最大允许大小设置为整个标头块,但nghttp2
(内部 http2 库)对每个解压缩的键/值对都有65536
的限制。paddingStrategy
<number> 用于确定用于HEADERS
和DATA
帧的填充量的策略。 默认值: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> 连接时发送到远程对等方的初始设置。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> 参见兼容性 API- 返回: <Http2Server>
返回创建和管理 Http2Session
实例的 net.Server
实例。
由于没有已知的浏览器支持未加密的 HTTP/2,所以在与浏览器客户端通信时必须使用 http2.createSecureServer()
。
const http2 = require('node:http2');
// 创建未加密的 HTTP/2 服务器。
// 由于没有已知的浏览器支持
// 未加密的 HTTP/2,
// 所以在与浏览器客户端通信时必须使用 `http2.createSecureServer()`。
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(80);
options
<Object>maxDeflateDynamicTableSize
<number> Sets the maximum dynamic table size for deflating header fields. Default:4Kib
.maxSettings
<number> Sets the maximum number of settings entries perSETTINGS
frame. The minimum value allowed is1
. Default:32
.maxSessionMemory
<number> Sets the maximum memory that theHttp2Session
is permitted to use. The value is expressed in terms of number of megabytes, e.g.1
equal 1 megabyte. The minimum value allowed is1
. This is a credit based limit, existingHttp2Stream
s may cause this limit to be exceeded, but newHttp2Stream
instances will be rejected while this limit is exceeded. The current number ofHttp2Stream
sessions, the current memory use of the header compression tables, current data queued to be sent, and unacknowledgedPING
andSETTINGS
frames are all counted towards the current limit. Default:10
.maxHeaderListPairs
<number> Sets the maximum number of header entries. This is similar toserver.maxHeadersCount
orrequest.maxHeadersCount
in thenode:http
module. The minimum value is4
. Default:128
.maxOutstandingPings
<number> Sets the maximum number of outstanding, unacknowledged pings. Default:10
.maxSendHeaderBlockLength
<number> Sets the maximum allowed size for a serialized, compressed block of headers. Attempts to send headers that exceed this limit will result in a'frameError'
event being emitted and the stream being closed and destroyed. While this sets the maximum allowed size to the entire block of headers,nghttp2
(the internal http2 library) has a limit of65536
for each decompressed key/value pair.paddingStrategy
<number> The strategy used for determining the amount of padding to use forHEADERS
andDATA
frames. Default:http2.constants.PADDING_STRATEGY_NONE
. Value may be one of:http2.constants.PADDING_STRATEGY_NONE
: No padding is applied.http2.constants.PADDING_STRATEGY_MAX
: The maximum amount of padding, determined by the internal implementation, is applied.http2.constants.PADDING_STRATEGY_ALIGNED
: Attempts to apply enough padding to ensure that the total frame length, including the 9-byte header, is a multiple of 8. For each frame, there is a maximum allowed number of padding bytes that is determined by current flow control state and settings. If this maximum is less than the calculated amount needed to ensure alignment, the maximum is used and the total frame length is not necessarily aligned at 8 bytes.
peerMaxConcurrentStreams
<number> Sets the maximum number of concurrent streams for the remote peer as if aSETTINGS
frame had been received. Will be overridden if the remote peer sets its own value formaxConcurrentStreams
. Default:100
.maxSessionInvalidFrames
<integer> Sets the maximum number of invalid frames that will be tolerated before the session is closed. Default:1000
.maxSessionRejectedStreams
<integer> Sets the maximum number of rejected upon creation streams that will be tolerated before the session is closed. Each rejection is associated with anNGHTTP2_ENHANCE_YOUR_CALM
error that should tell the peer to not open any more streams, continuing to open streams is therefore regarded as a sign of a misbehaving peer. Default:100
.settings
<HTTP/2 Settings Object> The initial settings to send to the remote peer upon connection.Http1IncomingMessage
<http.IncomingMessage> Specifies theIncomingMessage
class to used for HTTP/1 fallback. Useful for extending the originalhttp.IncomingMessage
. Default:http.IncomingMessage
.Http1ServerResponse
<http.ServerResponse> Specifies theServerResponse
class to used for HTTP/1 fallback. Useful for extending the originalhttp.ServerResponse
. Default:http.ServerResponse
.Http2ServerRequest
<http2.Http2ServerRequest> Specifies theHttp2ServerRequest
class to use. Useful for extending the originalHttp2ServerRequest
. Default:Http2ServerRequest
.Http2ServerResponse
<http2.Http2ServerResponse> Specifies theHttp2ServerResponse
class to use. Useful for extending the originalHttp2ServerResponse
. Default:Http2ServerResponse
.unknownProtocolTimeout
<number> Specifies a timeout in milliseconds that a server should wait when an'unknownProtocol'
is emitted. If the socket has not been destroyed by that time the server will destroy it. Default:10000
.- ...: Any
net.createServer()
option can be provided.
onRequestHandler
<Function> See Compatibility API- Returns: <Http2Server>
Returns a net.Server
instance that creates and manages Http2Session
instances.
Since there are no browsers known that support
unencrypted HTTP/2, the use of
http2.createSecureServer()
is necessary when communicating
with browser clients.
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(80);