http2.createSecureServer(options[, onRequestHandler])


  • options <Object>
    • allowHTTP1 <boolean> 当设置为 true 时,不支持 HTTP/2 的传入客户端连接将降级为 HTTP/1.x。 参见 'unknownProtocol' 事件。 参见 ALPN 协商默认值: false

    • maxDeflateDynamicTableSize <number> 设置用于压缩标头字段的最大动态表大小。 默认值: 4Kib

    • maxSettings <number> 设置每 SETTINGS 帧的最大设置条目数。 允许的最小值为 1默认值: 32

    • maxSessionMemory<number> 设置 Http2Session 允许使用的最大内存。 该值以兆字节数表示,例如 1 等于 1 兆字节。 允许的最小值为 1。 这是一个基于信用的限制,现有的 Http2Stream 可能会导致超出此限制,但超过此限制时,新的 Http2Stream 实例将被拒绝。 当前 Http2Stream 会话数、标头压缩表的当前内存使用、当前排队等待发送的数据以及未确认的 PINGSETTINGS 帧都计入当前限制。 默认值: 10

    • maxHeaderListPairs <number> 设置标头条目的最大数量。 这类似于 http.Server#maxHeadersCounthttp.ClientRequest#maxHeadersCount。 最小值为 4默认值: 128

    • maxOutstandingPings <number> 设置未确认的未确认 ping 的最大数量。 默认值: 10

    • maxSendHeaderBlockLength <number> 设置序列化的、压缩的标头块的最大允许大小。 尝试发送超出此限制的标头将导致触发 'frameError' 事件并且流被关闭和销毁。

    • paddingStrategy <number> 用于确定用于 HEADERSDATA 帧的填充量的策略。 默认值: http2.constants.PADDING_STRATEGY_NONE。 值可能是以下之一:

      • http2.constants.PADDING_STRATEGY_NONE:

      • http2.constants.PADDING_STRATEGY_MAX:

      • http2.constants.PADDING_STRATEGY_CALLBACK:

      • http2.constants.PADDING_STRATEGY_ALIGNED:

    • peerMaxConcurrentStreams <number> 设置远程对等方的最大并发流数,就好像已收到 SETTINGS 帧一样。 如果远程对等方为 maxConcurrentStreams 设置了自己的值,则将被覆盖。 默认值: 100

    • maxSessionInvalidFrames <integer> 设置会话关闭前允许的最大无效帧数。 默认值: 1000

    • maxSessionRejectedStreams <integer> 设置会话关闭前允许的创建流拒绝的最大数量。 每个拒绝都与 NGHTTP2_ENHANCE_YOUR_CALM 错误相关联,该错误应该告诉对等方不要再打开任何流,因此继续打开流被视为行为不端的对等方的标志。 默认值: 100

    • selectPadding <Function>

    • settings <HTTP/2 Settings Object> 连接时发送到远程对等方的初始设置。

    • ...: 可以提供任何 tls.createServer() 选项。 对于服务器,通常需要身份选项(pfxkey/cert)。

    • origins <string[]> 在创建新服务器 Http2Session 后立即在 ORIGIN 帧内发送的原始字符串数组。

    • unknownProtocolTimeout <number> 指定在触发 'unknownProtocol' 事件时服务器应等待的超时(以毫秒为单位)。 如果到那时套接字还没有被销毁,则服务器将销毁它。 默认值: 10000

  • onRequestHandler <Function> 参见兼容性 API
  • 返回: <Http2SecureServer>

返回创建和管理 Http2Session 实例的 tls.Server 实例。

const http2 = require('http2');
const fs = require('fs');

const options = {
  key: fs.readFileSync('server-key.pem'),
  cert: fs.readFileSync('server-cert.pem')
};

// 创建安全的 HTTP/2 服务器
const server = http2.createSecureServer(options);

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>
    • allowHTTP1 <boolean> Incoming client connections that do not support HTTP/2 will be downgraded to HTTP/1.x when set to true. See the 'unknownProtocol' event. See ALPN negotiation. Default: false.
    • maxDeflateDynamicTableSize <number> Sets the maximum dynamic table size for deflating header fields. Default: 4Kib.
    • maxSettings <number> Sets the maximum number of settings entries per SETTINGS frame. The minimum value allowed is 1. Default: 32.
    • maxSessionMemory<number> Sets the maximum memory that the Http2Session is permitted to use. The value is expressed in terms of number of megabytes, e.g. 1 equal 1 megabyte. The minimum value allowed is 1. This is a credit based limit, existing Http2Streams may cause this limit to be exceeded, but new Http2Stream instances will be rejected while this limit is exceeded. The current number of Http2Stream sessions, the current memory use of the header compression tables, current data queued to be sent, and unacknowledged PING and SETTINGS frames are all counted towards the current limit. Default: 10.
    • maxHeaderListPairs <number> Sets the maximum number of header entries. This is similar to http.Server#maxHeadersCount or http.ClientRequest#maxHeadersCount. The minimum value is 4. 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.
    • paddingStrategy <number> Strategy used for determining the amount of padding to use for HEADERS and DATA frames. Default: http2.constants.PADDING_STRATEGY_NONE. Value may be one of:
      • http2.constants.PADDING_STRATEGY_NONE: Specifies that no padding is to be applied.
      • http2.constants.PADDING_STRATEGY_MAX: Specifies that the maximum amount of padding, as determined by the internal implementation, is to be applied.
      • http2.constants.PADDING_STRATEGY_CALLBACK: Specifies that the user provided options.selectPadding() callback is to be used to determine the amount of padding.
      • http2.constants.PADDING_STRATEGY_ALIGNED: Will attempt to apply enough padding to ensure that the total frame length, including the 9-byte header, is a multiple of 8. For each frame, however, 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 will be used and the total frame length will not necessarily be aligned at 8 bytes.
    • peerMaxConcurrentStreams <number> Sets the maximum number of concurrent streams for the remote peer as if a SETTINGS frame had been received. Will be overridden if the remote peer sets its own value for maxConcurrentStreams. 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 an NGHTTP2_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.
    • selectPadding <Function> When options.paddingStrategy is equal to http2.constants.PADDING_STRATEGY_CALLBACK, provides the callback function used to determine the padding. See Using options.selectPadding().
    • settings <HTTP/2 Settings Object> The initial settings to send to the remote peer upon connection.
    • ...: Any tls.createServer() options can be provided. For servers, the identity options (pfx or key/cert) are usually required.
    • origins <string[]> An array of origin strings to send within an ORIGIN frame immediately following creation of a new server Http2Session.
    • unknownProtocolTimeout <number> Specifies a timeout in milliseconds that a server should wait when an 'unknownProtocol' event is emitted. If the socket has not been destroyed by that time the server will destroy it. Default: 10000.
  • onRequestHandler <Function> See Compatibility API
  • Returns: <Http2SecureServer>

Returns a tls.Server instance that creates and manages Http2Session instances.

const http2 = require('http2');
const fs = require('fs');

const options = {
  key: fs.readFileSync('server-key.pem'),
  cert: fs.readFileSync('server-cert.pem')
};

// Create a secure HTTP/2 server
const server = http2.createSecureServer(options);

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);