Node.js http2.createServer

🌙
手机阅读
本文目录结构

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

版本历史

版本 变更
v9.6.0 Added the Http1IncomingMessage and Http1ServerResponse option.
v8.9.3 Added the maxOutstandingPings option with a default limit of 10.
v8.9.3 Added the maxHeaderListPairs option with a default limit of 128 header pairs.
v8.4.0 新增于: v8.4.0
  • options < Object>

    • maxDeflateDynamicTableSize < number> Sets the maximum dynamic table size for deflating header fields. Default: 4Kib.
    • 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. 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> Identifies the 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.
    • 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.
    • Http1IncomingMessage < http.IncomingMessage> Specifies the IncomingMessage class to used for HTTP/1 fallback. Useful for extending the original http.IncomingMessage. Default: http.IncomingMessage.
    • Http1ServerResponse < http.ServerResponse> Specifies the ServerResponse class to used for HTTP/1 fallback. Useful for extending the original http.ServerResponse. Default: http.ServerResponse.
    • Http2ServerRequest < http2.Http2ServerRequest> Specifies the Http2ServerRequest class to use. Useful for extending the original Http2ServerRequest. Default: Http2ServerRequest.
    • Http2ServerResponse < http2.Http2ServerResponse> Specifies the Http2ServerResponse class to use. Useful for extending the original Http2ServerResponse. Default: Http2ServerResponse.
  • onRequestHandler < Function> See Compatibility API

  • Returns:

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('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',
    ':status': 200
  });
  stream.end('<h1>Hello World</h1>');
});

server.listen(80);

更多内容请参考:Node.js HTTP2,或者通过 点击对应菜单 进行查看;



AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

目前重心已经放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>