1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v18.18.2"> 7 <title>HTTP/2 | Node.js v18.18.2 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/http2.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 14</head> 15<body class="alt apidoc" id="api-section-http2"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2 active">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="http2" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.18.2 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 104 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.18.2</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_2"><a href="#http2">HTTP/2</a></span> 124<ul> 125<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li> 126<li><a href="#core-api">Core API</a> 127<ul> 128<li><a href="#server-side-example">Server-side example</a></li> 129<li><a href="#client-side-example">Client-side example</a></li> 130<li><a href="#class-http2session">Class: <code>Http2Session</code></a> 131<ul> 132<li><a href="#http2session-and-sockets"><code>Http2Session</code> and sockets</a></li> 133<li><a href="#event-close">Event: <code>'close'</code></a></li> 134<li><a href="#event-connect">Event: <code>'connect'</code></a></li> 135<li><a href="#event-error">Event: <code>'error'</code></a></li> 136<li><a href="#event-frameerror">Event: <code>'frameError'</code></a></li> 137<li><a href="#event-goaway">Event: <code>'goaway'</code></a></li> 138<li><a href="#event-localsettings">Event: <code>'localSettings'</code></a></li> 139<li><a href="#event-ping">Event: <code>'ping'</code></a></li> 140<li><a href="#event-remotesettings">Event: <code>'remoteSettings'</code></a></li> 141<li><a href="#event-stream">Event: <code>'stream'</code></a></li> 142<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li> 143<li><a href="#http2sessionalpnprotocol"><code>http2session.alpnProtocol</code></a></li> 144<li><a href="#http2sessionclosecallback"><code>http2session.close([callback])</code></a></li> 145<li><a href="#http2sessionclosed"><code>http2session.closed</code></a></li> 146<li><a href="#http2sessionconnecting"><code>http2session.connecting</code></a></li> 147<li><a href="#http2sessiondestroyerror-code"><code>http2session.destroy([error][, code])</code></a></li> 148<li><a href="#http2sessiondestroyed"><code>http2session.destroyed</code></a></li> 149<li><a href="#http2sessionencrypted"><code>http2session.encrypted</code></a></li> 150<li><a href="#http2sessiongoawaycode-laststreamid-opaquedata"><code>http2session.goaway([code[, lastStreamID[, opaqueData]]])</code></a></li> 151<li><a href="#http2sessionlocalsettings"><code>http2session.localSettings</code></a></li> 152<li><a href="#http2sessionoriginset"><code>http2session.originSet</code></a></li> 153<li><a href="#http2sessionpendingsettingsack"><code>http2session.pendingSettingsAck</code></a></li> 154<li><a href="#http2sessionpingpayload-callback"><code>http2session.ping([payload, ]callback)</code></a></li> 155<li><a href="#http2sessionref"><code>http2session.ref()</code></a></li> 156<li><a href="#http2sessionremotesettings"><code>http2session.remoteSettings</code></a></li> 157<li><a href="#http2sessionsetlocalwindowsizewindowsize"><code>http2session.setLocalWindowSize(windowSize)</code></a></li> 158<li><a href="#http2sessionsettimeoutmsecs-callback"><code>http2session.setTimeout(msecs, callback)</code></a></li> 159<li><a href="#http2sessionsocket"><code>http2session.socket</code></a></li> 160<li><a href="#http2sessionstate"><code>http2session.state</code></a></li> 161<li><a href="#http2sessionsettingssettings-callback"><code>http2session.settings([settings][, callback])</code></a></li> 162<li><a href="#http2sessiontype"><code>http2session.type</code></a></li> 163<li><a href="#http2sessionunref"><code>http2session.unref()</code></a></li> 164</ul> 165</li> 166<li><a href="#class-serverhttp2session">Class: <code>ServerHttp2Session</code></a> 167<ul> 168<li><a href="#serverhttp2sessionaltsvcalt-originorstream"><code>serverhttp2session.altsvc(alt, originOrStream)</code></a></li> 169<li><a href="#specifying-alternative-services">Specifying alternative services</a></li> 170<li><a href="#serverhttp2sessionoriginorigins"><code>serverhttp2session.origin(...origins)</code></a></li> 171</ul> 172</li> 173<li><a href="#class-clienthttp2session">Class: <code>ClientHttp2Session</code></a> 174<ul> 175<li><a href="#event-altsvc">Event: <code>'altsvc'</code></a></li> 176<li><a href="#event-origin">Event: <code>'origin'</code></a></li> 177<li><a href="#clienthttp2sessionrequestheaders-options"><code>clienthttp2session.request(headers[, options])</code></a></li> 178</ul> 179</li> 180<li><a href="#class-http2stream">Class: <code>Http2Stream</code></a> 181<ul> 182<li><a href="#http2stream-lifecycle"><code>Http2Stream</code> Lifecycle</a> 183<ul> 184<li><a href="#creation">Creation</a></li> 185<li><a href="#destruction">Destruction</a></li> 186</ul> 187</li> 188<li><a href="#event-aborted">Event: <code>'aborted'</code></a></li> 189<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 190<li><a href="#event-error_1">Event: <code>'error'</code></a></li> 191<li><a href="#event-frameerror_1">Event: <code>'frameError'</code></a></li> 192<li><a href="#event-ready">Event: <code>'ready'</code></a></li> 193<li><a href="#event-timeout_1">Event: <code>'timeout'</code></a></li> 194<li><a href="#event-trailers">Event: <code>'trailers'</code></a></li> 195<li><a href="#event-wanttrailers">Event: <code>'wantTrailers'</code></a></li> 196<li><a href="#http2streamaborted"><code>http2stream.aborted</code></a></li> 197<li><a href="#http2streambuffersize"><code>http2stream.bufferSize</code></a></li> 198<li><a href="#http2streamclosecode-callback"><code>http2stream.close(code[, callback])</code></a></li> 199<li><a href="#http2streamclosed"><code>http2stream.closed</code></a></li> 200<li><a href="#http2streamdestroyed"><code>http2stream.destroyed</code></a></li> 201<li><a href="#http2streamendafterheaders"><code>http2stream.endAfterHeaders</code></a></li> 202<li><a href="#http2streamid"><code>http2stream.id</code></a></li> 203<li><a href="#http2streampending"><code>http2stream.pending</code></a></li> 204<li><a href="#http2streampriorityoptions"><code>http2stream.priority(options)</code></a></li> 205<li><a href="#http2streamrstcode"><code>http2stream.rstCode</code></a></li> 206<li><a href="#http2streamsentheaders"><code>http2stream.sentHeaders</code></a></li> 207<li><a href="#http2streamsentinfoheaders"><code>http2stream.sentInfoHeaders</code></a></li> 208<li><a href="#http2streamsenttrailers"><code>http2stream.sentTrailers</code></a></li> 209<li><a href="#http2streamsession"><code>http2stream.session</code></a></li> 210<li><a href="#http2streamsettimeoutmsecs-callback"><code>http2stream.setTimeout(msecs, callback)</code></a></li> 211<li><a href="#http2streamstate"><code>http2stream.state</code></a></li> 212<li><a href="#http2streamsendtrailersheaders"><code>http2stream.sendTrailers(headers)</code></a></li> 213</ul> 214</li> 215<li><a href="#class-clienthttp2stream">Class: <code>ClientHttp2Stream</code></a> 216<ul> 217<li><a href="#event-continue">Event: <code>'continue'</code></a></li> 218<li><a href="#event-headers">Event: <code>'headers'</code></a></li> 219<li><a href="#event-push">Event: <code>'push'</code></a></li> 220<li><a href="#event-response">Event: <code>'response'</code></a></li> 221</ul> 222</li> 223<li><a href="#class-serverhttp2stream">Class: <code>ServerHttp2Stream</code></a> 224<ul> 225<li><a href="#http2streamadditionalheadersheaders"><code>http2stream.additionalHeaders(headers)</code></a></li> 226<li><a href="#http2streamheaderssent"><code>http2stream.headersSent</code></a></li> 227<li><a href="#http2streampushallowed"><code>http2stream.pushAllowed</code></a></li> 228<li><a href="#http2streampushstreamheaders-options-callback"><code>http2stream.pushStream(headers[, options], callback)</code></a></li> 229<li><a href="#http2streamrespondheaders-options"><code>http2stream.respond([headers[, options]])</code></a></li> 230<li><a href="#http2streamrespondwithfdfd-headers-options"><code>http2stream.respondWithFD(fd[, headers[, options]])</code></a></li> 231<li><a href="#http2streamrespondwithfilepath-headers-options"><code>http2stream.respondWithFile(path[, headers[, options]])</code></a></li> 232</ul> 233</li> 234<li><a href="#class-http2server">Class: <code>Http2Server</code></a> 235<ul> 236<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li> 237<li><a href="#event-connection">Event: <code>'connection'</code></a></li> 238<li><a href="#event-request">Event: <code>'request'</code></a></li> 239<li><a href="#event-session">Event: <code>'session'</code></a></li> 240<li><a href="#event-sessionerror">Event: <code>'sessionError'</code></a></li> 241<li><a href="#event-stream_1">Event: <code>'stream'</code></a></li> 242<li><a href="#event-timeout_2">Event: <code>'timeout'</code></a></li> 243<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li> 244<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li> 245<li><a href="#servertimeout"><code>server.timeout</code></a></li> 246<li><a href="#serverupdatesettingssettings"><code>server.updateSettings([settings])</code></a></li> 247</ul> 248</li> 249<li><a href="#class-http2secureserver">Class: <code>Http2SecureServer</code></a> 250<ul> 251<li><a href="#event-checkcontinue_1">Event: <code>'checkContinue'</code></a></li> 252<li><a href="#event-connection_1">Event: <code>'connection'</code></a></li> 253<li><a href="#event-request_1">Event: <code>'request'</code></a></li> 254<li><a href="#event-session_1">Event: <code>'session'</code></a></li> 255<li><a href="#event-sessionerror_1">Event: <code>'sessionError'</code></a></li> 256<li><a href="#event-stream_2">Event: <code>'stream'</code></a></li> 257<li><a href="#event-timeout_3">Event: <code>'timeout'</code></a></li> 258<li><a href="#event-unknownprotocol">Event: <code>'unknownProtocol'</code></a></li> 259<li><a href="#serverclosecallback_1"><code>server.close([callback])</code></a></li> 260<li><a href="#serversettimeoutmsecs-callback_1"><code>server.setTimeout([msecs][, callback])</code></a></li> 261<li><a href="#servertimeout_1"><code>server.timeout</code></a></li> 262<li><a href="#serverupdatesettingssettings_1"><code>server.updateSettings([settings])</code></a></li> 263</ul> 264</li> 265<li><a href="#http2createserveroptions-onrequesthandler"><code>http2.createServer([options][, onRequestHandler])</code></a></li> 266<li><a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer(options[, onRequestHandler])</code></a></li> 267<li><a href="#http2connectauthority-options-listener"><code>http2.connect(authority[, options][, listener])</code></a></li> 268<li><a href="#http2constants"><code>http2.constants</code></a> 269<ul> 270<li><a href="#error-codes-for-rst_stream-and-goaway">Error codes for <code>RST_STREAM</code> and <code>GOAWAY</code></a></li> 271</ul> 272</li> 273<li><a href="#http2getdefaultsettings"><code>http2.getDefaultSettings()</code></a></li> 274<li><a href="#http2getpackedsettingssettings"><code>http2.getPackedSettings([settings])</code></a></li> 275<li><a href="#http2getunpackedsettingsbuf"><code>http2.getUnpackedSettings(buf)</code></a></li> 276<li><a href="#http2sensitiveheaders"><code>http2.sensitiveHeaders</code></a></li> 277<li><a href="#headers-object">Headers object</a> 278<ul> 279<li><a href="#sensitive-headers">Sensitive headers</a></li> 280</ul> 281</li> 282<li><a href="#settings-object">Settings object</a></li> 283<li><a href="#error-handling">Error handling</a></li> 284<li><a href="#invalid-character-handling-in-header-names-and-values">Invalid character handling in header names and values</a></li> 285<li><a href="#push-streams-on-the-client">Push streams on the client</a></li> 286<li><a href="#supporting-the-connect-method">Supporting the <code>CONNECT</code> method</a></li> 287<li><a href="#the-extended-connect-protocol">The extended <code>CONNECT</code> protocol</a></li> 288</ul> 289</li> 290<li><a href="#compatibility-api">Compatibility API</a> 291<ul> 292<li><a href="#alpn-negotiation">ALPN negotiation</a></li> 293<li><a href="#class-http2http2serverrequest">Class: <code>http2.Http2ServerRequest</code></a> 294<ul> 295<li><a href="#event-aborted_1">Event: <code>'aborted'</code></a></li> 296<li><a href="#event-close_2">Event: <code>'close'</code></a></li> 297<li><a href="#requestaborted"><code>request.aborted</code></a></li> 298<li><a href="#requestauthority"><code>request.authority</code></a></li> 299<li><a href="#requestcomplete"><code>request.complete</code></a></li> 300<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li> 301<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a></li> 302<li><a href="#requestheaders"><code>request.headers</code></a></li> 303<li><a href="#requesthttpversion"><code>request.httpVersion</code></a></li> 304<li><a href="#requestmethod"><code>request.method</code></a></li> 305<li><a href="#requestrawheaders"><code>request.rawHeaders</code></a></li> 306<li><a href="#requestrawtrailers"><code>request.rawTrailers</code></a></li> 307<li><a href="#requestscheme"><code>request.scheme</code></a></li> 308<li><a href="#requestsettimeoutmsecs-callback"><code>request.setTimeout(msecs, callback)</code></a></li> 309<li><a href="#requestsocket"><code>request.socket</code></a></li> 310<li><a href="#requeststream"><code>request.stream</code></a></li> 311<li><a href="#requesttrailers"><code>request.trailers</code></a></li> 312<li><a href="#requesturl"><code>request.url</code></a></li> 313</ul> 314</li> 315<li><a href="#class-http2http2serverresponse">Class: <code>http2.Http2ServerResponse</code></a> 316<ul> 317<li><a href="#event-close_3">Event: <code>'close'</code></a></li> 318<li><a href="#event-finish">Event: <code>'finish'</code></a></li> 319<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li> 320<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li> 321<li><a href="#responsecreatepushresponseheaders-callback"><code>response.createPushResponse(headers, callback)</code></a></li> 322<li><a href="#responseenddata-encoding-callback"><code>response.end([data[, encoding]][, callback])</code></a></li> 323<li><span class="stability_0"><a href="#responsefinished"><code>response.finished</code></a></span></li> 324<li><a href="#responsegetheadername"><code>response.getHeader(name)</code></a></li> 325<li><a href="#responsegetheadernames"><code>response.getHeaderNames()</code></a></li> 326<li><a href="#responsegetheaders"><code>response.getHeaders()</code></a></li> 327<li><a href="#responsehasheadername"><code>response.hasHeader(name)</code></a></li> 328<li><a href="#responseheaderssent"><code>response.headersSent</code></a></li> 329<li><a href="#responseremoveheadername"><code>response.removeHeader(name)</code></a></li> 330<li><a href="#responsereq"><code>response.req</code></a></li> 331<li><a href="#responsesenddate"><code>response.sendDate</code></a></li> 332<li><a href="#responsesetheadername-value"><code>response.setHeader(name, value)</code></a></li> 333<li><a href="#responsesettimeoutmsecs-callback"><code>response.setTimeout(msecs[, callback])</code></a></li> 334<li><a href="#responsesocket"><code>response.socket</code></a></li> 335<li><a href="#responsestatuscode"><code>response.statusCode</code></a></li> 336<li><a href="#responsestatusmessage"><code>response.statusMessage</code></a></li> 337<li><a href="#responsestream"><code>response.stream</code></a></li> 338<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li> 339<li><a href="#responsewritechunk-encoding-callback"><code>response.write(chunk[, encoding][, callback])</code></a></li> 340<li><a href="#responsewritecontinue"><code>response.writeContinue()</code></a></li> 341<li><a href="#responsewriteearlyhintshints"><code>response.writeEarlyHints(hints)</code></a></li> 342<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li> 343</ul> 344</li> 345</ul> 346</li> 347<li><a href="#collecting-http2-performance-metrics">Collecting HTTP/2 performance metrics</a></li> 348<li><a href="#note-on-authority-and-host">Note on <code>:authority</code> and <code>host</code></a></li> 349</ul> 350</li> 351</ul></div></div> 352 </li> 353 354 355 <li class="picker-header"> 356 <a href="#"> 357 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 358 Index 359 </a> 360 361 <div class="picker"><ul> 362<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 363<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 364 365 <li> 366 <a href="index.html">Index</a> 367 </li> 368 </ul> 369 370<hr class="line"> 371<ul> 372<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 373<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 374<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 375<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 376<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 377<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 378<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 379<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 380<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 382<li><a href="console.html" class="nav-console">Console</a></li> 383<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 384<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 385<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 386<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 387<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 388<li><a href="dns.html" class="nav-dns">DNS</a></li> 389<li><a href="domain.html" class="nav-domain">Domain</a></li> 390<li><a href="errors.html" class="nav-errors">Errors</a></li> 391<li><a href="events.html" class="nav-events">Events</a></li> 392<li><a href="fs.html" class="nav-fs">File system</a></li> 393<li><a href="globals.html" class="nav-globals">Globals</a></li> 394<li><a href="http.html" class="nav-http">HTTP</a></li> 395<li><a href="http2.html" class="nav-http2 active">HTTP/2</a></li> 396<li><a href="https.html" class="nav-https">HTTPS</a></li> 397<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 398<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 399<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 400<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 401<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 402<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 403<li><a href="net.html" class="nav-net">Net</a></li> 404<li><a href="os.html" class="nav-os">OS</a></li> 405<li><a href="path.html" class="nav-path">Path</a></li> 406<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 407<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 408<li><a href="process.html" class="nav-process">Process</a></li> 409<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 410<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 411<li><a href="readline.html" class="nav-readline">Readline</a></li> 412<li><a href="repl.html" class="nav-repl">REPL</a></li> 413<li><a href="report.html" class="nav-report">Report</a></li> 414<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 415<li><a href="stream.html" class="nav-stream">Stream</a></li> 416<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 417<li><a href="test.html" class="nav-test">Test runner</a></li> 418<li><a href="timers.html" class="nav-timers">Timers</a></li> 419<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 420<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 421<li><a href="tty.html" class="nav-tty">TTY</a></li> 422<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 423<li><a href="url.html" class="nav-url">URL</a></li> 424<li><a href="util.html" class="nav-util">Utilities</a></li> 425<li><a href="v8.html" class="nav-v8">V8</a></li> 426<li><a href="vm.html" class="nav-vm">VM</a></li> 427<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 428<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 429<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 430<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 431<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 432</ul> 433<hr class="line"> 434<ul> 435<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 436</ul></div> 437 </li> 438 439 440 <li class="picker-header"> 441 <a href="#"> 442 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 443 Other versions 444 </a> 445 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/http2.html">20.x</a></li> 446<li><a href="https://nodejs.org/docs/latest-v19.x/api/http2.html">19.x</a></li> 447<li><a href="https://nodejs.org/docs/latest-v18.x/api/http2.html">18.x <b>LTS</b></a></li> 448<li><a href="https://nodejs.org/docs/latest-v17.x/api/http2.html">17.x</a></li> 449<li><a href="https://nodejs.org/docs/latest-v16.x/api/http2.html">16.x</a></li> 450<li><a href="https://nodejs.org/docs/latest-v15.x/api/http2.html">15.x</a></li> 451<li><a href="https://nodejs.org/docs/latest-v14.x/api/http2.html">14.x</a></li> 452<li><a href="https://nodejs.org/docs/latest-v13.x/api/http2.html">13.x</a></li> 453<li><a href="https://nodejs.org/docs/latest-v12.x/api/http2.html">12.x</a></li> 454<li><a href="https://nodejs.org/docs/latest-v11.x/api/http2.html">11.x</a></li> 455<li><a href="https://nodejs.org/docs/latest-v10.x/api/http2.html">10.x</a></li> 456<li><a href="https://nodejs.org/docs/latest-v9.x/api/http2.html">9.x</a></li> 457<li><a href="https://nodejs.org/docs/latest-v8.x/api/http2.html">8.x</a></li></ol></div> 458 </li> 459 460 <li class="picker-header"> 461 <a href="#"> 462 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 463 Options 464 </a> 465 466 <div class="picker"> 467 <ul> 468 <li> 469 <a href="all.html">View on single page</a> 470 </li> 471 <li> 472 <a href="http2.json">View as JSON</a> 473 </li> 474 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/http2.md">Edit on GitHub</a></li> 475 </ul> 476 </div> 477 </li> 478 </ul> 479 </div> 480 <hr> 481 </header> 482 483 <details id="toc" open><summary>Table of contents</summary><ul> 484<li><span class="stability_2"><a href="#http2">HTTP/2</a></span> 485<ul> 486<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li> 487<li><a href="#core-api">Core API</a> 488<ul> 489<li><a href="#server-side-example">Server-side example</a></li> 490<li><a href="#client-side-example">Client-side example</a></li> 491<li><a href="#class-http2session">Class: <code>Http2Session</code></a> 492<ul> 493<li><a href="#http2session-and-sockets"><code>Http2Session</code> and sockets</a></li> 494<li><a href="#event-close">Event: <code>'close'</code></a></li> 495<li><a href="#event-connect">Event: <code>'connect'</code></a></li> 496<li><a href="#event-error">Event: <code>'error'</code></a></li> 497<li><a href="#event-frameerror">Event: <code>'frameError'</code></a></li> 498<li><a href="#event-goaway">Event: <code>'goaway'</code></a></li> 499<li><a href="#event-localsettings">Event: <code>'localSettings'</code></a></li> 500<li><a href="#event-ping">Event: <code>'ping'</code></a></li> 501<li><a href="#event-remotesettings">Event: <code>'remoteSettings'</code></a></li> 502<li><a href="#event-stream">Event: <code>'stream'</code></a></li> 503<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li> 504<li><a href="#http2sessionalpnprotocol"><code>http2session.alpnProtocol</code></a></li> 505<li><a href="#http2sessionclosecallback"><code>http2session.close([callback])</code></a></li> 506<li><a href="#http2sessionclosed"><code>http2session.closed</code></a></li> 507<li><a href="#http2sessionconnecting"><code>http2session.connecting</code></a></li> 508<li><a href="#http2sessiondestroyerror-code"><code>http2session.destroy([error][, code])</code></a></li> 509<li><a href="#http2sessiondestroyed"><code>http2session.destroyed</code></a></li> 510<li><a href="#http2sessionencrypted"><code>http2session.encrypted</code></a></li> 511<li><a href="#http2sessiongoawaycode-laststreamid-opaquedata"><code>http2session.goaway([code[, lastStreamID[, opaqueData]]])</code></a></li> 512<li><a href="#http2sessionlocalsettings"><code>http2session.localSettings</code></a></li> 513<li><a href="#http2sessionoriginset"><code>http2session.originSet</code></a></li> 514<li><a href="#http2sessionpendingsettingsack"><code>http2session.pendingSettingsAck</code></a></li> 515<li><a href="#http2sessionpingpayload-callback"><code>http2session.ping([payload, ]callback)</code></a></li> 516<li><a href="#http2sessionref"><code>http2session.ref()</code></a></li> 517<li><a href="#http2sessionremotesettings"><code>http2session.remoteSettings</code></a></li> 518<li><a href="#http2sessionsetlocalwindowsizewindowsize"><code>http2session.setLocalWindowSize(windowSize)</code></a></li> 519<li><a href="#http2sessionsettimeoutmsecs-callback"><code>http2session.setTimeout(msecs, callback)</code></a></li> 520<li><a href="#http2sessionsocket"><code>http2session.socket</code></a></li> 521<li><a href="#http2sessionstate"><code>http2session.state</code></a></li> 522<li><a href="#http2sessionsettingssettings-callback"><code>http2session.settings([settings][, callback])</code></a></li> 523<li><a href="#http2sessiontype"><code>http2session.type</code></a></li> 524<li><a href="#http2sessionunref"><code>http2session.unref()</code></a></li> 525</ul> 526</li> 527<li><a href="#class-serverhttp2session">Class: <code>ServerHttp2Session</code></a> 528<ul> 529<li><a href="#serverhttp2sessionaltsvcalt-originorstream"><code>serverhttp2session.altsvc(alt, originOrStream)</code></a></li> 530<li><a href="#specifying-alternative-services">Specifying alternative services</a></li> 531<li><a href="#serverhttp2sessionoriginorigins"><code>serverhttp2session.origin(...origins)</code></a></li> 532</ul> 533</li> 534<li><a href="#class-clienthttp2session">Class: <code>ClientHttp2Session</code></a> 535<ul> 536<li><a href="#event-altsvc">Event: <code>'altsvc'</code></a></li> 537<li><a href="#event-origin">Event: <code>'origin'</code></a></li> 538<li><a href="#clienthttp2sessionrequestheaders-options"><code>clienthttp2session.request(headers[, options])</code></a></li> 539</ul> 540</li> 541<li><a href="#class-http2stream">Class: <code>Http2Stream</code></a> 542<ul> 543<li><a href="#http2stream-lifecycle"><code>Http2Stream</code> Lifecycle</a> 544<ul> 545<li><a href="#creation">Creation</a></li> 546<li><a href="#destruction">Destruction</a></li> 547</ul> 548</li> 549<li><a href="#event-aborted">Event: <code>'aborted'</code></a></li> 550<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 551<li><a href="#event-error_1">Event: <code>'error'</code></a></li> 552<li><a href="#event-frameerror_1">Event: <code>'frameError'</code></a></li> 553<li><a href="#event-ready">Event: <code>'ready'</code></a></li> 554<li><a href="#event-timeout_1">Event: <code>'timeout'</code></a></li> 555<li><a href="#event-trailers">Event: <code>'trailers'</code></a></li> 556<li><a href="#event-wanttrailers">Event: <code>'wantTrailers'</code></a></li> 557<li><a href="#http2streamaborted"><code>http2stream.aborted</code></a></li> 558<li><a href="#http2streambuffersize"><code>http2stream.bufferSize</code></a></li> 559<li><a href="#http2streamclosecode-callback"><code>http2stream.close(code[, callback])</code></a></li> 560<li><a href="#http2streamclosed"><code>http2stream.closed</code></a></li> 561<li><a href="#http2streamdestroyed"><code>http2stream.destroyed</code></a></li> 562<li><a href="#http2streamendafterheaders"><code>http2stream.endAfterHeaders</code></a></li> 563<li><a href="#http2streamid"><code>http2stream.id</code></a></li> 564<li><a href="#http2streampending"><code>http2stream.pending</code></a></li> 565<li><a href="#http2streampriorityoptions"><code>http2stream.priority(options)</code></a></li> 566<li><a href="#http2streamrstcode"><code>http2stream.rstCode</code></a></li> 567<li><a href="#http2streamsentheaders"><code>http2stream.sentHeaders</code></a></li> 568<li><a href="#http2streamsentinfoheaders"><code>http2stream.sentInfoHeaders</code></a></li> 569<li><a href="#http2streamsenttrailers"><code>http2stream.sentTrailers</code></a></li> 570<li><a href="#http2streamsession"><code>http2stream.session</code></a></li> 571<li><a href="#http2streamsettimeoutmsecs-callback"><code>http2stream.setTimeout(msecs, callback)</code></a></li> 572<li><a href="#http2streamstate"><code>http2stream.state</code></a></li> 573<li><a href="#http2streamsendtrailersheaders"><code>http2stream.sendTrailers(headers)</code></a></li> 574</ul> 575</li> 576<li><a href="#class-clienthttp2stream">Class: <code>ClientHttp2Stream</code></a> 577<ul> 578<li><a href="#event-continue">Event: <code>'continue'</code></a></li> 579<li><a href="#event-headers">Event: <code>'headers'</code></a></li> 580<li><a href="#event-push">Event: <code>'push'</code></a></li> 581<li><a href="#event-response">Event: <code>'response'</code></a></li> 582</ul> 583</li> 584<li><a href="#class-serverhttp2stream">Class: <code>ServerHttp2Stream</code></a> 585<ul> 586<li><a href="#http2streamadditionalheadersheaders"><code>http2stream.additionalHeaders(headers)</code></a></li> 587<li><a href="#http2streamheaderssent"><code>http2stream.headersSent</code></a></li> 588<li><a href="#http2streampushallowed"><code>http2stream.pushAllowed</code></a></li> 589<li><a href="#http2streampushstreamheaders-options-callback"><code>http2stream.pushStream(headers[, options], callback)</code></a></li> 590<li><a href="#http2streamrespondheaders-options"><code>http2stream.respond([headers[, options]])</code></a></li> 591<li><a href="#http2streamrespondwithfdfd-headers-options"><code>http2stream.respondWithFD(fd[, headers[, options]])</code></a></li> 592<li><a href="#http2streamrespondwithfilepath-headers-options"><code>http2stream.respondWithFile(path[, headers[, options]])</code></a></li> 593</ul> 594</li> 595<li><a href="#class-http2server">Class: <code>Http2Server</code></a> 596<ul> 597<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li> 598<li><a href="#event-connection">Event: <code>'connection'</code></a></li> 599<li><a href="#event-request">Event: <code>'request'</code></a></li> 600<li><a href="#event-session">Event: <code>'session'</code></a></li> 601<li><a href="#event-sessionerror">Event: <code>'sessionError'</code></a></li> 602<li><a href="#event-stream_1">Event: <code>'stream'</code></a></li> 603<li><a href="#event-timeout_2">Event: <code>'timeout'</code></a></li> 604<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li> 605<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li> 606<li><a href="#servertimeout"><code>server.timeout</code></a></li> 607<li><a href="#serverupdatesettingssettings"><code>server.updateSettings([settings])</code></a></li> 608</ul> 609</li> 610<li><a href="#class-http2secureserver">Class: <code>Http2SecureServer</code></a> 611<ul> 612<li><a href="#event-checkcontinue_1">Event: <code>'checkContinue'</code></a></li> 613<li><a href="#event-connection_1">Event: <code>'connection'</code></a></li> 614<li><a href="#event-request_1">Event: <code>'request'</code></a></li> 615<li><a href="#event-session_1">Event: <code>'session'</code></a></li> 616<li><a href="#event-sessionerror_1">Event: <code>'sessionError'</code></a></li> 617<li><a href="#event-stream_2">Event: <code>'stream'</code></a></li> 618<li><a href="#event-timeout_3">Event: <code>'timeout'</code></a></li> 619<li><a href="#event-unknownprotocol">Event: <code>'unknownProtocol'</code></a></li> 620<li><a href="#serverclosecallback_1"><code>server.close([callback])</code></a></li> 621<li><a href="#serversettimeoutmsecs-callback_1"><code>server.setTimeout([msecs][, callback])</code></a></li> 622<li><a href="#servertimeout_1"><code>server.timeout</code></a></li> 623<li><a href="#serverupdatesettingssettings_1"><code>server.updateSettings([settings])</code></a></li> 624</ul> 625</li> 626<li><a href="#http2createserveroptions-onrequesthandler"><code>http2.createServer([options][, onRequestHandler])</code></a></li> 627<li><a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer(options[, onRequestHandler])</code></a></li> 628<li><a href="#http2connectauthority-options-listener"><code>http2.connect(authority[, options][, listener])</code></a></li> 629<li><a href="#http2constants"><code>http2.constants</code></a> 630<ul> 631<li><a href="#error-codes-for-rst_stream-and-goaway">Error codes for <code>RST_STREAM</code> and <code>GOAWAY</code></a></li> 632</ul> 633</li> 634<li><a href="#http2getdefaultsettings"><code>http2.getDefaultSettings()</code></a></li> 635<li><a href="#http2getpackedsettingssettings"><code>http2.getPackedSettings([settings])</code></a></li> 636<li><a href="#http2getunpackedsettingsbuf"><code>http2.getUnpackedSettings(buf)</code></a></li> 637<li><a href="#http2sensitiveheaders"><code>http2.sensitiveHeaders</code></a></li> 638<li><a href="#headers-object">Headers object</a> 639<ul> 640<li><a href="#sensitive-headers">Sensitive headers</a></li> 641</ul> 642</li> 643<li><a href="#settings-object">Settings object</a></li> 644<li><a href="#error-handling">Error handling</a></li> 645<li><a href="#invalid-character-handling-in-header-names-and-values">Invalid character handling in header names and values</a></li> 646<li><a href="#push-streams-on-the-client">Push streams on the client</a></li> 647<li><a href="#supporting-the-connect-method">Supporting the <code>CONNECT</code> method</a></li> 648<li><a href="#the-extended-connect-protocol">The extended <code>CONNECT</code> protocol</a></li> 649</ul> 650</li> 651<li><a href="#compatibility-api">Compatibility API</a> 652<ul> 653<li><a href="#alpn-negotiation">ALPN negotiation</a></li> 654<li><a href="#class-http2http2serverrequest">Class: <code>http2.Http2ServerRequest</code></a> 655<ul> 656<li><a href="#event-aborted_1">Event: <code>'aborted'</code></a></li> 657<li><a href="#event-close_2">Event: <code>'close'</code></a></li> 658<li><a href="#requestaborted"><code>request.aborted</code></a></li> 659<li><a href="#requestauthority"><code>request.authority</code></a></li> 660<li><a href="#requestcomplete"><code>request.complete</code></a></li> 661<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li> 662<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a></li> 663<li><a href="#requestheaders"><code>request.headers</code></a></li> 664<li><a href="#requesthttpversion"><code>request.httpVersion</code></a></li> 665<li><a href="#requestmethod"><code>request.method</code></a></li> 666<li><a href="#requestrawheaders"><code>request.rawHeaders</code></a></li> 667<li><a href="#requestrawtrailers"><code>request.rawTrailers</code></a></li> 668<li><a href="#requestscheme"><code>request.scheme</code></a></li> 669<li><a href="#requestsettimeoutmsecs-callback"><code>request.setTimeout(msecs, callback)</code></a></li> 670<li><a href="#requestsocket"><code>request.socket</code></a></li> 671<li><a href="#requeststream"><code>request.stream</code></a></li> 672<li><a href="#requesttrailers"><code>request.trailers</code></a></li> 673<li><a href="#requesturl"><code>request.url</code></a></li> 674</ul> 675</li> 676<li><a href="#class-http2http2serverresponse">Class: <code>http2.Http2ServerResponse</code></a> 677<ul> 678<li><a href="#event-close_3">Event: <code>'close'</code></a></li> 679<li><a href="#event-finish">Event: <code>'finish'</code></a></li> 680<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li> 681<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li> 682<li><a href="#responsecreatepushresponseheaders-callback"><code>response.createPushResponse(headers, callback)</code></a></li> 683<li><a href="#responseenddata-encoding-callback"><code>response.end([data[, encoding]][, callback])</code></a></li> 684<li><span class="stability_0"><a href="#responsefinished"><code>response.finished</code></a></span></li> 685<li><a href="#responsegetheadername"><code>response.getHeader(name)</code></a></li> 686<li><a href="#responsegetheadernames"><code>response.getHeaderNames()</code></a></li> 687<li><a href="#responsegetheaders"><code>response.getHeaders()</code></a></li> 688<li><a href="#responsehasheadername"><code>response.hasHeader(name)</code></a></li> 689<li><a href="#responseheaderssent"><code>response.headersSent</code></a></li> 690<li><a href="#responseremoveheadername"><code>response.removeHeader(name)</code></a></li> 691<li><a href="#responsereq"><code>response.req</code></a></li> 692<li><a href="#responsesenddate"><code>response.sendDate</code></a></li> 693<li><a href="#responsesetheadername-value"><code>response.setHeader(name, value)</code></a></li> 694<li><a href="#responsesettimeoutmsecs-callback"><code>response.setTimeout(msecs[, callback])</code></a></li> 695<li><a href="#responsesocket"><code>response.socket</code></a></li> 696<li><a href="#responsestatuscode"><code>response.statusCode</code></a></li> 697<li><a href="#responsestatusmessage"><code>response.statusMessage</code></a></li> 698<li><a href="#responsestream"><code>response.stream</code></a></li> 699<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li> 700<li><a href="#responsewritechunk-encoding-callback"><code>response.write(chunk[, encoding][, callback])</code></a></li> 701<li><a href="#responsewritecontinue"><code>response.writeContinue()</code></a></li> 702<li><a href="#responsewriteearlyhintshints"><code>response.writeEarlyHints(hints)</code></a></li> 703<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li> 704</ul> 705</li> 706</ul> 707</li> 708<li><a href="#collecting-http2-performance-metrics">Collecting HTTP/2 performance metrics</a></li> 709<li><a href="#note-on-authority-and-host">Note on <code>:authority</code> and <code>host</code></a></li> 710</ul> 711</li> 712</ul></details> 713 714 <div id="apicontent"> 715 <h2>HTTP/2<span><a class="mark" href="#http2" id="http2">#</a></span><a aria-hidden="true" class="legacy" id="http2_http_2"></a></h2> 716<div class="api_metadata"> 717<details class="changelog"><summary>History</summary> 718<table> 719<tbody><tr><th>Version</th><th>Changes</th></tr> 720<tr><td>v15.0.0</td> 721<td><p>Requests with the <code>host</code> header (with or without <code>:authority</code>) can now be sent/received.</p></td></tr> 722<tr><td>v15.3.0, v14.17.0</td> 723<td><p>It is possible to abort a request with an AbortSignal.</p></td></tr> 724<tr><td>v10.10.0</td> 725<td><p>HTTP/2 is now Stable. Previously, it had been Experimental.</p></td></tr> 726<tr><td>v8.4.0</td> 727<td><p><span>Added in: v8.4.0</span></p></td></tr> 728</tbody></table> 729</details> 730</div> 731 732<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 733<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/http2.js">lib/http2.js</a></p> 734<p>The <code>node:http2</code> module provides an implementation of the <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> protocol. 735It can be accessed using:</p> 736<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);</code> <button class="copy-button">copy</button></pre> 737<section><h3>Determining if crypto support is unavailable<span><a class="mark" href="#determining-if-crypto-support-is-unavailable" id="determining-if-crypto-support-is-unavailable">#</a></span><a aria-hidden="true" class="legacy" id="http2_determining_if_crypto_support_is_unavailable"></a></h3> 738<p>It is possible for Node.js to be built without including support for the 739<code>node:crypto</code> module. In such cases, attempting to <code>import</code> from <code>node:http2</code> or 740calling <code>require('node:http2')</code> will result in an error being thrown.</p> 741<p>When using CommonJS, the error thrown can be caught using try/catch:</p> 742<pre><code class="language-js cjs"><span class="hljs-keyword">let</span> http2; 743<span class="hljs-keyword">try</span> { 744 http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 745} <span class="hljs-keyword">catch</span> (err) { 746 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'http2 support is disabled!'</span>); 747}</code> <button class="copy-button">copy</button></pre> 748<p>When using the lexical ESM <code>import</code> keyword, the error can only be 749caught if a handler for <code>process.on('uncaughtException')</code> is registered 750<em>before</em> any attempt to load the module is made (using, for instance, 751a preload module).</p> 752<p>When using ESM, if there is a chance that the code may be run on a build 753of Node.js where crypto support is not enabled, consider using the 754<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import"><code>import()</code></a> function instead of the lexical <code>import</code> keyword:</p> 755<pre><code class="language-js mjs"><span class="hljs-keyword">let</span> http2; 756<span class="hljs-keyword">try</span> { 757 http2 = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:http2'</span>); 758} <span class="hljs-keyword">catch</span> (err) { 759 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'http2 support is disabled!'</span>); 760}</code> <button class="copy-button">copy</button></pre> 761</section><section><h3>Core API<span><a class="mark" href="#core-api" id="core-api">#</a></span><a aria-hidden="true" class="legacy" id="http2_core_api"></a></h3> 762<p>The Core API provides a low-level interface designed specifically around 763support for HTTP/2 protocol features. It is specifically <em>not</em> designed for 764compatibility with the existing <a href="http.html">HTTP/1</a> module API. However, 765the <a href="#compatibility-api">Compatibility API</a> is.</p> 766<p>The <code>http2</code> Core API is much more symmetric between client and server than the 767<code>http</code> API. For instance, most events, like <code>'error'</code>, <code>'connect'</code> and 768<code>'stream'</code>, can be emitted either by client-side code or server-side code.</p> 769<h4>Server-side example<span><a class="mark" href="#server-side-example" id="server-side-example">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_side_example"></a></h4> 770<p>The following illustrates a simple HTTP/2 server using the Core API. 771Since there are no browsers known that support 772<a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the use of 773<a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a> is necessary when communicating 774with browser clients.</p> 775<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 776<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 777 778<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>({ 779 <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-privkey.pem'</span>), 780 <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-cert.pem'</span>), 781}); 782server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err)); 783 784server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> { 785 <span class="hljs-comment">// stream is a Duplex</span> 786 stream.<span class="hljs-title function_">respond</span>({ 787 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>, 788 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>, 789 }); 790 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>); 791}); 792 793server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</code> <button class="copy-button">copy</button></pre> 794<p>To generate the certificate and key for this example, run:</p> 795<pre><code class="language-bash">openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj <span class="hljs-string">'/CN=localhost'</span> \ 796 -keyout localhost-privkey.pem -out localhost-cert.pem</code> <button class="copy-button">copy</button></pre> 797<h4>Client-side example<span><a class="mark" href="#client-side-example" id="client-side-example">#</a></span><a aria-hidden="true" class="legacy" id="http2_client_side_example"></a></h4> 798<p>The following illustrates an HTTP/2 client:</p> 799<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 800<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 801<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:8443'</span>, { 802 <span class="hljs-attr">ca</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-cert.pem'</span>), 803}); 804client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err)); 805 806<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> }); 807 808req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers, flags</span>) =></span> { 809 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">in</span> headers) { 810 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${name}</span>: <span class="hljs-subst">${headers[name]}</span>`</span>); 811 } 812}); 813 814req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>); 815<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>; 816req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> { data += chunk; }); 817req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 818 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`\n<span class="hljs-subst">${data}</span>`</span>); 819 client.<span class="hljs-title function_">close</span>(); 820}); 821req.<span class="hljs-title function_">end</span>();</code> <button class="copy-button">copy</button></pre> 822<h4>Class: <code>Http2Session</code><span><a class="mark" href="#class-http2session" id="class-http2session">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2session"></a></h4> 823<div class="api_metadata"> 824<span>Added in: v8.4.0</span> 825</div> 826<ul> 827<li>Extends: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 828</ul> 829<p>Instances of the <code>http2.Http2Session</code> class represent an active communications 830session between an HTTP/2 client and server. Instances of this class are <em>not</em> 831intended to be constructed directly by user code.</p> 832<p>Each <code>Http2Session</code> instance will exhibit slightly different behaviors 833depending on whether it is operating as a server or a client. The 834<code>http2session.type</code> property can be used to determine the mode in which an 835<code>Http2Session</code> is operating. On the server side, user code should rarely 836have occasion to work with the <code>Http2Session</code> object directly, with most 837actions typically taken through interactions with either the <code>Http2Server</code> or 838<code>Http2Stream</code> objects.</p> 839<p>User code will not create <code>Http2Session</code> instances directly. Server-side 840<code>Http2Session</code> instances are created by the <code>Http2Server</code> instance when a 841new HTTP/2 connection is received. Client-side <code>Http2Session</code> instances are 842created using the <code>http2.connect()</code> method.</p> 843<h5><code>Http2Session</code> and sockets<span><a class="mark" href="#http2session-and-sockets" id="http2session-and-sockets">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_and_sockets"></a></h5> 844<p>Every <code>Http2Session</code> instance is associated with exactly one <a href="net.html#class-netsocket"><code>net.Socket</code></a> or 845<a href="tls.html#class-tlstlssocket"><code>tls.TLSSocket</code></a> when it is created. When either the <code>Socket</code> or the 846<code>Http2Session</code> are destroyed, both will be destroyed.</p> 847<p>Because of the specific serialization and processing requirements imposed 848by the HTTP/2 protocol, it is not recommended for user code to read data from 849or write data to a <code>Socket</code> instance bound to a <code>Http2Session</code>. Doing so can 850put the HTTP/2 session into an indeterminate state causing the session and 851the socket to become unusable.</p> 852<p>Once a <code>Socket</code> has been bound to an <code>Http2Session</code>, user code should rely 853solely on the API of the <code>Http2Session</code>.</p> 854<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close" id="event-close">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_close"></a></h5> 855<div class="api_metadata"> 856<span>Added in: v8.4.0</span> 857</div> 858<p>The <code>'close'</code> event is emitted once the <code>Http2Session</code> has been destroyed. Its 859listener does not expect any arguments.</p> 860<h5>Event: <code>'connect'</code><span><a class="mark" href="#event-connect" id="event-connect">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_connect"></a></h5> 861<div class="api_metadata"> 862<span>Added in: v8.4.0</span> 863</div> 864<ul> 865<li><code>session</code> <a href="http2.html#class-http2session" class="type"><Http2Session></a></li> 866<li><code>socket</code> <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 867</ul> 868<p>The <code>'connect'</code> event is emitted once the <code>Http2Session</code> has been successfully 869connected to the remote peer and communication may begin.</p> 870<p>User code will typically not listen for this event directly.</p> 871<h5>Event: <code>'error'</code><span><a class="mark" href="#event-error" id="event-error">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_error"></a></h5> 872<div class="api_metadata"> 873<span>Added in: v8.4.0</span> 874</div> 875<ul> 876<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 877</ul> 878<p>The <code>'error'</code> event is emitted when an error occurs during the processing of 879an <code>Http2Session</code>.</p> 880<h5>Event: <code>'frameError'</code><span><a class="mark" href="#event-frameerror" id="event-frameerror">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_frameerror"></a></h5> 881<div class="api_metadata"> 882<span>Added in: v8.4.0</span> 883</div> 884<ul> 885<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The frame type.</li> 886<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The error code.</li> 887<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The stream id (or <code>0</code> if the frame isn't associated with a 888stream).</li> 889</ul> 890<p>The <code>'frameError'</code> event is emitted when an error occurs while attempting to 891send a frame on the session. If the frame that could not be sent is associated 892with a specific <code>Http2Stream</code>, an attempt to emit a <code>'frameError'</code> event on the 893<code>Http2Stream</code> is made.</p> 894<p>If the <code>'frameError'</code> event is associated with a stream, the stream will be 895closed and destroyed immediately following the <code>'frameError'</code> event. If the 896event is not associated with a stream, the <code>Http2Session</code> will be shut down 897immediately following the <code>'frameError'</code> event.</p> 898<h5>Event: <code>'goaway'</code><span><a class="mark" href="#event-goaway" id="event-goaway">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_goaway"></a></h5> 899<div class="api_metadata"> 900<span>Added in: v8.4.0</span> 901</div> 902<ul> 903<li><code>errorCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The HTTP/2 error code specified in the <code>GOAWAY</code> frame.</li> 904<li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The ID of the last stream the remote peer successfully 905processed (or <code>0</code> if no ID is specified).</li> 906<li><code>opaqueData</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> If additional opaque data was included in the <code>GOAWAY</code> 907frame, a <code>Buffer</code> instance will be passed containing that data.</li> 908</ul> 909<p>The <code>'goaway'</code> event is emitted when a <code>GOAWAY</code> frame is received.</p> 910<p>The <code>Http2Session</code> instance will be shut down automatically when the <code>'goaway'</code> 911event is emitted.</p> 912<h5>Event: <code>'localSettings'</code><span><a class="mark" href="#event-localsettings" id="event-localsettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_localsettings"></a></h5> 913<div class="api_metadata"> 914<span>Added in: v8.4.0</span> 915</div> 916<ul> 917<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a> A copy of the <code>SETTINGS</code> frame received.</li> 918</ul> 919<p>The <code>'localSettings'</code> event is emitted when an acknowledgment <code>SETTINGS</code> frame 920has been received.</p> 921<p>When using <code>http2session.settings()</code> to submit new settings, the modified 922settings do not take effect until the <code>'localSettings'</code> event is emitted.</p> 923<pre><code class="language-js">session.<span class="hljs-title function_">settings</span>({ <span class="hljs-attr">enablePush</span>: <span class="hljs-literal">false</span> }); 924 925session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'localSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> { 926 <span class="hljs-comment">/* Use the new settings */</span> 927});</code> <button class="copy-button">copy</button></pre> 928<h5>Event: <code>'ping'</code><span><a class="mark" href="#event-ping" id="event-ping">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_ping"></a></h5> 929<div class="api_metadata"> 930<span>Added in: v10.12.0</span> 931</div> 932<ul> 933<li><code>payload</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> The <code>PING</code> frame 8-byte payload</li> 934</ul> 935<p>The <code>'ping'</code> event is emitted whenever a <code>PING</code> frame is received from the 936connected peer.</p> 937<h5>Event: <code>'remoteSettings'</code><span><a class="mark" href="#event-remotesettings" id="event-remotesettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_remotesettings"></a></h5> 938<div class="api_metadata"> 939<span>Added in: v8.4.0</span> 940</div> 941<ul> 942<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a> A copy of the <code>SETTINGS</code> frame received.</li> 943</ul> 944<p>The <code>'remoteSettings'</code> event is emitted when a new <code>SETTINGS</code> frame is received 945from the connected peer.</p> 946<pre><code class="language-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> { 947 <span class="hljs-comment">/* Use the new settings */</span> 948});</code> <button class="copy-button">copy</button></pre> 949<h5>Event: <code>'stream'</code><span><a class="mark" href="#event-stream" id="event-stream">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_stream"></a></h5> 950<div class="api_metadata"> 951<span>Added in: v8.4.0</span> 952</div> 953<ul> 954<li><code>stream</code> <a href="http2.html#class-http2stream" class="type"><Http2Stream></a> A reference to the stream</li> 955<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li> 956<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The associated numeric flags</li> 957<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array containing the raw header names followed by 958their respective values.</li> 959</ul> 960<p>The <code>'stream'</code> event is emitted when a new <code>Http2Stream</code> is created.</p> 961<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 962session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers, flags</span>) =></span> { 963 <span class="hljs-keyword">const</span> method = headers[<span class="hljs-string">':method'</span>]; 964 <span class="hljs-keyword">const</span> path = headers[<span class="hljs-string">':path'</span>]; 965 <span class="hljs-comment">// ...</span> 966 stream.<span class="hljs-title function_">respond</span>({ 967 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>, 968 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>, 969 }); 970 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>); 971 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>); 972});</code> <button class="copy-button">copy</button></pre> 973<p>On the server side, user code will typically not listen for this event directly, 974and would instead register a handler for the <code>'stream'</code> event emitted by the 975<code>net.Server</code> or <code>tls.Server</code> instances returned by <code>http2.createServer()</code> and 976<code>http2.createSecureServer()</code>, respectively, as in the example below:</p> 977<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 978 979<span class="hljs-comment">// Create an unencrypted HTTP/2 server</span> 980<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 981 982server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> { 983 stream.<span class="hljs-title function_">respond</span>({ 984 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>, 985 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>, 986 }); 987 stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">error</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(error)); 988 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>); 989}); 990 991server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre> 992<p>Even though HTTP/2 streams and network sockets are not in a 1:1 correspondence, 993a network error will destroy each individual stream and must be handled on the 994stream level, as shown above.</p> 995<h5>Event: <code>'timeout'</code><span><a class="mark" href="#event-timeout" id="event-timeout">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_timeout"></a></h5> 996<div class="api_metadata"> 997<span>Added in: v8.4.0</span> 998</div> 999<p>After the <code>http2session.setTimeout()</code> method is used to set the timeout period 1000for this <code>Http2Session</code>, the <code>'timeout'</code> event is emitted if there is no 1001activity on the <code>Http2Session</code> after the configured number of milliseconds. 1002Its listener does not expect any arguments.</p> 1003<pre><code class="language-js">session.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">2000</span>); 1004session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <span class="hljs-function">() =></span> { <span class="hljs-comment">/* .. */</span> });</code> <button class="copy-button">copy</button></pre> 1005<h5><code>http2session.alpnProtocol</code><span><a class="mark" href="#http2sessionalpnprotocol" id="http2sessionalpnprotocol">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_alpnprotocol"></a></h5> 1006<div class="api_metadata"> 1007<span>Added in: v9.4.0</span> 1008</div> 1009<ul> 1010<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 1011</ul> 1012<p>Value will be <code>undefined</code> if the <code>Http2Session</code> is not yet connected to a 1013socket, <code>h2c</code> if the <code>Http2Session</code> is not connected to a <code>TLSSocket</code>, or 1014will return the value of the connected <code>TLSSocket</code>'s own <code>alpnProtocol</code> 1015property.</p> 1016<h5><code>http2session.close([callback])</code><span><a class="mark" href="#http2sessionclosecallback" id="http2sessionclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_close_callback"></a></h5> 1017<div class="api_metadata"> 1018<span>Added in: v9.4.0</span> 1019</div> 1020<ul> 1021<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1022</ul> 1023<p>Gracefully closes the <code>Http2Session</code>, allowing any existing streams to 1024complete on their own and preventing new <code>Http2Stream</code> instances from being 1025created. Once closed, <code>http2session.destroy()</code> <em>might</em> be called if there 1026are no open <code>Http2Stream</code> instances.</p> 1027<p>If specified, the <code>callback</code> function is registered as a handler for the 1028<code>'close'</code> event.</p> 1029<h5><code>http2session.closed</code><span><a class="mark" href="#http2sessionclosed" id="http2sessionclosed">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_closed"></a></h5> 1030<div class="api_metadata"> 1031<span>Added in: v9.4.0</span> 1032</div> 1033<ul> 1034<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1035</ul> 1036<p>Will be <code>true</code> if this <code>Http2Session</code> instance has been closed, otherwise 1037<code>false</code>.</p> 1038<h5><code>http2session.connecting</code><span><a class="mark" href="#http2sessionconnecting" id="http2sessionconnecting">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_connecting"></a></h5> 1039<div class="api_metadata"> 1040<span>Added in: v10.0.0</span> 1041</div> 1042<ul> 1043<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1044</ul> 1045<p>Will be <code>true</code> if this <code>Http2Session</code> instance is still connecting, will be set 1046to <code>false</code> before emitting <code>connect</code> event and/or calling the <code>http2.connect</code> 1047callback.</p> 1048<h5><code>http2session.destroy([error][, code])</code><span><a class="mark" href="#http2sessiondestroyerror-code" id="http2sessiondestroyerror-code">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_destroy_error_code"></a></h5> 1049<div class="api_metadata"> 1050<span>Added in: v8.4.0</span> 1051</div> 1052<ul> 1053<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> An <code>Error</code> object if the <code>Http2Session</code> is being destroyed 1054due to an error.</li> 1055<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The HTTP/2 error code to send in the final <code>GOAWAY</code> frame. 1056If unspecified, and <code>error</code> is not undefined, the default is <code>INTERNAL_ERROR</code>, 1057otherwise defaults to <code>NO_ERROR</code>.</li> 1058</ul> 1059<p>Immediately terminates the <code>Http2Session</code> and the associated <code>net.Socket</code> or 1060<code>tls.TLSSocket</code>.</p> 1061<p>Once destroyed, the <code>Http2Session</code> will emit the <code>'close'</code> event. If <code>error</code> 1062is not undefined, an <code>'error'</code> event will be emitted immediately before the 1063<code>'close'</code> event.</p> 1064<p>If there are any remaining open <code>Http2Streams</code> associated with the 1065<code>Http2Session</code>, those will also be destroyed.</p> 1066<h5><code>http2session.destroyed</code><span><a class="mark" href="#http2sessiondestroyed" id="http2sessiondestroyed">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_destroyed"></a></h5> 1067<div class="api_metadata"> 1068<span>Added in: v8.4.0</span> 1069</div> 1070<ul> 1071<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1072</ul> 1073<p>Will be <code>true</code> if this <code>Http2Session</code> instance has been destroyed and must no 1074longer be used, otherwise <code>false</code>.</p> 1075<h5><code>http2session.encrypted</code><span><a class="mark" href="#http2sessionencrypted" id="http2sessionencrypted">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_encrypted"></a></h5> 1076<div class="api_metadata"> 1077<span>Added in: v9.4.0</span> 1078</div> 1079<ul> 1080<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 1081</ul> 1082<p>Value is <code>undefined</code> if the <code>Http2Session</code> session socket has not yet been 1083connected, <code>true</code> if the <code>Http2Session</code> is connected with a <code>TLSSocket</code>, 1084and <code>false</code> if the <code>Http2Session</code> is connected to any other kind of socket 1085or stream.</p> 1086<h5><code>http2session.goaway([code[, lastStreamID[, opaqueData]]])</code><span><a class="mark" href="#http2sessiongoawaycode-laststreamid-opaquedata" id="http2sessiongoawaycode-laststreamid-opaquedata">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_goaway_code_laststreamid_opaquedata"></a></h5> 1087<div class="api_metadata"> 1088<span>Added in: v9.4.0</span> 1089</div> 1090<ul> 1091<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> An HTTP/2 error code</li> 1092<li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The numeric ID of the last processed <code>Http2Stream</code></li> 1093<li><code>opaqueData</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A <code>TypedArray</code> or <code>DataView</code> 1094instance containing additional data to be carried within the <code>GOAWAY</code> frame.</li> 1095</ul> 1096<p>Transmits a <code>GOAWAY</code> frame to the connected peer <em>without</em> shutting down the 1097<code>Http2Session</code>.</p> 1098<h5><code>http2session.localSettings</code><span><a class="mark" href="#http2sessionlocalsettings" id="http2sessionlocalsettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_localsettings"></a></h5> 1099<div class="api_metadata"> 1100<span>Added in: v8.4.0</span> 1101</div> 1102<ul> 1103<li><a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li> 1104</ul> 1105<p>A prototype-less object describing the current local settings of this 1106<code>Http2Session</code>. The local settings are local to <em>this</em> <code>Http2Session</code> instance.</p> 1107<h5><code>http2session.originSet</code><span><a class="mark" href="#http2sessionoriginset" id="http2sessionoriginset">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_originset"></a></h5> 1108<div class="api_metadata"> 1109<span>Added in: v9.4.0</span> 1110</div> 1111<ul> 1112<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 1113</ul> 1114<p>If the <code>Http2Session</code> is connected to a <code>TLSSocket</code>, the <code>originSet</code> property 1115will return an <code>Array</code> of origins for which the <code>Http2Session</code> may be 1116considered authoritative.</p> 1117<p>The <code>originSet</code> property is only available when using a secure TLS connection.</p> 1118<h5><code>http2session.pendingSettingsAck</code><span><a class="mark" href="#http2sessionpendingsettingsack" id="http2sessionpendingsettingsack">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_pendingsettingsack"></a></h5> 1119<div class="api_metadata"> 1120<span>Added in: v8.4.0</span> 1121</div> 1122<ul> 1123<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1124</ul> 1125<p>Indicates whether the <code>Http2Session</code> is currently waiting for acknowledgment of 1126a sent <code>SETTINGS</code> frame. Will be <code>true</code> after calling the 1127<code>http2session.settings()</code> method. Will be <code>false</code> once all sent <code>SETTINGS</code> 1128frames have been acknowledged.</p> 1129<h5><code>http2session.ping([payload, ]callback)</code><span><a class="mark" href="#http2sessionpingpayload-callback" id="http2sessionpingpayload-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_ping_payload_callback"></a></h5> 1130<div class="api_metadata"> 1131<details class="changelog"><summary>History</summary> 1132<table> 1133<tbody><tr><th>Version</th><th>Changes</th></tr> 1134<tr><td>v18.0.0</td> 1135<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 1136<tr><td>v8.9.3</td> 1137<td><p><span>Added in: v8.9.3</span></p></td></tr> 1138</tbody></table> 1139</details> 1140</div> 1141<ul> 1142<li><code>payload</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> Optional ping payload.</li> 1143<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1144<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1145</ul> 1146<p>Sends a <code>PING</code> frame to the connected HTTP/2 peer. A <code>callback</code> function must 1147be provided. The method will return <code>true</code> if the <code>PING</code> was sent, <code>false</code> 1148otherwise.</p> 1149<p>The maximum number of outstanding (unacknowledged) pings is determined by the 1150<code>maxOutstandingPings</code> configuration option. The default maximum is 10.</p> 1151<p>If provided, the <code>payload</code> must be a <code>Buffer</code>, <code>TypedArray</code>, or <code>DataView</code> 1152containing 8 bytes of data that will be transmitted with the <code>PING</code> and 1153returned with the ping acknowledgment.</p> 1154<p>The callback will be invoked with three arguments: an error argument that will 1155be <code>null</code> if the <code>PING</code> was successfully acknowledged, a <code>duration</code> argument 1156that reports the number of milliseconds elapsed since the ping was sent and the 1157acknowledgment was received, and a <code>Buffer</code> containing the 8-byte <code>PING</code> 1158payload.</p> 1159<pre><code class="language-js">session.<span class="hljs-title function_">ping</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'abcdefgh'</span>), <span class="hljs-function">(<span class="hljs-params">err, duration, payload</span>) =></span> { 1160 <span class="hljs-keyword">if</span> (!err) { 1161 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Ping acknowledged in <span class="hljs-subst">${duration}</span> milliseconds`</span>); 1162 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`With payload '<span class="hljs-subst">${payload.toString()}</span>'`</span>); 1163 } 1164});</code> <button class="copy-button">copy</button></pre> 1165<p>If the <code>payload</code> argument is not specified, the default payload will be the 116664-bit timestamp (little endian) marking the start of the <code>PING</code> duration.</p> 1167<h5><code>http2session.ref()</code><span><a class="mark" href="#http2sessionref" id="http2sessionref">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_ref"></a></h5> 1168<div class="api_metadata"> 1169<span>Added in: v9.4.0</span> 1170</div> 1171<p>Calls <a href="net.html#socketref"><code>ref()</code></a> on this <code>Http2Session</code> 1172instance's underlying <a href="net.html#class-netsocket"><code>net.Socket</code></a>.</p> 1173<h5><code>http2session.remoteSettings</code><span><a class="mark" href="#http2sessionremotesettings" id="http2sessionremotesettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_remotesettings"></a></h5> 1174<div class="api_metadata"> 1175<span>Added in: v8.4.0</span> 1176</div> 1177<ul> 1178<li><a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li> 1179</ul> 1180<p>A prototype-less object describing the current remote settings of this 1181<code>Http2Session</code>. The remote settings are set by the <em>connected</em> HTTP/2 peer.</p> 1182<h5><code>http2session.setLocalWindowSize(windowSize)</code><span><a class="mark" href="#http2sessionsetlocalwindowsizewindowsize" id="http2sessionsetlocalwindowsizewindowsize">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_setlocalwindowsize_windowsize"></a></h5> 1183<div class="api_metadata"> 1184<span>Added in: v15.3.0, v14.18.0</span> 1185</div> 1186<ul> 1187<li><code>windowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1188</ul> 1189<p>Sets the local endpoint's window size. 1190The <code>windowSize</code> is the total window size to set, not 1191the delta.</p> 1192<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 1193 1194<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 1195<span class="hljs-keyword">const</span> expectedWindowSize = <span class="hljs-number">2</span> ** <span class="hljs-number">20</span>; 1196server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> { 1197 1198 <span class="hljs-comment">// Set local window size to be 2 ** 20</span> 1199 session.<span class="hljs-title function_">setLocalWindowSize</span>(expectedWindowSize); 1200});</code> <button class="copy-button">copy</button></pre> 1201<h5><code>http2session.setTimeout(msecs, callback)</code><span><a class="mark" href="#http2sessionsettimeoutmsecs-callback" id="http2sessionsettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_settimeout_msecs_callback"></a></h5> 1202<div class="api_metadata"> 1203<details class="changelog"><summary>History</summary> 1204<table> 1205<tbody><tr><th>Version</th><th>Changes</th></tr> 1206<tr><td>v18.0.0</td> 1207<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 1208<tr><td>v8.4.0</td> 1209<td><p><span>Added in: v8.4.0</span></p></td></tr> 1210</tbody></table> 1211</details> 1212</div> 1213<ul> 1214<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1215<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1216</ul> 1217<p>Used to set a callback function that is called when there is no activity on 1218the <code>Http2Session</code> after <code>msecs</code> milliseconds. The given <code>callback</code> is 1219registered as a listener on the <code>'timeout'</code> event.</p> 1220<h5><code>http2session.socket</code><span><a class="mark" href="#http2sessionsocket" id="http2sessionsocket">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_socket"></a></h5> 1221<div class="api_metadata"> 1222<span>Added in: v8.4.0</span> 1223</div> 1224<ul> 1225<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type"><tls.TLSSocket></a></li> 1226</ul> 1227<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but 1228limits available methods to ones safe to use with HTTP/2.</p> 1229<p><code>destroy</code>, <code>emit</code>, <code>end</code>, <code>pause</code>, <code>read</code>, <code>resume</code>, and <code>write</code> will throw 1230an error with code <code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See 1231<a href="#http2session-and-sockets"><code>Http2Session</code> and Sockets</a> for more information.</p> 1232<p><code>setTimeout</code> method will be called on this <code>Http2Session</code>.</p> 1233<p>All other interactions will be routed directly to the socket.</p> 1234<h5><code>http2session.state</code><span><a class="mark" href="#http2sessionstate" id="http2sessionstate">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_state"></a></h5> 1235<div class="api_metadata"> 1236<span>Added in: v8.4.0</span> 1237</div> 1238<p>Provides miscellaneous information about the current state of the 1239<code>Http2Session</code>.</p> 1240<ul> 1241<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1242<ul> 1243<li><code>effectiveLocalWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The current local (receive) 1244flow control window size for the <code>Http2Session</code>.</li> 1245<li><code>effectiveRecvDataLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The current number of bytes 1246that have been received since the last flow control <code>WINDOW_UPDATE</code>.</li> 1247<li><code>nextStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The numeric identifier to be used the 1248next time a new <code>Http2Stream</code> is created by this <code>Http2Session</code>.</li> 1249<li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes that the remote peer can 1250send without receiving a <code>WINDOW_UPDATE</code>.</li> 1251<li><code>lastProcStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The numeric id of the <code>Http2Stream</code> 1252for which a <code>HEADERS</code> or <code>DATA</code> frame was most recently received.</li> 1253<li><code>remoteWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes that this <code>Http2Session</code> 1254may send without receiving a <code>WINDOW_UPDATE</code>.</li> 1255<li><code>outboundQueueSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of frames currently within the 1256outbound queue for this <code>Http2Session</code>.</li> 1257<li><code>deflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The current size in bytes of the 1258outbound header compression state table.</li> 1259<li><code>inflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The current size in bytes of the 1260inbound header compression state table.</li> 1261</ul> 1262</li> 1263</ul> 1264<p>An object describing the current status of this <code>Http2Session</code>.</p> 1265<h5><code>http2session.settings([settings][, callback])</code><span><a class="mark" href="#http2sessionsettingssettings-callback" id="http2sessionsettingssettings-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_settings_settings_callback"></a></h5> 1266<div class="api_metadata"> 1267<details class="changelog"><summary>History</summary> 1268<table> 1269<tbody><tr><th>Version</th><th>Changes</th></tr> 1270<tr><td>v18.0.0</td> 1271<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 1272<tr><td>v8.4.0</td> 1273<td><p><span>Added in: v8.4.0</span></p></td></tr> 1274</tbody></table> 1275</details> 1276</div> 1277<ul> 1278<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li> 1279<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Callback that is called once the session is connected or 1280right away if the session is already connected. 1281<ul> 1282<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li> 1283<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a> The updated <code>settings</code> object.</li> 1284<li><code>duration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1285</ul> 1286</li> 1287</ul> 1288<p>Updates the current local settings for this <code>Http2Session</code> and sends a new 1289<code>SETTINGS</code> frame to the connected HTTP/2 peer.</p> 1290<p>Once called, the <code>http2session.pendingSettingsAck</code> property will be <code>true</code> 1291while the session is waiting for the remote peer to acknowledge the new 1292settings.</p> 1293<p>The new settings will not become effective until the <code>SETTINGS</code> acknowledgment 1294is received and the <code>'localSettings'</code> event is emitted. It is possible to send 1295multiple <code>SETTINGS</code> frames while acknowledgment is still pending.</p> 1296<h5><code>http2session.type</code><span><a class="mark" href="#http2sessiontype" id="http2sessiontype">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_type"></a></h5> 1297<div class="api_metadata"> 1298<span>Added in: v8.4.0</span> 1299</div> 1300<ul> 1301<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1302</ul> 1303<p>The <code>http2session.type</code> will be equal to 1304<code>http2.constants.NGHTTP2_SESSION_SERVER</code> if this <code>Http2Session</code> instance is a 1305server, and <code>http2.constants.NGHTTP2_SESSION_CLIENT</code> if the instance is a 1306client.</p> 1307<h5><code>http2session.unref()</code><span><a class="mark" href="#http2sessionunref" id="http2sessionunref">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_unref"></a></h5> 1308<div class="api_metadata"> 1309<span>Added in: v9.4.0</span> 1310</div> 1311<p>Calls <a href="net.html#socketunref"><code>unref()</code></a> on this <code>Http2Session</code> 1312instance's underlying <a href="net.html#class-netsocket"><code>net.Socket</code></a>.</p> 1313<h4>Class: <code>ServerHttp2Session</code><span><a class="mark" href="#class-serverhttp2session" id="class-serverhttp2session">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_serverhttp2session"></a></h4> 1314<div class="api_metadata"> 1315<span>Added in: v8.4.0</span> 1316</div> 1317<ul> 1318<li>Extends: <a href="http2.html#class-http2session" class="type"><Http2Session></a></li> 1319</ul> 1320<h5><code>serverhttp2session.altsvc(alt, originOrStream)</code><span><a class="mark" href="#serverhttp2sessionaltsvcalt-originorstream" id="serverhttp2sessionaltsvcalt-originorstream">#</a></span><a aria-hidden="true" class="legacy" id="http2_serverhttp2session_altsvc_alt_originorstream"></a></h5> 1321<div class="api_metadata"> 1322<span>Added in: v9.4.0</span> 1323</div> 1324<ul> 1325<li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A description of the alternative service configuration as 1326defined by <a href="https://tools.ietf.org/html/rfc7838">RFC 7838</a>.</li> 1327<li><code>originOrStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Either a URL string specifying 1328the origin (or an <code>Object</code> with an <code>origin</code> property) or the numeric 1329identifier of an active <code>Http2Stream</code> as given by the <code>http2stream.id</code> 1330property.</li> 1331</ul> 1332<p>Submits an <code>ALTSVC</code> frame (as defined by <a href="https://tools.ietf.org/html/rfc7838">RFC 7838</a>) to the connected client.</p> 1333<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 1334 1335<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 1336server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> { 1337 <span class="hljs-comment">// Set altsvc for origin https://example.org:80</span> 1338 session.<span class="hljs-title function_">altsvc</span>(<span class="hljs-string">'h2=":8000"'</span>, <span class="hljs-string">'https://example.org:80'</span>); 1339}); 1340 1341server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 1342 <span class="hljs-comment">// Set altsvc for a specific stream</span> 1343 stream.<span class="hljs-property">session</span>.<span class="hljs-title function_">altsvc</span>(<span class="hljs-string">'h2=":8000"'</span>, stream.<span class="hljs-property">id</span>); 1344});</code> <button class="copy-button">copy</button></pre> 1345<p>Sending an <code>ALTSVC</code> frame with a specific stream ID indicates that the alternate 1346service is associated with the origin of the given <code>Http2Stream</code>.</p> 1347<p>The <code>alt</code> and origin string <em>must</em> contain only ASCII bytes and are 1348strictly interpreted as a sequence of ASCII bytes. The special value <code>'clear'</code> 1349may be passed to clear any previously set alternative service for a given 1350domain.</p> 1351<p>When a string is passed for the <code>originOrStream</code> argument, it will be parsed as 1352a URL and the origin will be derived. For instance, the origin for the 1353HTTP URL <code>'https://example.org/foo/bar'</code> is the ASCII string 1354<code>'https://example.org'</code>. An error will be thrown if either the given string 1355cannot be parsed as a URL or if a valid origin cannot be derived.</p> 1356<p>A <code>URL</code> object, or any object with an <code>origin</code> property, may be passed as 1357<code>originOrStream</code>, in which case the value of the <code>origin</code> property will be 1358used. The value of the <code>origin</code> property <em>must</em> be a properly serialized 1359ASCII origin.</p> 1360<h5>Specifying alternative services<span><a class="mark" href="#specifying-alternative-services" id="specifying-alternative-services">#</a></span><a aria-hidden="true" class="legacy" id="http2_specifying_alternative_services"></a></h5> 1361<p>The format of the <code>alt</code> parameter is strictly defined by <a href="https://tools.ietf.org/html/rfc7838">RFC 7838</a> as an 1362ASCII string containing a comma-delimited list of "alternative" protocols 1363associated with a specific host and port.</p> 1364<p>For example, the value <code>'h2="example.org:81"'</code> indicates that the HTTP/2 1365protocol is available on the host <code>'example.org'</code> on TCP/IP port 81. The 1366host and port <em>must</em> be contained within the quote (<code>"</code>) characters.</p> 1367<p>Multiple alternatives may be specified, for instance: <code>'h2="example.org:81", h2=":82"'</code>.</p> 1368<p>The protocol identifier (<code>'h2'</code> in the examples) may be any valid 1369<a href="https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids">ALPN Protocol ID</a>.</p> 1370<p>The syntax of these values is not validated by the Node.js implementation and 1371are passed through as provided by the user or received from the peer.</p> 1372<h5><code>serverhttp2session.origin(...origins)</code><span><a class="mark" href="#serverhttp2sessionoriginorigins" id="serverhttp2sessionoriginorigins">#</a></span><a aria-hidden="true" class="legacy" id="http2_serverhttp2session_origin_origins"></a></h5> 1373<div class="api_metadata"> 1374<span>Added in: v10.12.0</span> 1375</div> 1376<ul> 1377<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> One or more URL Strings passed as 1378separate arguments.</li> 1379</ul> 1380<p>Submits an <code>ORIGIN</code> frame (as defined by <a href="https://tools.ietf.org/html/rfc8336">RFC 8336</a>) to the connected client 1381to advertise the set of origins for which the server is capable of providing 1382authoritative responses.</p> 1383<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 1384<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptionsSomehow</span>(); 1385<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options); 1386server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 1387 stream.<span class="hljs-title function_">respond</span>(); 1388 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 1389}); 1390server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> { 1391 session.<span class="hljs-title function_">origin</span>(<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>); 1392});</code> <button class="copy-button">copy</button></pre> 1393<p>When a string is passed as an <code>origin</code>, it will be parsed as a URL and the 1394origin will be derived. For instance, the origin for the HTTP URL 1395<code>'https://example.org/foo/bar'</code> is the ASCII string 1396<code>'https://example.org'</code>. An error will be thrown if either the given string 1397cannot be parsed as a URL or if a valid origin cannot be derived.</p> 1398<p>A <code>URL</code> object, or any object with an <code>origin</code> property, may be passed as 1399an <code>origin</code>, in which case the value of the <code>origin</code> property will be 1400used. The value of the <code>origin</code> property <em>must</em> be a properly serialized 1401ASCII origin.</p> 1402<p>Alternatively, the <code>origins</code> option may be used when creating a new HTTP/2 1403server using the <code>http2.createSecureServer()</code> method:</p> 1404<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 1405<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptionsSomehow</span>(); 1406options.<span class="hljs-property">origins</span> = [<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>]; 1407<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options); 1408server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 1409 stream.<span class="hljs-title function_">respond</span>(); 1410 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 1411});</code> <button class="copy-button">copy</button></pre> 1412<h4>Class: <code>ClientHttp2Session</code><span><a class="mark" href="#class-clienthttp2session" id="class-clienthttp2session">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_clienthttp2session"></a></h4> 1413<div class="api_metadata"> 1414<span>Added in: v8.4.0</span> 1415</div> 1416<ul> 1417<li>Extends: <a href="http2.html#class-http2session" class="type"><Http2Session></a></li> 1418</ul> 1419<h5>Event: <code>'altsvc'</code><span><a class="mark" href="#event-altsvc" id="event-altsvc">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_altsvc"></a></h5> 1420<div class="api_metadata"> 1421<span>Added in: v9.4.0</span> 1422</div> 1423<ul> 1424<li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1425<li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1426<li><code>streamId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1427</ul> 1428<p>The <code>'altsvc'</code> event is emitted whenever an <code>ALTSVC</code> frame is received by 1429the client. The event is emitted with the <code>ALTSVC</code> value, origin, and stream 1430ID. If no <code>origin</code> is provided in the <code>ALTSVC</code> frame, <code>origin</code> will 1431be an empty string.</p> 1432<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 1433<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://example.org'</span>); 1434 1435client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'altsvc'</span>, <span class="hljs-function">(<span class="hljs-params">alt, origin, streamId</span>) =></span> { 1436 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(alt); 1437 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(origin); 1438 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(streamId); 1439});</code> <button class="copy-button">copy</button></pre> 1440<h5>Event: <code>'origin'</code><span><a class="mark" href="#event-origin" id="event-origin">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_origin"></a></h5> 1441<div class="api_metadata"> 1442<span>Added in: v10.12.0</span> 1443</div> 1444<ul> 1445<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 1446</ul> 1447<p>The <code>'origin'</code> event is emitted whenever an <code>ORIGIN</code> frame is received by 1448the client. The event is emitted with an array of <code>origin</code> strings. The 1449<code>http2session.originSet</code> will be updated to include the received 1450origins.</p> 1451<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 1452<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://example.org'</span>); 1453 1454client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'origin'</span>, <span class="hljs-function">(<span class="hljs-params">origins</span>) =></span> { 1455 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n < origins.<span class="hljs-property">length</span>; n++) 1456 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(origins[n]); 1457});</code> <button class="copy-button">copy</button></pre> 1458<p>The <code>'origin'</code> event is only emitted when using a secure TLS connection.</p> 1459<h5><code>clienthttp2session.request(headers[, options])</code><span><a class="mark" href="#clienthttp2sessionrequestheaders-options" id="clienthttp2sessionrequestheaders-options">#</a></span><a aria-hidden="true" class="legacy" id="http2_clienthttp2session_request_headers_options"></a></h5> 1460<div class="api_metadata"> 1461<span>Added in: v8.4.0</span> 1462</div> 1463<ul> 1464<li> 1465<p><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></p> 1466</li> 1467<li> 1468<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 1469<ul> 1470<li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the <code>Http2Stream</code> <em>writable</em> side should 1471be closed initially, such as when sending a <code>GET</code> request that should not 1472expect a payload body.</li> 1473<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code> and <code>parent</code> identifies a parent Stream, 1474the created stream is made the sole direct dependency of the parent, with 1475all other existing dependents made a dependent of the newly created stream. 1476<strong>Default:</strong> <code>false</code>.</li> 1477<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the numeric identifier of a stream the newly 1478created stream is dependent on.</li> 1479<li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the relative dependency of a stream in relation 1480to other streams with the same <code>parent</code>. The value is a number between <code>1</code> 1481and <code>256</code> (inclusive).</li> 1482<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the 1483<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li> 1484<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An AbortSignal that may be used to abort an ongoing 1485request.</li> 1486</ul> 1487</li> 1488<li> 1489<p>Returns: <a href="http2.html#class-clienthttp2stream" class="type"><ClientHttp2Stream></a></p> 1490</li> 1491</ul> 1492<p>For HTTP/2 Client <code>Http2Session</code> instances only, the <code>http2session.request()</code> 1493creates and returns an <code>Http2Stream</code> instance that can be used to send an 1494HTTP/2 request to the connected server.</p> 1495<p>When a <code>ClientHttp2Session</code> is first created, the socket may not yet be 1496connected. if <code>clienthttp2session.request()</code> is called during this time, the 1497actual request will be deferred until the socket is ready to go. 1498If the <code>session</code> is closed before the actual request be executed, an 1499<code>ERR_HTTP2_GOAWAY_SESSION</code> is thrown.</p> 1500<p>This method is only available if <code>http2session.type</code> is equal to 1501<code>http2.constants.NGHTTP2_SESSION_CLIENT</code>.</p> 1502<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 1503<span class="hljs-keyword">const</span> clientSession = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:1234'</span>); 1504<span class="hljs-keyword">const</span> { 1505 <span class="hljs-title class_">HTTP2</span>_HEADER_PATH, 1506 <span class="hljs-title class_">HTTP2</span>_HEADER_STATUS, 1507} = http2.<span class="hljs-property">constants</span>; 1508 1509<span class="hljs-keyword">const</span> req = clientSession.<span class="hljs-title function_">request</span>({ [<span class="hljs-title class_">HTTP2</span>_HEADER_PATH]: <span class="hljs-string">'/'</span> }); 1510req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers</span>) =></span> { 1511 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]); 1512 req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> { <span class="hljs-comment">/* .. */</span> }); 1513 req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { <span class="hljs-comment">/* .. */</span> }); 1514});</code> <button class="copy-button">copy</button></pre> 1515<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event 1516is emitted immediately after queuing the last chunk of payload data to be sent. 1517The <code>http2stream.sendTrailers()</code> method can then be called to send trailing 1518headers to the peer.</p> 1519<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically 1520close when the final <code>DATA</code> frame is transmitted. User code must call either 1521<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the 1522<code>Http2Stream</code>.</p> 1523<p>When <code>options.signal</code> is set with an <code>AbortSignal</code> and then <code>abort</code> on the 1524corresponding <code>AbortController</code> is called, the request will emit an <code>'error'</code> 1525event with an <code>AbortError</code> error.</p> 1526<p>The <code>:method</code> and <code>:path</code> pseudo-headers are not specified within <code>headers</code>, 1527they respectively default to:</p> 1528<ul> 1529<li><code>:method</code> = <code>'GET'</code></li> 1530<li><code>:path</code> = <code>/</code></li> 1531</ul> 1532<h4>Class: <code>Http2Stream</code><span><a class="mark" href="#class-http2stream" id="class-http2stream">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2stream"></a></h4> 1533<div class="api_metadata"> 1534<span>Added in: v8.4.0</span> 1535</div> 1536<ul> 1537<li>Extends: <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 1538</ul> 1539<p>Each instance of the <code>Http2Stream</code> class represents a bidirectional HTTP/2 1540communications stream over an <code>Http2Session</code> instance. Any single <code>Http2Session</code> 1541may have up to 2<sup>31</sup>-1 <code>Http2Stream</code> instances over its lifetime.</p> 1542<p>User code will not construct <code>Http2Stream</code> instances directly. Rather, these 1543are created, managed, and provided to user code through the <code>Http2Session</code> 1544instance. On the server, <code>Http2Stream</code> instances are created either in response 1545to an incoming HTTP request (and handed off to user code via the <code>'stream'</code> 1546event), or in response to a call to the <code>http2stream.pushStream()</code> method. 1547On the client, <code>Http2Stream</code> instances are created and returned when either the 1548<code>http2session.request()</code> method is called, or in response to an incoming 1549<code>'push'</code> event.</p> 1550<p>The <code>Http2Stream</code> class is a base for the <a href="#class-serverhttp2stream"><code>ServerHttp2Stream</code></a> and 1551<a href="#class-clienthttp2stream"><code>ClientHttp2Stream</code></a> classes, each of which is used specifically by either 1552the Server or Client side, respectively.</p> 1553<p>All <code>Http2Stream</code> instances are <a href="stream.html#class-streamduplex"><code>Duplex</code></a> streams. The <code>Writable</code> side of the 1554<code>Duplex</code> is used to send data to the connected peer, while the <code>Readable</code> side 1555is used to receive data sent by the connected peer.</p> 1556<p>The default text character encoding for an <code>Http2Stream</code> is UTF-8. When using an 1557<code>Http2Stream</code> to send text, use the <code>'content-type'</code> header to set the character 1558encoding.</p> 1559<pre><code class="language-js">stream.<span class="hljs-title function_">respond</span>({ 1560 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>, 1561 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>, 1562});</code> <button class="copy-button">copy</button></pre> 1563<h5><code>Http2Stream</code> Lifecycle<span><a class="mark" href="#http2stream-lifecycle" id="http2stream-lifecycle">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_lifecycle"></a></h5> 1564<h6>Creation<span><a class="mark" href="#creation" id="creation">#</a></span><a aria-hidden="true" class="legacy" id="http2_creation"></a></h6> 1565<p>On the server side, instances of <a href="#class-serverhttp2stream"><code>ServerHttp2Stream</code></a> are created either 1566when:</p> 1567<ul> 1568<li>A new HTTP/2 <code>HEADERS</code> frame with a previously unused stream ID is received;</li> 1569<li>The <code>http2stream.pushStream()</code> method is called.</li> 1570</ul> 1571<p>On the client side, instances of <a href="#class-clienthttp2stream"><code>ClientHttp2Stream</code></a> are created when the 1572<code>http2session.request()</code> method is called.</p> 1573<p>On the client, the <code>Http2Stream</code> instance returned by <code>http2session.request()</code> 1574may not be immediately ready for use if the parent <code>Http2Session</code> has not yet 1575been fully established. In such cases, operations called on the <code>Http2Stream</code> 1576will be buffered until the <code>'ready'</code> event is emitted. User code should rarely, 1577if ever, need to handle the <code>'ready'</code> event directly. The ready status of an 1578<code>Http2Stream</code> can be determined by checking the value of <code>http2stream.id</code>. If 1579the value is <code>undefined</code>, the stream is not yet ready for use.</p> 1580<h6>Destruction<span><a class="mark" href="#destruction" id="destruction">#</a></span><a aria-hidden="true" class="legacy" id="http2_destruction"></a></h6> 1581<p>All <a href="#class-http2stream"><code>Http2Stream</code></a> instances are destroyed either when:</p> 1582<ul> 1583<li>An <code>RST_STREAM</code> frame for the stream is received by the connected peer, 1584and (for client streams only) pending data has been read.</li> 1585<li>The <code>http2stream.close()</code> method is called, and (for client streams only) 1586pending data has been read.</li> 1587<li>The <code>http2stream.destroy()</code> or <code>http2session.destroy()</code> methods are called.</li> 1588</ul> 1589<p>When an <code>Http2Stream</code> instance is destroyed, an attempt will be made to send an 1590<code>RST_STREAM</code> frame to the connected peer.</p> 1591<p>When the <code>Http2Stream</code> instance is destroyed, the <code>'close'</code> event will 1592be emitted. Because <code>Http2Stream</code> is an instance of <code>stream.Duplex</code>, the 1593<code>'end'</code> event will also be emitted if the stream data is currently flowing. 1594The <code>'error'</code> event may also be emitted if <code>http2stream.destroy()</code> was called 1595with an <code>Error</code> passed as the first argument.</p> 1596<p>After the <code>Http2Stream</code> has been destroyed, the <code>http2stream.destroyed</code> 1597property will be <code>true</code> and the <code>http2stream.rstCode</code> property will specify the 1598<code>RST_STREAM</code> error code. The <code>Http2Stream</code> instance is no longer usable once 1599destroyed.</p> 1600<h5>Event: <code>'aborted'</code><span><a class="mark" href="#event-aborted" id="event-aborted">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_aborted"></a></h5> 1601<div class="api_metadata"> 1602<span>Added in: v8.4.0</span> 1603</div> 1604<p>The <code>'aborted'</code> event is emitted whenever a <code>Http2Stream</code> instance is 1605abnormally aborted in mid-communication. 1606Its listener does not expect any arguments.</p> 1607<p>The <code>'aborted'</code> event will only be emitted if the <code>Http2Stream</code> writable side 1608has not been ended.</p> 1609<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_1" id="event-close_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_close_1"></a></h5> 1610<div class="api_metadata"> 1611<span>Added in: v8.4.0</span> 1612</div> 1613<p>The <code>'close'</code> event is emitted when the <code>Http2Stream</code> is destroyed. Once 1614this event is emitted, the <code>Http2Stream</code> instance is no longer usable.</p> 1615<p>The HTTP/2 error code used when closing the stream can be retrieved using 1616the <code>http2stream.rstCode</code> property. If the code is any value other than 1617<code>NGHTTP2_NO_ERROR</code> (<code>0</code>), an <code>'error'</code> event will have also been emitted.</p> 1618<h5>Event: <code>'error'</code><span><a class="mark" href="#event-error_1" id="event-error_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_error_1"></a></h5> 1619<div class="api_metadata"> 1620<span>Added in: v8.4.0</span> 1621</div> 1622<ul> 1623<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 1624</ul> 1625<p>The <code>'error'</code> event is emitted when an error occurs during the processing of 1626an <code>Http2Stream</code>.</p> 1627<h5>Event: <code>'frameError'</code><span><a class="mark" href="#event-frameerror_1" id="event-frameerror_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_frameerror_1"></a></h5> 1628<div class="api_metadata"> 1629<span>Added in: v8.4.0</span> 1630</div> 1631<ul> 1632<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The frame type.</li> 1633<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The error code.</li> 1634<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The stream id (or <code>0</code> if the frame isn't associated with a 1635stream).</li> 1636</ul> 1637<p>The <code>'frameError'</code> event is emitted when an error occurs while attempting to 1638send a frame. When invoked, the handler function will receive an integer 1639argument identifying the frame type, and an integer argument identifying the 1640error code. The <code>Http2Stream</code> instance will be destroyed immediately after the 1641<code>'frameError'</code> event is emitted.</p> 1642<h5>Event: <code>'ready'</code><span><a class="mark" href="#event-ready" id="event-ready">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_ready"></a></h5> 1643<div class="api_metadata"> 1644<span>Added in: v8.4.0</span> 1645</div> 1646<p>The <code>'ready'</code> event is emitted when the <code>Http2Stream</code> has been opened, has 1647been assigned an <code>id</code>, and can be used. The listener does not expect any 1648arguments.</p> 1649<h5>Event: <code>'timeout'</code><span><a class="mark" href="#event-timeout_1" id="event-timeout_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_timeout_1"></a></h5> 1650<div class="api_metadata"> 1651<span>Added in: v8.4.0</span> 1652</div> 1653<p>The <code>'timeout'</code> event is emitted after no activity is received for this 1654<code>Http2Stream</code> within the number of milliseconds set using 1655<code>http2stream.setTimeout()</code>. 1656Its listener does not expect any arguments.</p> 1657<h5>Event: <code>'trailers'</code><span><a class="mark" href="#event-trailers" id="event-trailers">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_trailers"></a></h5> 1658<div class="api_metadata"> 1659<span>Added in: v8.4.0</span> 1660</div> 1661<ul> 1662<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li> 1663<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The associated numeric flags</li> 1664</ul> 1665<p>The <code>'trailers'</code> event is emitted when a block of headers associated with 1666trailing header fields is received. The listener callback is passed the 1667<a href="#headers-object">HTTP/2 Headers Object</a> and flags associated with the headers.</p> 1668<p>This event might not be emitted if <code>http2stream.end()</code> is called 1669before trailers are received and the incoming data is not being read or 1670listened for.</p> 1671<pre><code class="language-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'trailers'</span>, <span class="hljs-function">(<span class="hljs-params">headers, flags</span>) =></span> { 1672 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers); 1673});</code> <button class="copy-button">copy</button></pre> 1674<h5>Event: <code>'wantTrailers'</code><span><a class="mark" href="#event-wanttrailers" id="event-wanttrailers">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_wanttrailers"></a></h5> 1675<div class="api_metadata"> 1676<span>Added in: v10.0.0</span> 1677</div> 1678<p>The <code>'wantTrailers'</code> event is emitted when the <code>Http2Stream</code> has queued the 1679final <code>DATA</code> frame to be sent on a frame and the <code>Http2Stream</code> is ready to send 1680trailing headers. When initiating a request or response, the <code>waitForTrailers</code> 1681option must be set for this event to be emitted.</p> 1682<h5><code>http2stream.aborted</code><span><a class="mark" href="#http2streamaborted" id="http2streamaborted">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_aborted"></a></h5> 1683<div class="api_metadata"> 1684<span>Added in: v8.4.0</span> 1685</div> 1686<ul> 1687<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1688</ul> 1689<p>Set to <code>true</code> if the <code>Http2Stream</code> instance was aborted abnormally. When set, 1690the <code>'aborted'</code> event will have been emitted.</p> 1691<h5><code>http2stream.bufferSize</code><span><a class="mark" href="#http2streambuffersize" id="http2streambuffersize">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_buffersize"></a></h5> 1692<div class="api_metadata"> 1693<span>Added in: v11.2.0, v10.16.0</span> 1694</div> 1695<ul> 1696<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1697</ul> 1698<p>This property shows the number of characters currently buffered to be written. 1699See <a href="net.html#socketbuffersize"><code>net.Socket.bufferSize</code></a> for details.</p> 1700<h5><code>http2stream.close(code[, callback])</code><span><a class="mark" href="#http2streamclosecode-callback" id="http2streamclosecode-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_close_code_callback"></a></h5> 1701<div class="api_metadata"> 1702<details class="changelog"><summary>History</summary> 1703<table> 1704<tbody><tr><th>Version</th><th>Changes</th></tr> 1705<tr><td>v18.0.0</td> 1706<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 1707<tr><td>v8.4.0</td> 1708<td><p><span>Added in: v8.4.0</span></p></td></tr> 1709</tbody></table> 1710</details> 1711</div> 1712<ul> 1713<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Unsigned 32-bit integer identifying the error code. 1714<strong>Default:</strong> <code>http2.constants.NGHTTP2_NO_ERROR</code> (<code>0x00</code>).</li> 1715<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> An optional function registered to listen for the 1716<code>'close'</code> event.</li> 1717</ul> 1718<p>Closes the <code>Http2Stream</code> instance by sending an <code>RST_STREAM</code> frame to the 1719connected HTTP/2 peer.</p> 1720<h5><code>http2stream.closed</code><span><a class="mark" href="#http2streamclosed" id="http2streamclosed">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_closed"></a></h5> 1721<div class="api_metadata"> 1722<span>Added in: v9.4.0</span> 1723</div> 1724<ul> 1725<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1726</ul> 1727<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has been closed.</p> 1728<h5><code>http2stream.destroyed</code><span><a class="mark" href="#http2streamdestroyed" id="http2streamdestroyed">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_destroyed"></a></h5> 1729<div class="api_metadata"> 1730<span>Added in: v8.4.0</span> 1731</div> 1732<ul> 1733<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1734</ul> 1735<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has been destroyed and is no longer 1736usable.</p> 1737<h5><code>http2stream.endAfterHeaders</code><span><a class="mark" href="#http2streamendafterheaders" id="http2streamendafterheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_endafterheaders"></a></h5> 1738<div class="api_metadata"> 1739<span>Added in: v10.11.0</span> 1740</div> 1741<ul> 1742<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1743</ul> 1744<p>Set to <code>true</code> if the <code>END_STREAM</code> flag was set in the request or response 1745HEADERS frame received, indicating that no additional data should be received 1746and the readable side of the <code>Http2Stream</code> will be closed.</p> 1747<h5><code>http2stream.id</code><span><a class="mark" href="#http2streamid" id="http2streamid">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_id"></a></h5> 1748<div class="api_metadata"> 1749<span>Added in: v8.4.0</span> 1750</div> 1751<ul> 1752<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 1753</ul> 1754<p>The numeric stream identifier of this <code>Http2Stream</code> instance. Set to <code>undefined</code> 1755if the stream identifier has not yet been assigned.</p> 1756<h5><code>http2stream.pending</code><span><a class="mark" href="#http2streampending" id="http2streampending">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_pending"></a></h5> 1757<div class="api_metadata"> 1758<span>Added in: v9.4.0</span> 1759</div> 1760<ul> 1761<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1762</ul> 1763<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has not yet been assigned a 1764numeric stream identifier.</p> 1765<h5><code>http2stream.priority(options)</code><span><a class="mark" href="#http2streampriorityoptions" id="http2streampriorityoptions">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_priority_options"></a></h5> 1766<div class="api_metadata"> 1767<span>Added in: v8.4.0</span> 1768</div> 1769<ul> 1770<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1771<ul> 1772<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code> and <code>parent</code> identifies a parent Stream, 1773this stream is made the sole direct dependency of the parent, with 1774all other existing dependents made a dependent of this stream. <strong>Default:</strong> 1775<code>false</code>.</li> 1776<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the numeric identifier of a stream this stream 1777is dependent on.</li> 1778<li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the relative dependency of a stream in relation 1779to other streams with the same <code>parent</code>. The value is a number between <code>1</code> 1780and <code>256</code> (inclusive).</li> 1781<li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, changes the priority locally without 1782sending a <code>PRIORITY</code> frame to the connected peer.</li> 1783</ul> 1784</li> 1785</ul> 1786<p>Updates the priority for this <code>Http2Stream</code> instance.</p> 1787<h5><code>http2stream.rstCode</code><span><a class="mark" href="#http2streamrstcode" id="http2streamrstcode">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_rstcode"></a></h5> 1788<div class="api_metadata"> 1789<span>Added in: v8.4.0</span> 1790</div> 1791<ul> 1792<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1793</ul> 1794<p>Set to the <code>RST_STREAM</code> <a href="#error-codes-for-rst_stream-and-goaway">error code</a> reported when the <code>Http2Stream</code> is 1795destroyed after either receiving an <code>RST_STREAM</code> frame from the connected peer, 1796calling <code>http2stream.close()</code>, or <code>http2stream.destroy()</code>. Will be 1797<code>undefined</code> if the <code>Http2Stream</code> has not been closed.</p> 1798<h5><code>http2stream.sentHeaders</code><span><a class="mark" href="#http2streamsentheaders" id="http2streamsentheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_sentheaders"></a></h5> 1799<div class="api_metadata"> 1800<span>Added in: v9.5.0</span> 1801</div> 1802<ul> 1803<li><a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 1804</ul> 1805<p>An object containing the outbound headers sent for this <code>Http2Stream</code>.</p> 1806<h5><code>http2stream.sentInfoHeaders</code><span><a class="mark" href="#http2streamsentinfoheaders" id="http2streamsentinfoheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_sentinfoheaders"></a></h5> 1807<div class="api_metadata"> 1808<span>Added in: v9.5.0</span> 1809</div> 1810<ul> 1811<li><a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object[]></a></li> 1812</ul> 1813<p>An array of objects containing the outbound informational (additional) headers 1814sent for this <code>Http2Stream</code>.</p> 1815<h5><code>http2stream.sentTrailers</code><span><a class="mark" href="#http2streamsenttrailers" id="http2streamsenttrailers">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_senttrailers"></a></h5> 1816<div class="api_metadata"> 1817<span>Added in: v9.5.0</span> 1818</div> 1819<ul> 1820<li><a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 1821</ul> 1822<p>An object containing the outbound trailers sent for this <code>HttpStream</code>.</p> 1823<h5><code>http2stream.session</code><span><a class="mark" href="#http2streamsession" id="http2streamsession">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_session"></a></h5> 1824<div class="api_metadata"> 1825<span>Added in: v8.4.0</span> 1826</div> 1827<ul> 1828<li><a href="http2.html#class-http2session" class="type"><Http2Session></a></li> 1829</ul> 1830<p>A reference to the <code>Http2Session</code> instance that owns this <code>Http2Stream</code>. The 1831value will be <code>undefined</code> after the <code>Http2Stream</code> instance is destroyed.</p> 1832<h5><code>http2stream.setTimeout(msecs, callback)</code><span><a class="mark" href="#http2streamsettimeoutmsecs-callback" id="http2streamsettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_settimeout_msecs_callback"></a></h5> 1833<div class="api_metadata"> 1834<details class="changelog"><summary>History</summary> 1835<table> 1836<tbody><tr><th>Version</th><th>Changes</th></tr> 1837<tr><td>v18.0.0</td> 1838<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 1839<tr><td>v8.4.0</td> 1840<td><p><span>Added in: v8.4.0</span></p></td></tr> 1841</tbody></table> 1842</details> 1843</div> 1844<ul> 1845<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1846<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1847</ul> 1848<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 1849<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://example.org:8000'</span>); 1850<span class="hljs-keyword">const</span> { <span class="hljs-title class_">NGHTTP2</span>_CANCEL } = http2.<span class="hljs-property">constants</span>; 1851<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> }); 1852 1853<span class="hljs-comment">// Cancel the stream if there's no activity after 5 seconds</span> 1854req.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">5000</span>, <span class="hljs-function">() =></span> req.<span class="hljs-title function_">close</span>(<span class="hljs-title class_">NGHTTP2</span>_CANCEL));</code> <button class="copy-button">copy</button></pre> 1855<h5><code>http2stream.state</code><span><a class="mark" href="#http2streamstate" id="http2streamstate">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_state"></a></h5> 1856<div class="api_metadata"> 1857<span>Added in: v8.4.0</span> 1858</div> 1859<p>Provides miscellaneous information about the current state of the 1860<code>Http2Stream</code>.</p> 1861<ul> 1862<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1863<ul> 1864<li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes the connected peer may send 1865for this <code>Http2Stream</code> without receiving a <code>WINDOW_UPDATE</code>.</li> 1866<li><code>state</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A flag indicating the low-level current state of the 1867<code>Http2Stream</code> as determined by <code>nghttp2</code>.</li> 1868<li><code>localClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <code>1</code> if this <code>Http2Stream</code> has been closed locally.</li> 1869<li><code>remoteClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <code>1</code> if this <code>Http2Stream</code> has been closed 1870remotely.</li> 1871<li><code>sumDependencyWeight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The sum weight of all <code>Http2Stream</code> 1872instances that depend on this <code>Http2Stream</code> as specified using 1873<code>PRIORITY</code> frames.</li> 1874<li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The priority weight of this <code>Http2Stream</code>.</li> 1875</ul> 1876</li> 1877</ul> 1878<p>A current state of this <code>Http2Stream</code>.</p> 1879<h5><code>http2stream.sendTrailers(headers)</code><span><a class="mark" href="#http2streamsendtrailersheaders" id="http2streamsendtrailersheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_sendtrailers_headers"></a></h5> 1880<div class="api_metadata"> 1881<span>Added in: v10.0.0</span> 1882</div> 1883<ul> 1884<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 1885</ul> 1886<p>Sends a trailing <code>HEADERS</code> frame to the connected HTTP/2 peer. This method 1887will cause the <code>Http2Stream</code> to be immediately closed and must only be 1888called after the <code>'wantTrailers'</code> event has been emitted. When sending a 1889request or sending a response, the <code>options.waitForTrailers</code> option must be set 1890in order to keep the <code>Http2Stream</code> open after the final <code>DATA</code> frame so that 1891trailers can be sent.</p> 1892<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 1893<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 1894server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 1895 stream.<span class="hljs-title function_">respond</span>(<span class="hljs-literal">undefined</span>, { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> }); 1896 stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> { 1897 stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">xyz</span>: <span class="hljs-string">'abc'</span> }); 1898 }); 1899 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello World'</span>); 1900});</code> <button class="copy-button">copy</button></pre> 1901<p>The HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header 1902fields (e.g. <code>':method'</code>, <code>':path'</code>, etc).</p> 1903<h4>Class: <code>ClientHttp2Stream</code><span><a class="mark" href="#class-clienthttp2stream" id="class-clienthttp2stream">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_clienthttp2stream"></a></h4> 1904<div class="api_metadata"> 1905<span>Added in: v8.4.0</span> 1906</div> 1907<ul> 1908<li>Extends <a href="http2.html#class-http2stream" class="type"><Http2Stream></a></li> 1909</ul> 1910<p>The <code>ClientHttp2Stream</code> class is an extension of <code>Http2Stream</code> that is 1911used exclusively on HTTP/2 Clients. <code>Http2Stream</code> instances on the client 1912provide events such as <code>'response'</code> and <code>'push'</code> that are only relevant on 1913the client.</p> 1914<h5>Event: <code>'continue'</code><span><a class="mark" href="#event-continue" id="event-continue">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_continue"></a></h5> 1915<div class="api_metadata"> 1916<span>Added in: v8.5.0</span> 1917</div> 1918<p>Emitted when the server sends a <code>100 Continue</code> status, usually because 1919the request contained <code>Expect: 100-continue</code>. This is an instruction that 1920the client should send the request body.</p> 1921<h5>Event: <code>'headers'</code><span><a class="mark" href="#event-headers" id="event-headers">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_headers"></a></h5> 1922<div class="api_metadata"> 1923<span>Added in: v8.4.0</span> 1924</div> 1925<ul> 1926<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 1927<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1928</ul> 1929<p>The <code>'headers'</code> event is emitted when an additional block of headers is received 1930for a stream, such as when a block of <code>1xx</code> informational headers is received. 1931The listener callback is passed the <a href="#headers-object">HTTP/2 Headers Object</a> and flags 1932associated with the headers.</p> 1933<pre><code class="language-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'headers'</span>, <span class="hljs-function">(<span class="hljs-params">headers, flags</span>) =></span> { 1934 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers); 1935});</code> <button class="copy-button">copy</button></pre> 1936<h5>Event: <code>'push'</code><span><a class="mark" href="#event-push" id="event-push">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_push"></a></h5> 1937<div class="api_metadata"> 1938<span>Added in: v8.4.0</span> 1939</div> 1940<ul> 1941<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 1942<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1943</ul> 1944<p>The <code>'push'</code> event is emitted when response headers for a Server Push stream 1945are received. The listener callback is passed the <a href="#headers-object">HTTP/2 Headers Object</a> and 1946flags associated with the headers.</p> 1947<pre><code class="language-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'push'</span>, <span class="hljs-function">(<span class="hljs-params">headers, flags</span>) =></span> { 1948 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers); 1949});</code> <button class="copy-button">copy</button></pre> 1950<h5>Event: <code>'response'</code><span><a class="mark" href="#event-response" id="event-response">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_response"></a></h5> 1951<div class="api_metadata"> 1952<span>Added in: v8.4.0</span> 1953</div> 1954<ul> 1955<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 1956<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1957</ul> 1958<p>The <code>'response'</code> event is emitted when a response <code>HEADERS</code> frame has been 1959received for this stream from the connected HTTP/2 server. The listener is 1960invoked with two arguments: an <code>Object</code> containing the received 1961<a href="#headers-object">HTTP/2 Headers Object</a>, and flags associated with the headers.</p> 1962<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 1963<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost'</span>); 1964<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> }); 1965req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers, flags</span>) =></span> { 1966 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-string">':status'</span>]); 1967});</code> <button class="copy-button">copy</button></pre> 1968<h4>Class: <code>ServerHttp2Stream</code><span><a class="mark" href="#class-serverhttp2stream" id="class-serverhttp2stream">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_serverhttp2stream"></a></h4> 1969<div class="api_metadata"> 1970<span>Added in: v8.4.0</span> 1971</div> 1972<ul> 1973<li>Extends: <a href="http2.html#class-http2stream" class="type"><Http2Stream></a></li> 1974</ul> 1975<p>The <code>ServerHttp2Stream</code> class is an extension of <a href="#class-http2stream"><code>Http2Stream</code></a> that is 1976used exclusively on HTTP/2 Servers. <code>Http2Stream</code> instances on the server 1977provide additional methods such as <code>http2stream.pushStream()</code> and 1978<code>http2stream.respond()</code> that are only relevant on the server.</p> 1979<h5><code>http2stream.additionalHeaders(headers)</code><span><a class="mark" href="#http2streamadditionalheadersheaders" id="http2streamadditionalheadersheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_additionalheaders_headers"></a></h5> 1980<div class="api_metadata"> 1981<span>Added in: v8.4.0</span> 1982</div> 1983<ul> 1984<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 1985</ul> 1986<p>Sends an additional informational <code>HEADERS</code> frame to the connected HTTP/2 peer.</p> 1987<h5><code>http2stream.headersSent</code><span><a class="mark" href="#http2streamheaderssent" id="http2streamheaderssent">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_headerssent"></a></h5> 1988<div class="api_metadata"> 1989<span>Added in: v8.4.0</span> 1990</div> 1991<ul> 1992<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1993</ul> 1994<p>True if headers were sent, false otherwise (read-only).</p> 1995<h5><code>http2stream.pushAllowed</code><span><a class="mark" href="#http2streampushallowed" id="http2streampushallowed">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_pushallowed"></a></h5> 1996<div class="api_metadata"> 1997<span>Added in: v8.4.0</span> 1998</div> 1999<ul> 2000<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2001</ul> 2002<p>Read-only property mapped to the <code>SETTINGS_ENABLE_PUSH</code> flag of the remote 2003client's most recent <code>SETTINGS</code> frame. Will be <code>true</code> if the remote peer 2004accepts push streams, <code>false</code> otherwise. Settings are the same for every 2005<code>Http2Stream</code> in the same <code>Http2Session</code>.</p> 2006<h5><code>http2stream.pushStream(headers[, options], callback)</code><span><a class="mark" href="#http2streampushstreamheaders-options-callback" id="http2streampushstreamheaders-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_pushstream_headers_options_callback"></a></h5> 2007<div class="api_metadata"> 2008<details class="changelog"><summary>History</summary> 2009<table> 2010<tbody><tr><th>Version</th><th>Changes</th></tr> 2011<tr><td>v18.0.0</td> 2012<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 2013<tr><td>v8.4.0</td> 2014<td><p><span>Added in: v8.4.0</span></p></td></tr> 2015</tbody></table> 2016</details> 2017</div> 2018<ul> 2019<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 2020<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2021<ul> 2022<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code> and <code>parent</code> identifies a parent Stream, 2023the created stream is made the sole direct dependency of the parent, with 2024all other existing dependents made a dependent of the newly created stream. 2025<strong>Default:</strong> <code>false</code>.</li> 2026<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the numeric identifier of a stream the newly 2027created stream is dependent on.</li> 2028</ul> 2029</li> 2030<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Callback that is called once the push stream has been 2031initiated. 2032<ul> 2033<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2034<li><code>pushStream</code> <a href="http2.html#class-serverhttp2stream" class="type"><ServerHttp2Stream></a> The returned <code>pushStream</code> object.</li> 2035<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> Headers object the <code>pushStream</code> was 2036initiated with.</li> 2037</ul> 2038</li> 2039</ul> 2040<p>Initiates a push stream. The callback is invoked with the new <code>Http2Stream</code> 2041instance created for the push stream passed as the second argument, or an 2042<code>Error</code> passed as the first argument.</p> 2043<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2044<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 2045server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 2046 stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> }); 2047 stream.<span class="hljs-title function_">pushStream</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> }, <span class="hljs-function">(<span class="hljs-params">err, pushStream, headers</span>) =></span> { 2048 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2049 pushStream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> }); 2050 pushStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some pushed data'</span>); 2051 }); 2052 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>); 2053});</code> <button class="copy-button">copy</button></pre> 2054<p>Setting the weight of a push stream is not allowed in the <code>HEADERS</code> frame. Pass 2055a <code>weight</code> value to <code>http2stream.priority</code> with the <code>silent</code> option set to 2056<code>true</code> to enable server-side bandwidth balancing between concurrent streams.</p> 2057<p>Calling <code>http2stream.pushStream()</code> from within a pushed stream is not permitted 2058and will throw an error.</p> 2059<h5><code>http2stream.respond([headers[, options]])</code><span><a class="mark" href="#http2streamrespondheaders-options" id="http2streamrespondheaders-options">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_respond_headers_options"></a></h5> 2060<div class="api_metadata"> 2061<details class="changelog"><summary>History</summary> 2062<table> 2063<tbody><tr><th>Version</th><th>Changes</th></tr> 2064<tr><td>v14.5.0, v12.19.0</td> 2065<td><p>Allow explicitly setting date headers.</p></td></tr> 2066<tr><td>v8.4.0</td> 2067<td><p><span>Added in: v8.4.0</span></p></td></tr> 2068</tbody></table> 2069</details> 2070</div> 2071<ul> 2072<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 2073<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2074<ul> 2075<li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Set to <code>true</code> to indicate that the response will not 2076include payload data.</li> 2077<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the 2078<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li> 2079</ul> 2080</li> 2081</ul> 2082<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2083<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 2084server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 2085 stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> }); 2086 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>); 2087});</code> <button class="copy-button">copy</button></pre> 2088<p>Initiates a response. When the <code>options.waitForTrailers</code> option is set, the 2089<code>'wantTrailers'</code> event will be emitted immediately after queuing the last chunk 2090of payload data to be sent. The <code>http2stream.sendTrailers()</code> method can then be 2091used to sent trailing header fields to the peer.</p> 2092<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically 2093close when the final <code>DATA</code> frame is transmitted. User code must call either 2094<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the 2095<code>Http2Stream</code>.</p> 2096<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2097<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 2098server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 2099 stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> }, { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> }); 2100 stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> { 2101 stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> }); 2102 }); 2103 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>); 2104});</code> <button class="copy-button">copy</button></pre> 2105<h5><code>http2stream.respondWithFD(fd[, headers[, options]])</code><span><a class="mark" href="#http2streamrespondwithfdfd-headers-options" id="http2streamrespondwithfdfd-headers-options">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_respondwithfd_fd_headers_options"></a></h5> 2106<div class="api_metadata"> 2107<details class="changelog"><summary>History</summary> 2108<table> 2109<tbody><tr><th>Version</th><th>Changes</th></tr> 2110<tr><td>v14.5.0, v12.19.0</td> 2111<td><p>Allow explicitly setting date headers.</p></td></tr> 2112<tr><td>v12.12.0</td> 2113<td><p>The <code>fd</code> option may now be a <code>FileHandle</code>.</p></td></tr> 2114<tr><td>v10.0.0</td> 2115<td><p>Any readable file descriptor, not necessarily for a regular file, is supported now.</p></td></tr> 2116<tr><td>v8.4.0</td> 2117<td><p><span>Added in: v8.4.0</span></p></td></tr> 2118</tbody></table> 2119</details> 2120</div> 2121<ul> 2122<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> A readable file descriptor.</li> 2123<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 2124<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2125<ul> 2126<li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2127<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the 2128<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li> 2129<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The offset position at which to begin reading.</li> 2130<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The amount of data from the fd to send.</li> 2131</ul> 2132</li> 2133</ul> 2134<p>Initiates a response whose data is read from the given file descriptor. No 2135validation is performed on the given file descriptor. If an error occurs while 2136attempting to read data using the file descriptor, the <code>Http2Stream</code> will be 2137closed using an <code>RST_STREAM</code> frame using the standard <code>INTERNAL_ERROR</code> code.</p> 2138<p>When used, the <code>Http2Stream</code> object's <code>Duplex</code> interface will be closed 2139automatically.</p> 2140<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2141<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 2142 2143<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 2144server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 2145 <span class="hljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="hljs-string">'/some/file'</span>, <span class="hljs-string">'r'</span>); 2146 2147 <span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</span>(fd); 2148 <span class="hljs-keyword">const</span> headers = { 2149 <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>, 2150 <span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(), 2151 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>, 2152 }; 2153 stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers); 2154 stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> fs.<span class="hljs-title function_">closeSync</span>(fd)); 2155});</code> <button class="copy-button">copy</button></pre> 2156<p>The optional <code>options.statCheck</code> function may be specified to give user code 2157an opportunity to set additional content headers based on the <code>fs.Stat</code> details 2158of the given fd. If the <code>statCheck</code> function is provided, the 2159<code>http2stream.respondWithFD()</code> method will perform an <code>fs.fstat()</code> call to 2160collect details on the provided file descriptor.</p> 2161<p>The <code>offset</code> and <code>length</code> options may be used to limit the response to a 2162specific range subset. This can be used, for instance, to support HTTP Range 2163requests.</p> 2164<p>The file descriptor or <code>FileHandle</code> is not closed when the stream is closed, 2165so it will need to be closed manually once it is no longer needed. 2166Using the same file descriptor concurrently for multiple streams 2167is not supported and may result in data loss. Re-using a file descriptor 2168after a stream has finished is supported.</p> 2169<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event 2170will be emitted immediately after queuing the last chunk of payload data to be 2171sent. The <code>http2stream.sendTrailers()</code> method can then be used to sent trailing 2172header fields to the peer.</p> 2173<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically 2174close when the final <code>DATA</code> frame is transmitted. User code <em>must</em> call either 2175<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the 2176<code>Http2Stream</code>.</p> 2177<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2178<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 2179 2180<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 2181server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 2182 <span class="hljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="hljs-string">'/some/file'</span>, <span class="hljs-string">'r'</span>); 2183 2184 <span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</span>(fd); 2185 <span class="hljs-keyword">const</span> headers = { 2186 <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>, 2187 <span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(), 2188 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>, 2189 }; 2190 stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers, { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> }); 2191 stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> { 2192 stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> }); 2193 }); 2194 2195 stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> fs.<span class="hljs-title function_">closeSync</span>(fd)); 2196});</code> <button class="copy-button">copy</button></pre> 2197<h5><code>http2stream.respondWithFile(path[, headers[, options]])</code><span><a class="mark" href="#http2streamrespondwithfilepath-headers-options" id="http2streamrespondwithfilepath-headers-options">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_respondwithfile_path_headers_options"></a></h5> 2198<div class="api_metadata"> 2199<details class="changelog"><summary>History</summary> 2200<table> 2201<tbody><tr><th>Version</th><th>Changes</th></tr> 2202<tr><td>v14.5.0, v12.19.0</td> 2203<td><p>Allow explicitly setting date headers.</p></td></tr> 2204<tr><td>v10.0.0</td> 2205<td><p>Any readable file, not necessarily a regular file, is supported now.</p></td></tr> 2206<tr><td>v8.4.0</td> 2207<td><p><span>Added in: v8.4.0</span></p></td></tr> 2208</tbody></table> 2209</details> 2210</div> 2211<ul> 2212<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2213<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li> 2214<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2215<ul> 2216<li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2217<li><code>onError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Callback function invoked in the case of an 2218error before send.</li> 2219<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the 2220<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li> 2221<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The offset position at which to begin reading.</li> 2222<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The amount of data from the fd to send.</li> 2223</ul> 2224</li> 2225</ul> 2226<p>Sends a regular file as the response. The <code>path</code> must specify a regular file 2227or an <code>'error'</code> event will be emitted on the <code>Http2Stream</code> object.</p> 2228<p>When used, the <code>Http2Stream</code> object's <code>Duplex</code> interface will be closed 2229automatically.</p> 2230<p>The optional <code>options.statCheck</code> function may be specified to give user code 2231an opportunity to set additional content headers based on the <code>fs.Stat</code> details 2232of the given file:</p> 2233<p>If an error occurs while attempting to read the file data, the <code>Http2Stream</code> 2234will be closed using an <code>RST_STREAM</code> frame using the standard <code>INTERNAL_ERROR</code> 2235code. If the <code>onError</code> callback is defined, then it will be called. Otherwise 2236the stream will be destroyed.</p> 2237<p>Example using a file path:</p> 2238<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2239<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 2240server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 2241 <span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<span class="hljs-params">stat, headers</span>) { 2242 headers[<span class="hljs-string">'last-modified'</span>] = stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(); 2243 } 2244 2245 <span class="hljs-keyword">function</span> <span class="hljs-title function_">onError</span>(<span class="hljs-params">err</span>) { 2246 <span class="hljs-comment">// stream.respond() can throw if the stream has been destroyed by</span> 2247 <span class="hljs-comment">// the other side.</span> 2248 <span class="hljs-keyword">try</span> { 2249 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) { 2250 stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">404</span> }); 2251 } <span class="hljs-keyword">else</span> { 2252 stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">500</span> }); 2253 } 2254 } <span class="hljs-keyword">catch</span> (err) { 2255 <span class="hljs-comment">// Perform actual error handling.</span> 2256 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2257 } 2258 stream.<span class="hljs-title function_">end</span>(); 2259 } 2260 2261 stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>, 2262 { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> }, 2263 { statCheck, onError }); 2264});</code> <button class="copy-button">copy</button></pre> 2265<p>The <code>options.statCheck</code> function may also be used to cancel the send operation 2266by returning <code>false</code>. For instance, a conditional request may check the stat 2267results to determine if the file has been modified to return an appropriate 2268<code>304</code> response:</p> 2269<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2270<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 2271server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 2272 <span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<span class="hljs-params">stat, headers</span>) { 2273 <span class="hljs-comment">// Check the stat here...</span> 2274 stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">304</span> }); 2275 <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hljs-comment">// Cancel the send operation</span> 2276 } 2277 stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>, 2278 { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> }, 2279 { statCheck }); 2280});</code> <button class="copy-button">copy</button></pre> 2281<p>The <code>content-length</code> header field will be automatically set.</p> 2282<p>The <code>offset</code> and <code>length</code> options may be used to limit the response to a 2283specific range subset. This can be used, for instance, to support HTTP Range 2284requests.</p> 2285<p>The <code>options.onError</code> function may also be used to handle all the errors 2286that could happen before the delivery of the file is initiated. The 2287default behavior is to destroy the stream.</p> 2288<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event 2289will be emitted immediately after queuing the last chunk of payload data to be 2290sent. The <code>http2stream.sendTrailers()</code> method can then be used to sent trailing 2291header fields to the peer.</p> 2292<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically 2293close when the final <code>DATA</code> frame is transmitted. User code must call either 2294<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the 2295<code>Http2Stream</code>.</p> 2296<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2297<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 2298server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> { 2299 stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>, 2300 { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> }, 2301 { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> }); 2302 stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> { 2303 stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> }); 2304 }); 2305});</code> <button class="copy-button">copy</button></pre> 2306<h4>Class: <code>Http2Server</code><span><a class="mark" href="#class-http2server" id="class-http2server">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2server"></a></h4> 2307<div class="api_metadata"> 2308<span>Added in: v8.4.0</span> 2309</div> 2310<ul> 2311<li>Extends: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 2312</ul> 2313<p>Instances of <code>Http2Server</code> are created using the <code>http2.createServer()</code> 2314function. The <code>Http2Server</code> class is not exported directly by the 2315<code>node:http2</code> module.</p> 2316<h5>Event: <code>'checkContinue'</code><span><a class="mark" href="#event-checkcontinue" id="event-checkcontinue">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_checkcontinue"></a></h5> 2317<div class="api_metadata"> 2318<span>Added in: v8.5.0</span> 2319</div> 2320<ul> 2321<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li> 2322<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li> 2323</ul> 2324<p>If a <a href="#event-request"><code>'request'</code></a> listener is registered or <a href="#http2createserveroptions-onrequesthandler"><code>http2.createServer()</code></a> is 2325supplied a callback function, the <code>'checkContinue'</code> event is emitted each time 2326a request with an HTTP <code>Expect: 100-continue</code> is received. If this event is 2327not listened for, the server will automatically respond with a status 2328<code>100 Continue</code> as appropriate.</p> 2329<p>Handling this event involves calling <a href="#responsewritecontinue"><code>response.writeContinue()</code></a> if the 2330client should continue to send the request body, or generating an appropriate 2331HTTP response (e.g. 400 Bad Request) if the client should not continue to send 2332the request body.</p> 2333<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will 2334not be emitted.</p> 2335<h5>Event: <code>'connection'</code><span><a class="mark" href="#event-connection" id="event-connection">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_connection"></a></h5> 2336<div class="api_metadata"> 2337<span>Added in: v8.4.0</span> 2338</div> 2339<ul> 2340<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 2341</ul> 2342<p>This event is emitted when a new TCP stream is established. <code>socket</code> is 2343typically an object of type <a href="net.html#class-netsocket"><code>net.Socket</code></a>. Usually users will not want to 2344access this event.</p> 2345<p>This event can also be explicitly emitted by users to inject connections 2346into the HTTP server. In that case, any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream can be passed.</p> 2347<h5>Event: <code>'request'</code><span><a class="mark" href="#event-request" id="event-request">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_request"></a></h5> 2348<div class="api_metadata"> 2349<span>Added in: v8.4.0</span> 2350</div> 2351<ul> 2352<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li> 2353<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li> 2354</ul> 2355<p>Emitted each time there is a request. There may be multiple requests 2356per session. See the <a href="#compatibility-api">Compatibility API</a>.</p> 2357<h5>Event: <code>'session'</code><span><a class="mark" href="#event-session" id="event-session">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_session"></a></h5> 2358<div class="api_metadata"> 2359<span>Added in: v8.4.0</span> 2360</div> 2361<ul> 2362<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type"><ServerHttp2Session></a></li> 2363</ul> 2364<p>The <code>'session'</code> event is emitted when a new <code>Http2Session</code> is created by the 2365<code>Http2Server</code>.</p> 2366<h5>Event: <code>'sessionError'</code><span><a class="mark" href="#event-sessionerror" id="event-sessionerror">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_sessionerror"></a></h5> 2367<div class="api_metadata"> 2368<span>Added in: v8.4.0</span> 2369</div> 2370<ul> 2371<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2372<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type"><ServerHttp2Session></a></li> 2373</ul> 2374<p>The <code>'sessionError'</code> event is emitted when an <code>'error'</code> event is emitted by 2375an <code>Http2Session</code> object associated with the <code>Http2Server</code>.</p> 2376<h5>Event: <code>'stream'</code><span><a class="mark" href="#event-stream_1" id="event-stream_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_stream_1"></a></h5> 2377<div class="api_metadata"> 2378<span>Added in: v8.4.0</span> 2379</div> 2380<ul> 2381<li><code>stream</code> <a href="http2.html#class-http2stream" class="type"><Http2Stream></a> A reference to the stream</li> 2382<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li> 2383<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The associated numeric flags</li> 2384<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array containing the raw header names followed by 2385their respective values.</li> 2386</ul> 2387<p>The <code>'stream'</code> event is emitted when a <code>'stream'</code> event has been emitted by 2388an <code>Http2Session</code> associated with the server.</p> 2389<p>See also <a href="#event-stream"><code>Http2Session</code>'s <code>'stream'</code> event</a>.</p> 2390<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2391<span class="hljs-keyword">const</span> { 2392 <span class="hljs-title class_">HTTP2</span>_HEADER_METHOD, 2393 <span class="hljs-title class_">HTTP2</span>_HEADER_PATH, 2394 <span class="hljs-title class_">HTTP2</span>_HEADER_STATUS, 2395 <span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE, 2396} = http2.<span class="hljs-property">constants</span>; 2397 2398<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 2399server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers, flags</span>) =></span> { 2400 <span class="hljs-keyword">const</span> method = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD]; 2401 <span class="hljs-keyword">const</span> path = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_PATH]; 2402 <span class="hljs-comment">// ...</span> 2403 stream.<span class="hljs-title function_">respond</span>({ 2404 [<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]: <span class="hljs-number">200</span>, 2405 [<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>, 2406 }); 2407 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>); 2408 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>); 2409});</code> <button class="copy-button">copy</button></pre> 2410<h5>Event: <code>'timeout'</code><span><a class="mark" href="#event-timeout_2" id="event-timeout_2">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_timeout_2"></a></h5> 2411<div class="api_metadata"> 2412<details class="changelog"><summary>History</summary> 2413<table> 2414<tbody><tr><th>Version</th><th>Changes</th></tr> 2415<tr><td>v13.0.0</td> 2416<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr> 2417<tr><td>v8.4.0</td> 2418<td><p><span>Added in: v8.4.0</span></p></td></tr> 2419</tbody></table> 2420</details> 2421</div> 2422<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for 2423a given number of milliseconds set using <code>http2server.setTimeout()</code>. 2424<strong>Default:</strong> 0 (no timeout)</p> 2425<h5><code>server.close([callback])</code><span><a class="mark" href="#serverclosecallback" id="serverclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_close_callback"></a></h5> 2426<div class="api_metadata"> 2427<span>Added in: v8.4.0</span> 2428</div> 2429<ul> 2430<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2431</ul> 2432<p>Stops the server from establishing new sessions. This does not prevent new 2433request streams from being created due to the persistent nature of HTTP/2 2434sessions. To gracefully shut down the server, call <a href="#http2sessionclosecallback"><code>http2session.close()</code></a> on 2435all active sessions.</p> 2436<p>If <code>callback</code> is provided, it is not invoked until all active sessions have been 2437closed, although the server has already stopped allowing new sessions. See 2438<a href="net.html#serverclosecallback"><code>net.Server.close()</code></a> for more details.</p> 2439<h5><code>server.setTimeout([msecs][, callback])</code><span><a class="mark" href="#serversettimeoutmsecs-callback" id="serversettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_settimeout_msecs_callback"></a></h5> 2440<div class="api_metadata"> 2441<details class="changelog"><summary>History</summary> 2442<table> 2443<tbody><tr><th>Version</th><th>Changes</th></tr> 2444<tr><td>v18.0.0</td> 2445<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 2446<tr><td>v13.0.0</td> 2447<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr> 2448<tr><td>v8.4.0</td> 2449<td><p><span>Added in: v8.4.0</span></p></td></tr> 2450</tbody></table> 2451</details> 2452</div> 2453<ul> 2454<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> 0 (no timeout)</li> 2455<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2456<li>Returns: <a href="http2.html#class-http2server" class="type"><Http2Server></a></li> 2457</ul> 2458<p>Used to set the timeout value for http2 server requests, 2459and sets a callback function that is called when there is no activity 2460on the <code>Http2Server</code> after <code>msecs</code> milliseconds.</p> 2461<p>The given callback is registered as a listener on the <code>'timeout'</code> event.</p> 2462<p>In case if <code>callback</code> is not a function, a new <code>ERR_INVALID_ARG_TYPE</code> 2463error will be thrown.</p> 2464<h5><code>server.timeout</code><span><a class="mark" href="#servertimeout" id="servertimeout">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_timeout"></a></h5> 2465<div class="api_metadata"> 2466<details class="changelog"><summary>History</summary> 2467<table> 2468<tbody><tr><th>Version</th><th>Changes</th></tr> 2469<tr><td>v13.0.0</td> 2470<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr> 2471<tr><td>v8.4.0</td> 2472<td><p><span>Added in: v8.4.0</span></p></td></tr> 2473</tbody></table> 2474</details> 2475</div> 2476<ul> 2477<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Timeout in milliseconds. <strong>Default:</strong> 0 (no timeout)</li> 2478</ul> 2479<p>The number of milliseconds of inactivity before a socket is presumed 2480to have timed out.</p> 2481<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p> 2482<p>The socket timeout logic is set up on connection, so changing this 2483value only affects new connections to the server, not any existing connections.</p> 2484<h5><code>server.updateSettings([settings])</code><span><a class="mark" href="#serverupdatesettingssettings" id="serverupdatesettingssettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_updatesettings_settings"></a></h5> 2485<div class="api_metadata"> 2486<span>Added in: v15.1.0, v14.17.0</span> 2487</div> 2488<ul> 2489<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li> 2490</ul> 2491<p>Used to update the server with the provided settings.</p> 2492<p>Throws <code>ERR_HTTP2_INVALID_SETTING_VALUE</code> for invalid <code>settings</code> values.</p> 2493<p>Throws <code>ERR_INVALID_ARG_TYPE</code> for invalid <code>settings</code> argument.</p> 2494<h4>Class: <code>Http2SecureServer</code><span><a class="mark" href="#class-http2secureserver" id="class-http2secureserver">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2secureserver"></a></h4> 2495<div class="api_metadata"> 2496<span>Added in: v8.4.0</span> 2497</div> 2498<ul> 2499<li>Extends: <a href="tls.html#class-tlsserver" class="type"><tls.Server></a></li> 2500</ul> 2501<p>Instances of <code>Http2SecureServer</code> are created using the 2502<code>http2.createSecureServer()</code> function. The <code>Http2SecureServer</code> class is not 2503exported directly by the <code>node:http2</code> module.</p> 2504<h5>Event: <code>'checkContinue'</code><span><a class="mark" href="#event-checkcontinue_1" id="event-checkcontinue_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_checkcontinue_1"></a></h5> 2505<div class="api_metadata"> 2506<span>Added in: v8.5.0</span> 2507</div> 2508<ul> 2509<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li> 2510<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li> 2511</ul> 2512<p>If a <a href="#event-request"><code>'request'</code></a> listener is registered or <a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a> 2513is supplied a callback function, the <code>'checkContinue'</code> event is emitted each 2514time a request with an HTTP <code>Expect: 100-continue</code> is received. If this event 2515is not listened for, the server will automatically respond with a status 2516<code>100 Continue</code> as appropriate.</p> 2517<p>Handling this event involves calling <a href="#responsewritecontinue"><code>response.writeContinue()</code></a> if the 2518client should continue to send the request body, or generating an appropriate 2519HTTP response (e.g. 400 Bad Request) if the client should not continue to send 2520the request body.</p> 2521<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will 2522not be emitted.</p> 2523<h5>Event: <code>'connection'</code><span><a class="mark" href="#event-connection_1" id="event-connection_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_connection_1"></a></h5> 2524<div class="api_metadata"> 2525<span>Added in: v8.4.0</span> 2526</div> 2527<ul> 2528<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 2529</ul> 2530<p>This event is emitted when a new TCP stream is established, before the TLS 2531handshake begins. <code>socket</code> is typically an object of type <a href="net.html#class-netsocket"><code>net.Socket</code></a>. 2532Usually users will not want to access this event.</p> 2533<p>This event can also be explicitly emitted by users to inject connections 2534into the HTTP server. In that case, any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream can be passed.</p> 2535<h5>Event: <code>'request'</code><span><a class="mark" href="#event-request_1" id="event-request_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_request_1"></a></h5> 2536<div class="api_metadata"> 2537<span>Added in: v8.4.0</span> 2538</div> 2539<ul> 2540<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li> 2541<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li> 2542</ul> 2543<p>Emitted each time there is a request. There may be multiple requests 2544per session. See the <a href="#compatibility-api">Compatibility API</a>.</p> 2545<h5>Event: <code>'session'</code><span><a class="mark" href="#event-session_1" id="event-session_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_session_1"></a></h5> 2546<div class="api_metadata"> 2547<span>Added in: v8.4.0</span> 2548</div> 2549<ul> 2550<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type"><ServerHttp2Session></a></li> 2551</ul> 2552<p>The <code>'session'</code> event is emitted when a new <code>Http2Session</code> is created by the 2553<code>Http2SecureServer</code>.</p> 2554<h5>Event: <code>'sessionError'</code><span><a class="mark" href="#event-sessionerror_1" id="event-sessionerror_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_sessionerror_1"></a></h5> 2555<div class="api_metadata"> 2556<span>Added in: v8.4.0</span> 2557</div> 2558<ul> 2559<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2560<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type"><ServerHttp2Session></a></li> 2561</ul> 2562<p>The <code>'sessionError'</code> event is emitted when an <code>'error'</code> event is emitted by 2563an <code>Http2Session</code> object associated with the <code>Http2SecureServer</code>.</p> 2564<h5>Event: <code>'stream'</code><span><a class="mark" href="#event-stream_2" id="event-stream_2">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_stream_2"></a></h5> 2565<div class="api_metadata"> 2566<span>Added in: v8.4.0</span> 2567</div> 2568<ul> 2569<li><code>stream</code> <a href="http2.html#class-http2stream" class="type"><Http2Stream></a> A reference to the stream</li> 2570<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li> 2571<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The associated numeric flags</li> 2572<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array containing the raw header names followed by 2573their respective values.</li> 2574</ul> 2575<p>The <code>'stream'</code> event is emitted when a <code>'stream'</code> event has been emitted by 2576an <code>Http2Session</code> associated with the server.</p> 2577<p>See also <a href="#event-stream"><code>Http2Session</code>'s <code>'stream'</code> event</a>.</p> 2578<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2579<span class="hljs-keyword">const</span> { 2580 <span class="hljs-title class_">HTTP2</span>_HEADER_METHOD, 2581 <span class="hljs-title class_">HTTP2</span>_HEADER_PATH, 2582 <span class="hljs-title class_">HTTP2</span>_HEADER_STATUS, 2583 <span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE, 2584} = http2.<span class="hljs-property">constants</span>; 2585 2586<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getOptionsSomehow</span>(); 2587 2588<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options); 2589server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers, flags</span>) =></span> { 2590 <span class="hljs-keyword">const</span> method = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD]; 2591 <span class="hljs-keyword">const</span> path = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_PATH]; 2592 <span class="hljs-comment">// ...</span> 2593 stream.<span class="hljs-title function_">respond</span>({ 2594 [<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]: <span class="hljs-number">200</span>, 2595 [<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>, 2596 }); 2597 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>); 2598 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>); 2599});</code> <button class="copy-button">copy</button></pre> 2600<h5>Event: <code>'timeout'</code><span><a class="mark" href="#event-timeout_3" id="event-timeout_3">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_timeout_3"></a></h5> 2601<div class="api_metadata"> 2602<span>Added in: v8.4.0</span> 2603</div> 2604<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for 2605a given number of milliseconds set using <code>http2secureServer.setTimeout()</code>. 2606<strong>Default:</strong> 2 minutes.</p> 2607<h5>Event: <code>'unknownProtocol'</code><span><a class="mark" href="#event-unknownprotocol" id="event-unknownprotocol">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_unknownprotocol"></a></h5> 2608<div class="api_metadata"> 2609<span>Added in: v8.4.0</span> 2610</div> 2611<ul> 2612<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 2613</ul> 2614<p>The <code>'unknownProtocol'</code> event is emitted when a connecting client fails to 2615negotiate an allowed protocol (i.e. HTTP/2 or HTTP/1.1). The event handler 2616receives the socket for handling. If no listener is registered for this event, 2617the connection is terminated. A timeout may be specified using the 2618<code>'unknownProtocolTimeout'</code> option passed to <a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a>. 2619See the <a href="#compatibility-api">Compatibility API</a>.</p> 2620<h5><code>server.close([callback])</code><span><a class="mark" href="#serverclosecallback_1" id="serverclosecallback_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_close_callback_1"></a></h5> 2621<div class="api_metadata"> 2622<span>Added in: v8.4.0</span> 2623</div> 2624<ul> 2625<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2626</ul> 2627<p>Stops the server from establishing new sessions. This does not prevent new 2628request streams from being created due to the persistent nature of HTTP/2 2629sessions. To gracefully shut down the server, call <a href="#http2sessionclosecallback"><code>http2session.close()</code></a> on 2630all active sessions.</p> 2631<p>If <code>callback</code> is provided, it is not invoked until all active sessions have been 2632closed, although the server has already stopped allowing new sessions. See 2633<a href="tls.html#serverclosecallback"><code>tls.Server.close()</code></a> for more details.</p> 2634<h5><code>server.setTimeout([msecs][, callback])</code><span><a class="mark" href="#serversettimeoutmsecs-callback_1" id="serversettimeoutmsecs-callback_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_settimeout_msecs_callback_1"></a></h5> 2635<div class="api_metadata"> 2636<details class="changelog"><summary>History</summary> 2637<table> 2638<tbody><tr><th>Version</th><th>Changes</th></tr> 2639<tr><td>v18.0.0</td> 2640<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 2641<tr><td>v8.4.0</td> 2642<td><p><span>Added in: v8.4.0</span></p></td></tr> 2643</tbody></table> 2644</details> 2645</div> 2646<ul> 2647<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>120000</code> (2 minutes)</li> 2648<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2649<li>Returns: <a href="http2.html#class-http2secureserver" class="type"><Http2SecureServer></a></li> 2650</ul> 2651<p>Used to set the timeout value for http2 secure server requests, 2652and sets a callback function that is called when there is no activity 2653on the <code>Http2SecureServer</code> after <code>msecs</code> milliseconds.</p> 2654<p>The given callback is registered as a listener on the <code>'timeout'</code> event.</p> 2655<p>In case if <code>callback</code> is not a function, a new <code>ERR_INVALID_ARG_TYPE</code> 2656error will be thrown.</p> 2657<h5><code>server.timeout</code><span><a class="mark" href="#servertimeout_1" id="servertimeout_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_timeout_1"></a></h5> 2658<div class="api_metadata"> 2659<details class="changelog"><summary>History</summary> 2660<table> 2661<tbody><tr><th>Version</th><th>Changes</th></tr> 2662<tr><td>v13.0.0</td> 2663<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr> 2664<tr><td>v8.4.0</td> 2665<td><p><span>Added in: v8.4.0</span></p></td></tr> 2666</tbody></table> 2667</details> 2668</div> 2669<ul> 2670<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Timeout in milliseconds. <strong>Default:</strong> 0 (no timeout)</li> 2671</ul> 2672<p>The number of milliseconds of inactivity before a socket is presumed 2673to have timed out.</p> 2674<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p> 2675<p>The socket timeout logic is set up on connection, so changing this 2676value only affects new connections to the server, not any existing connections.</p> 2677<h5><code>server.updateSettings([settings])</code><span><a class="mark" href="#serverupdatesettingssettings_1" id="serverupdatesettingssettings_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_updatesettings_settings_1"></a></h5> 2678<div class="api_metadata"> 2679<span>Added in: v15.1.0, v14.17.0</span> 2680</div> 2681<ul> 2682<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li> 2683</ul> 2684<p>Used to update the server with the provided settings.</p> 2685<p>Throws <code>ERR_HTTP2_INVALID_SETTING_VALUE</code> for invalid <code>settings</code> values.</p> 2686<p>Throws <code>ERR_INVALID_ARG_TYPE</code> for invalid <code>settings</code> argument.</p> 2687<h4><code>http2.createServer([options][, onRequestHandler])</code><span><a class="mark" href="#http2createserveroptions-onrequesthandler" id="http2createserveroptions-onrequesthandler">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_createserver_options_onrequesthandler"></a></h4> 2688<div class="api_metadata"> 2689<details class="changelog"><summary>History</summary> 2690<table> 2691<tbody><tr><th>Version</th><th>Changes</th></tr> 2692<tr><td>v13.0.0</td> 2693<td><p>The <code>PADDING_STRATEGY_CALLBACK</code> has been made equivalent to providing <code>PADDING_STRATEGY_ALIGNED</code> and <code>selectPadding</code> has been removed.</p></td></tr> 2694<tr><td>v13.3.0, v12.16.0</td> 2695<td><p>Added <code>maxSessionRejectedStreams</code> option with a default of 100.</p></td></tr> 2696<tr><td>v13.3.0, v12.16.0</td> 2697<td><p>Added <code>maxSessionInvalidFrames</code> option with a default of 1000.</p></td></tr> 2698<tr><td>v12.4.0</td> 2699<td><p>The <code>options</code> parameter now supports <code>net.createServer()</code> options.</p></td></tr> 2700<tr><td>v15.10.0, v14.16.0, v12.21.0, v10.24.0</td> 2701<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr> 2702<tr><td>v14.4.0, v12.18.0, v10.21.0</td> 2703<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr> 2704<tr><td>v9.6.0</td> 2705<td><p>Added the <code>Http1IncomingMessage</code> and <code>Http1ServerResponse</code> option.</p></td></tr> 2706<tr><td>v8.9.3</td> 2707<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr> 2708<tr><td>v8.9.3</td> 2709<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr> 2710<tr><td>v8.4.0</td> 2711<td><p><span>Added in: v8.4.0</span></p></td></tr> 2712</tbody></table> 2713</details> 2714</div> 2715<ul> 2716<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2717<ul> 2718<li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum dynamic table size 2719for deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li> 2720<li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of settings entries per 2721<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li> 2722<li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum memory that the <code>Http2Session</code> 2723is permitted to use. The value is expressed in terms of number of megabytes, 2724e.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>. 2725This is a credit based limit, existing <code>Http2Stream</code>s may cause this 2726limit to be exceeded, but new <code>Http2Stream</code> instances will be rejected 2727while this limit is exceeded. The current number of <code>Http2Stream</code> sessions, 2728the current memory use of the header compression tables, current data 2729queued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all 2730counted towards the current limit. <strong>Default:</strong> <code>10</code>.</li> 2731<li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of header entries. 2732This is similar to <a href="http.html#servermaxheaderscount"><code>server.maxHeadersCount</code></a> or 2733<a href="http.html#requestmaxheaderscount"><code>request.maxHeadersCount</code></a> in the <code>node:http</code> module. The minimum value 2734is <code>4</code>. <strong>Default:</strong> <code>128</code>.</li> 2735<li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of outstanding, 2736unacknowledged pings. <strong>Default:</strong> <code>10</code>.</li> 2737<li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum allowed size for a 2738serialized, compressed block of headers. Attempts to send headers that 2739exceed this limit will result in a <code>'frameError'</code> event being emitted 2740and the stream being closed and destroyed. 2741While this sets the maximum allowed size to the entire block of headers, 2742<code>nghttp2</code> (the internal http2 library) has a limit of <code>65536</code> 2743for each decompressed key/value pair.</li> 2744<li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The strategy used for determining the amount of 2745padding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong> 2746<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of: 2747<ul> 2748<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li> 2749<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding, 2750determined by the internal implementation, is applied.</li> 2751<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough 2752padding to ensure that the total frame length, including the 9-byte 2753header, is a multiple of 8. For each frame, there is a maximum allowed 2754number of padding bytes that is determined by current flow control state 2755and settings. If this maximum is less than the calculated amount needed to 2756ensure alignment, the maximum is used and the total frame length is not 2757necessarily aligned at 8 bytes.</li> 2758</ul> 2759</li> 2760<li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of concurrent 2761streams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will 2762be overridden if the remote peer sets its own value for 2763<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li> 2764<li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Sets the maximum number of invalid 2765frames that will be tolerated before the session is closed. 2766<strong>Default:</strong> <code>1000</code>.</li> 2767<li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Sets the maximum number of rejected 2768upon creation streams that will be tolerated before the session is closed. 2769Each rejection is associated with an <code>NGHTTP2_ENHANCE_YOUR_CALM</code> 2770error that should tell the peer to not open any more streams, continuing 2771to open streams is therefore regarded as a sign of a misbehaving peer. 2772<strong>Default:</strong> <code>100</code>.</li> 2773<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a> The initial settings to send to the 2774remote peer upon connection.</li> 2775<li><code>Http1IncomingMessage</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Specifies the 2776<code>IncomingMessage</code> class to used for HTTP/1 fallback. Useful for extending 2777the original <code>http.IncomingMessage</code>. <strong>Default:</strong> <code>http.IncomingMessage</code>.</li> 2778<li><code>Http1ServerResponse</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a> Specifies the <code>ServerResponse</code> 2779class to used for HTTP/1 fallback. Useful for extending the original 2780<code>http.ServerResponse</code>. <strong>Default:</strong> <code>http.ServerResponse</code>.</li> 2781<li><code>Http2ServerRequest</code> <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a> Specifies the 2782<code>Http2ServerRequest</code> class to use. 2783Useful for extending the original <code>Http2ServerRequest</code>. 2784<strong>Default:</strong> <code>Http2ServerRequest</code>.</li> 2785<li><code>Http2ServerResponse</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a> Specifies the 2786<code>Http2ServerResponse</code> class to use. 2787Useful for extending the original <code>Http2ServerResponse</code>. 2788<strong>Default:</strong> <code>Http2ServerResponse</code>.</li> 2789<li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies a timeout in milliseconds that 2790a server should wait when an <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> is emitted. If the 2791socket has not been destroyed by that time the server will destroy it. 2792<strong>Default:</strong> <code>10000</code>.</li> 2793<li>...: Any <a href="net.html#netcreateserveroptions-connectionlistener"><code>net.createServer()</code></a> option can be provided.</li> 2794</ul> 2795</li> 2796<li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> See <a href="#compatibility-api">Compatibility API</a></li> 2797<li>Returns: <a href="http2.html#class-http2server" class="type"><Http2Server></a></li> 2798</ul> 2799<p>Returns a <code>net.Server</code> instance that creates and manages <code>Http2Session</code> 2800instances.</p> 2801<p>Since there are no browsers known that support 2802<a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the use of 2803<a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a> is necessary when communicating 2804with browser clients.</p> 2805<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2806 2807<span class="hljs-comment">// Create an unencrypted HTTP/2 server.</span> 2808<span class="hljs-comment">// Since there are no browsers known that support</span> 2809<span class="hljs-comment">// unencrypted HTTP/2, the use of `http2.createSecureServer()`</span> 2810<span class="hljs-comment">// is necessary when communicating with browser clients.</span> 2811<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 2812 2813server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> { 2814 stream.<span class="hljs-title function_">respond</span>({ 2815 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>, 2816 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>, 2817 }); 2818 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>); 2819}); 2820 2821server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre> 2822<h4><code>http2.createSecureServer(options[, onRequestHandler])</code><span><a class="mark" href="#http2createsecureserveroptions-onrequesthandler" id="http2createsecureserveroptions-onrequesthandler">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_createsecureserver_options_onrequesthandler"></a></h4> 2823<div class="api_metadata"> 2824<details class="changelog"><summary>History</summary> 2825<table> 2826<tbody><tr><th>Version</th><th>Changes</th></tr> 2827<tr><td>v13.0.0</td> 2828<td><p>The <code>PADDING_STRATEGY_CALLBACK</code> has been made equivalent to providing <code>PADDING_STRATEGY_ALIGNED</code> and <code>selectPadding</code> has been removed.</p></td></tr> 2829<tr><td>v13.3.0, v12.16.0</td> 2830<td><p>Added <code>maxSessionRejectedStreams</code> option with a default of 100.</p></td></tr> 2831<tr><td>v13.3.0, v12.16.0</td> 2832<td><p>Added <code>maxSessionInvalidFrames</code> option with a default of 1000.</p></td></tr> 2833<tr><td>v15.10.0, v14.16.0, v12.21.0, v10.24.0</td> 2834<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr> 2835<tr><td>v14.4.0, v12.18.0, v10.21.0</td> 2836<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr> 2837<tr><td>v10.12.0</td> 2838<td><p>Added the <code>origins</code> option to automatically send an <code>ORIGIN</code> frame on <code>Http2Session</code> startup.</p></td></tr> 2839<tr><td>v8.9.3</td> 2840<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr> 2841<tr><td>v8.9.3</td> 2842<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr> 2843<tr><td>v8.4.0</td> 2844<td><p><span>Added in: v8.4.0</span></p></td></tr> 2845</tbody></table> 2846</details> 2847</div> 2848<ul> 2849<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2850<ul> 2851<li><code>allowHTTP1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Incoming client connections that do not support 2852HTTP/2 will be downgraded to HTTP/1.x when set to <code>true</code>. 2853See the <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> event. See <a href="#alpn-negotiation">ALPN negotiation</a>. 2854<strong>Default:</strong> <code>false</code>.</li> 2855<li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum dynamic table size 2856for deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li> 2857<li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of settings entries per 2858<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li> 2859<li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum memory that the <code>Http2Session</code> 2860is permitted to use. The value is expressed in terms of number of megabytes, 2861e.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>. This is a 2862credit based limit, existing <code>Http2Stream</code>s may cause this 2863limit to be exceeded, but new <code>Http2Stream</code> instances will be rejected 2864while this limit is exceeded. The current number of <code>Http2Stream</code> sessions, 2865the current memory use of the header compression tables, current data 2866queued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all 2867counted towards the current limit. <strong>Default:</strong> <code>10</code>.</li> 2868<li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of header entries. 2869This is similar to <a href="http.html#servermaxheaderscount"><code>server.maxHeadersCount</code></a> or 2870<a href="http.html#requestmaxheaderscount"><code>request.maxHeadersCount</code></a> in the <code>node:http</code> module. The minimum value 2871is <code>4</code>. <strong>Default:</strong> <code>128</code>.</li> 2872<li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of outstanding, 2873unacknowledged pings. <strong>Default:</strong> <code>10</code>.</li> 2874<li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum allowed size for a 2875serialized, compressed block of headers. Attempts to send headers that 2876exceed this limit will result in a <code>'frameError'</code> event being emitted 2877and the stream being closed and destroyed.</li> 2878<li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Strategy used for determining the amount of 2879padding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong> 2880<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of: 2881<ul> 2882<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li> 2883<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding, 2884determined by the internal implementation, is applied.</li> 2885<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough 2886padding to ensure that the total frame length, including the 28879-byte header, is a multiple of 8. For each frame, there is a maximum 2888allowed number of padding bytes that is determined by current flow control 2889state and settings. If this maximum is less than the calculated amount 2890needed to ensure alignment, the maximum is used and the total frame length 2891is not necessarily aligned at 8 bytes.</li> 2892</ul> 2893</li> 2894<li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of concurrent 2895streams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will 2896be overridden if the remote peer sets its own value for 2897<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li> 2898<li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Sets the maximum number of invalid 2899frames that will be tolerated before the session is closed. 2900<strong>Default:</strong> <code>1000</code>.</li> 2901<li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Sets the maximum number of rejected 2902upon creation streams that will be tolerated before the session is closed. 2903Each rejection is associated with an <code>NGHTTP2_ENHANCE_YOUR_CALM</code> 2904error that should tell the peer to not open any more streams, continuing 2905to open streams is therefore regarded as a sign of a misbehaving peer. 2906<strong>Default:</strong> <code>100</code>.</li> 2907<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a> The initial settings to send to the 2908remote peer upon connection.</li> 2909<li>...: Any <a href="tls.html#tlscreateserveroptions-secureconnectionlistener"><code>tls.createServer()</code></a> options can be provided. For 2910servers, the identity options (<code>pfx</code> or <code>key</code>/<code>cert</code>) are usually required.</li> 2911<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> An array of origin strings to send within an <code>ORIGIN</code> 2912frame immediately following creation of a new server <code>Http2Session</code>.</li> 2913<li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies a timeout in milliseconds that 2914a server should wait when an <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> event is emitted. If 2915the socket has not been destroyed by that time the server will destroy it. 2916<strong>Default:</strong> <code>10000</code>.</li> 2917</ul> 2918</li> 2919<li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> See <a href="#compatibility-api">Compatibility API</a></li> 2920<li>Returns: <a href="http2.html#class-http2secureserver" class="type"><Http2SecureServer></a></li> 2921</ul> 2922<p>Returns a <code>tls.Server</code> instance that creates and manages <code>Http2Session</code> 2923instances.</p> 2924<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 2925<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 2926 2927<span class="hljs-keyword">const</span> options = { 2928 <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-key.pem'</span>), 2929 <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-cert.pem'</span>), 2930}; 2931 2932<span class="hljs-comment">// Create a secure HTTP/2 server</span> 2933<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options); 2934 2935server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> { 2936 stream.<span class="hljs-title function_">respond</span>({ 2937 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>, 2938 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>, 2939 }); 2940 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>); 2941}); 2942 2943server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</code> <button class="copy-button">copy</button></pre> 2944<h4><code>http2.connect(authority[, options][, listener])</code><span><a class="mark" href="#http2connectauthority-options-listener" id="http2connectauthority-options-listener">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_connect_authority_options_listener"></a></h4> 2945<div class="api_metadata"> 2946<details class="changelog"><summary>History</summary> 2947<table> 2948<tbody><tr><th>Version</th><th>Changes</th></tr> 2949<tr><td>v13.0.0</td> 2950<td><p>The <code>PADDING_STRATEGY_CALLBACK</code> has been made equivalent to providing <code>PADDING_STRATEGY_ALIGNED</code> and <code>selectPadding</code> has been removed.</p></td></tr> 2951<tr><td>v15.10.0, v14.16.0, v12.21.0, v10.24.0</td> 2952<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr> 2953<tr><td>v14.4.0, v12.18.0, v10.21.0</td> 2954<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr> 2955<tr><td>v8.9.3</td> 2956<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr> 2957<tr><td>v8.9.3</td> 2958<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr> 2959<tr><td>v8.4.0</td> 2960<td><p><span>Added in: v8.4.0</span></p></td></tr> 2961</tbody></table> 2962</details> 2963</div> 2964<ul> 2965<li><code>authority</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> The remote HTTP/2 server to connect to. This must 2966be in the form of a minimal, valid URL with the <code>http://</code> or <code>https://</code> 2967prefix, host name, and IP port (if a non-default port is used). Userinfo 2968(user ID and password), path, querystring, and fragment details in the 2969URL will be ignored.</li> 2970<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2971<ul> 2972<li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum dynamic table size 2973for deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li> 2974<li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of settings entries per 2975<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li> 2976<li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum memory that the <code>Http2Session</code> 2977is permitted to use. The value is expressed in terms of number of megabytes, 2978e.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>. 2979This is a credit based limit, existing <code>Http2Stream</code>s may cause this 2980limit to be exceeded, but new <code>Http2Stream</code> instances will be rejected 2981while this limit is exceeded. The current number of <code>Http2Stream</code> sessions, 2982the current memory use of the header compression tables, current data 2983queued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all 2984counted towards the current limit. <strong>Default:</strong> <code>10</code>.</li> 2985<li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of header entries. 2986This is similar to <a href="http.html#servermaxheaderscount"><code>server.maxHeadersCount</code></a> or 2987<a href="http.html#requestmaxheaderscount"><code>request.maxHeadersCount</code></a> in the <code>node:http</code> module. The minimum value 2988is <code>1</code>. <strong>Default:</strong> <code>128</code>.</li> 2989<li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of outstanding, 2990unacknowledged pings. <strong>Default:</strong> <code>10</code>.</li> 2991<li><code>maxReservedRemoteStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of reserved push 2992streams the client will accept at any given time. Once the current number of 2993currently reserved push streams exceeds reaches this limit, new push streams 2994sent by the server will be automatically rejected. The minimum allowed value 2995is 0. The maximum allowed value is 2<sup>32</sup>-1. A negative value sets 2996this option to the maximum allowed value. <strong>Default:</strong> <code>200</code>.</li> 2997<li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum allowed size for a 2998serialized, compressed block of headers. Attempts to send headers that 2999exceed this limit will result in a <code>'frameError'</code> event being emitted 3000and the stream being closed and destroyed.</li> 3001<li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Strategy used for determining the amount of 3002padding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong> 3003<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of: 3004<ul> 3005<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li> 3006<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding, 3007determined by the internal implementation, is applied.</li> 3008<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough 3009padding to ensure that the total frame length, including the 30109-byte header, is a multiple of 8. For each frame, there is a maximum 3011allowed number of padding bytes that is determined by current flow control 3012state and settings. If this maximum is less than the calculated amount 3013needed to ensure alignment, the maximum is used and the total frame length 3014is not necessarily aligned at 8 bytes.</li> 3015</ul> 3016</li> 3017<li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the maximum number of concurrent 3018streams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will 3019be overridden if the remote peer sets its own value for 3020<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li> 3021<li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The protocol to connect with, if not set in the 3022<code>authority</code>. Value may be either <code>'http:'</code> or <code>'https:'</code>. <strong>Default:</strong> 3023<code>'https:'</code></li> 3024<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a> The initial settings to send to the 3025remote peer upon connection.</li> 3026<li><code>createConnection</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> An optional callback that receives the <code>URL</code> 3027instance passed to <code>connect</code> and the <code>options</code> object, and returns any 3028<a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream that is to be used as the connection for this session.</li> 3029<li>...: Any <a href="net.html#netconnect"><code>net.connect()</code></a> or <a href="tls.html#tlsconnectoptions-callback"><code>tls.connect()</code></a> options can be provided.</li> 3030<li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies a timeout in milliseconds that 3031a server should wait when an <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> event is emitted. If 3032the socket has not been destroyed by that time the server will destroy it. 3033<strong>Default:</strong> <code>10000</code>.</li> 3034</ul> 3035</li> 3036<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Will be registered as a one-time listener of the 3037<a href="#event-connect"><code>'connect'</code></a> event.</li> 3038<li>Returns: <a href="http2.html#class-clienthttp2session" class="type"><ClientHttp2Session></a></li> 3039</ul> 3040<p>Returns a <code>ClientHttp2Session</code> instance.</p> 3041<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3042<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:1234'</span>); 3043 3044<span class="hljs-comment">/* Use the client */</span> 3045 3046client.<span class="hljs-title function_">close</span>();</code> <button class="copy-button">copy</button></pre> 3047<h4><code>http2.constants</code><span><a class="mark" href="#http2constants" id="http2constants">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_constants"></a></h4> 3048<div class="api_metadata"> 3049<span>Added in: v8.4.0</span> 3050</div> 3051<h5>Error codes for <code>RST_STREAM</code> and <code>GOAWAY</code><span><a class="mark" href="#error-codes-for-rst_stream-and-goaway" id="error-codes-for-rst_stream-and-goaway">#</a></span><a aria-hidden="true" class="legacy" id="http2_error_codes_for_rst_stream_and_goaway"></a></h5> 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132<table><thead><tr><th>Value</th><th>Name</th><th>Constant</th></tr></thead><tbody><tr><td><code>0x00</code></td><td>No Error</td><td><code>http2.constants.NGHTTP2_NO_ERROR</code></td></tr><tr><td><code>0x01</code></td><td>Protocol Error</td><td><code>http2.constants.NGHTTP2_PROTOCOL_ERROR</code></td></tr><tr><td><code>0x02</code></td><td>Internal Error</td><td><code>http2.constants.NGHTTP2_INTERNAL_ERROR</code></td></tr><tr><td><code>0x03</code></td><td>Flow Control Error</td><td><code>http2.constants.NGHTTP2_FLOW_CONTROL_ERROR</code></td></tr><tr><td><code>0x04</code></td><td>Settings Timeout</td><td><code>http2.constants.NGHTTP2_SETTINGS_TIMEOUT</code></td></tr><tr><td><code>0x05</code></td><td>Stream Closed</td><td><code>http2.constants.NGHTTP2_STREAM_CLOSED</code></td></tr><tr><td><code>0x06</code></td><td>Frame Size Error</td><td><code>http2.constants.NGHTTP2_FRAME_SIZE_ERROR</code></td></tr><tr><td><code>0x07</code></td><td>Refused Stream</td><td><code>http2.constants.NGHTTP2_REFUSED_STREAM</code></td></tr><tr><td><code>0x08</code></td><td>Cancel</td><td><code>http2.constants.NGHTTP2_CANCEL</code></td></tr><tr><td><code>0x09</code></td><td>Compression Error</td><td><code>http2.constants.NGHTTP2_COMPRESSION_ERROR</code></td></tr><tr><td><code>0x0a</code></td><td>Connect Error</td><td><code>http2.constants.NGHTTP2_CONNECT_ERROR</code></td></tr><tr><td><code>0x0b</code></td><td>Enhance Your Calm</td><td><code>http2.constants.NGHTTP2_ENHANCE_YOUR_CALM</code></td></tr><tr><td><code>0x0c</code></td><td>Inadequate Security</td><td><code>http2.constants.NGHTTP2_INADEQUATE_SECURITY</code></td></tr><tr><td><code>0x0d</code></td><td>HTTP/1.1 Required</td><td><code>http2.constants.NGHTTP2_HTTP_1_1_REQUIRED</code></td></tr></tbody></table> 3133<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for 3134a given number of milliseconds set using <code>http2server.setTimeout()</code>.</p> 3135<h4><code>http2.getDefaultSettings()</code><span><a class="mark" href="#http2getdefaultsettings" id="http2getdefaultsettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_getdefaultsettings"></a></h4> 3136<div class="api_metadata"> 3137<span>Added in: v8.4.0</span> 3138</div> 3139<ul> 3140<li>Returns: <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li> 3141</ul> 3142<p>Returns an object containing the default settings for an <code>Http2Session</code> 3143instance. This method returns a new object instance every time it is called 3144so instances returned may be safely modified for use.</p> 3145<h4><code>http2.getPackedSettings([settings])</code><span><a class="mark" href="#http2getpackedsettingssettings" id="http2getpackedsettingssettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_getpackedsettings_settings"></a></h4> 3146<div class="api_metadata"> 3147<span>Added in: v8.4.0</span> 3148</div> 3149<ul> 3150<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li> 3151<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 3152</ul> 3153<p>Returns a <code>Buffer</code> instance containing serialized representation of the given 3154HTTP/2 settings as specified in the <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> specification. This is intended 3155for use with the <code>HTTP2-Settings</code> header field.</p> 3156<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3157 3158<span class="hljs-keyword">const</span> packed = http2.<span class="hljs-title function_">getPackedSettings</span>({ <span class="hljs-attr">enablePush</span>: <span class="hljs-literal">false</span> }); 3159 3160<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(packed.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'base64'</span>)); 3161<span class="hljs-comment">// Prints: AAIAAAAA</span></code> <button class="copy-button">copy</button></pre> 3162<h4><code>http2.getUnpackedSettings(buf)</code><span><a class="mark" href="#http2getunpackedsettingsbuf" id="http2getunpackedsettingsbuf">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_getunpackedsettings_buf"></a></h4> 3163<div class="api_metadata"> 3164<span>Added in: v8.4.0</span> 3165</div> 3166<ul> 3167<li><code>buf</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> The packed settings.</li> 3168<li>Returns: <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li> 3169</ul> 3170<p>Returns a <a href="#settings-object">HTTP/2 Settings Object</a> containing the deserialized settings from 3171the given <code>Buffer</code> as generated by <code>http2.getPackedSettings()</code>.</p> 3172<h4><code>http2.sensitiveHeaders</code><span><a class="mark" href="#http2sensitiveheaders" id="http2sensitiveheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_sensitiveheaders"></a></h4> 3173<div class="api_metadata"> 3174<span>Added in: v15.0.0, v14.18.0</span> 3175</div> 3176<ul> 3177<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a></li> 3178</ul> 3179<p>This symbol can be set as a property on the HTTP/2 headers object with an array 3180value in order to provide a list of headers considered sensitive. 3181See <a href="#sensitive-headers">Sensitive headers</a> for more details.</p> 3182<h4>Headers object<span><a class="mark" href="#headers-object" id="headers-object">#</a></span><a aria-hidden="true" class="legacy" id="http2_headers_object"></a></h4> 3183<p>Headers are represented as own-properties on JavaScript objects. The property 3184keys will be serialized to lower-case. Property values should be strings (if 3185they are not they will be coerced to strings) or an <code>Array</code> of strings (in order 3186to send more than one value per header field).</p> 3187<pre><code class="language-js"><span class="hljs-keyword">const</span> headers = { 3188 <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>, 3189 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>, 3190 <span class="hljs-string">'ABC'</span>: [<span class="hljs-string">'has'</span>, <span class="hljs-string">'more'</span>, <span class="hljs-string">'than'</span>, <span class="hljs-string">'one'</span>, <span class="hljs-string">'value'</span>], 3191}; 3192 3193stream.<span class="hljs-title function_">respond</span>(headers);</code> <button class="copy-button">copy</button></pre> 3194<p>Header objects passed to callback functions will have a <code>null</code> prototype. This 3195means that normal JavaScript object methods such as 3196<code>Object.prototype.toString()</code> and <code>Object.prototype.hasOwnProperty()</code> will 3197not work.</p> 3198<p>For incoming headers:</p> 3199<ul> 3200<li>The <code>:status</code> header is converted to <code>number</code>.</li> 3201<li>Duplicates of <code>:status</code>, <code>:method</code>, <code>:authority</code>, <code>:scheme</code>, <code>:path</code>, 3202<code>:protocol</code>, <code>age</code>, <code>authorization</code>, <code>access-control-allow-credentials</code>, 3203<code>access-control-max-age</code>, <code>access-control-request-method</code>, <code>content-encoding</code>, 3204<code>content-language</code>, <code>content-length</code>, <code>content-location</code>, <code>content-md5</code>, 3205<code>content-range</code>, <code>content-type</code>, <code>date</code>, <code>dnt</code>, <code>etag</code>, <code>expires</code>, <code>from</code>, 3206<code>host</code>, <code>if-match</code>, <code>if-modified-since</code>, <code>if-none-match</code>, <code>if-range</code>, 3207<code>if-unmodified-since</code>, <code>last-modified</code>, <code>location</code>, <code>max-forwards</code>, 3208<code>proxy-authorization</code>, <code>range</code>, <code>referer</code>,<code>retry-after</code>, <code>tk</code>, 3209<code>upgrade-insecure-requests</code>, <code>user-agent</code> or <code>x-content-type-options</code> are 3210discarded.</li> 3211<li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li> 3212<li>For duplicate <code>cookie</code> headers, the values are joined together with '; '.</li> 3213<li>For all other headers, the values are joined together with ', '.</li> 3214</ul> 3215<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3216<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(); 3217server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> { 3218 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-string">':path'</span>]); 3219 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers.<span class="hljs-property">ABC</span>); 3220});</code> <button class="copy-button">copy</button></pre> 3221<h5>Sensitive headers<span><a class="mark" href="#sensitive-headers" id="sensitive-headers">#</a></span><a aria-hidden="true" class="legacy" id="http2_sensitive_headers"></a></h5> 3222<p>HTTP2 headers can be marked as sensitive, which means that the HTTP/2 3223header compression algorithm will never index them. This can make sense for 3224header values with low entropy and that may be considered valuable to an 3225attacker, for example <code>Cookie</code> or <code>Authorization</code>. To achieve this, add 3226the header name to the <code>[http2.sensitiveHeaders]</code> property as an array:</p> 3227<pre><code class="language-js"><span class="hljs-keyword">const</span> headers = { 3228 <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>, 3229 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>, 3230 <span class="hljs-string">'cookie'</span>: <span class="hljs-string">'some-cookie'</span>, 3231 <span class="hljs-string">'other-sensitive-header'</span>: <span class="hljs-string">'very secret data'</span>, 3232 [http2.<span class="hljs-property">sensitiveHeaders</span>]: [<span class="hljs-string">'cookie'</span>, <span class="hljs-string">'other-sensitive-header'</span>], 3233}; 3234 3235stream.<span class="hljs-title function_">respond</span>(headers);</code> <button class="copy-button">copy</button></pre> 3236<p>For some headers, such as <code>Authorization</code> and short <code>Cookie</code> headers, 3237this flag is set automatically.</p> 3238<p>This property is also set for received headers. It will contain the names of 3239all headers marked as sensitive, including ones marked that way automatically.</p> 3240<h4>Settings object<span><a class="mark" href="#settings-object" id="settings-object">#</a></span><a aria-hidden="true" class="legacy" id="http2_settings_object"></a></h4> 3241<div class="api_metadata"> 3242<details class="changelog"><summary>History</summary> 3243<table> 3244<tbody><tr><th>Version</th><th>Changes</th></tr> 3245<tr><td>v12.12.0</td> 3246<td><p>The <code>maxConcurrentStreams</code> setting is stricter.</p></td></tr> 3247<tr><td>v8.9.3</td> 3248<td><p>The <code>maxHeaderListSize</code> setting is now strictly enforced.</p></td></tr> 3249<tr><td>v8.4.0</td> 3250<td><p><span>Added in: v8.4.0</span></p></td></tr> 3251</tbody></table> 3252</details> 3253</div> 3254<p>The <code>http2.getDefaultSettings()</code>, <code>http2.getPackedSettings()</code>, 3255<code>http2.createServer()</code>, <code>http2.createSecureServer()</code>, 3256<code>http2session.settings()</code>, <code>http2session.localSettings</code>, and 3257<code>http2session.remoteSettings</code> APIs either return or receive as input an 3258object that defines configuration settings for an <code>Http2Session</code> object. 3259These objects are ordinary JavaScript objects containing the following 3260properties.</p> 3261<ul> 3262<li><code>headerTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the maximum number of bytes used for 3263header compression. The minimum allowed value is 0. The maximum allowed value 3264is 2<sup>32</sup>-1. <strong>Default:</strong> <code>4096</code>.</li> 3265<li><code>enablePush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Specifies <code>true</code> if HTTP/2 Push Streams are to be 3266permitted on the <code>Http2Session</code> instances. <strong>Default:</strong> <code>true</code>.</li> 3267<li><code>initialWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the <em>sender's</em> initial window size in 3268bytes for stream-level flow control. The minimum allowed value is 0. The 3269maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li> 3270<li><code>maxFrameSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the size in bytes of the largest frame 3271payload. The minimum allowed value is 16,384. The maximum allowed value is 32722<sup>24</sup>-1. <strong>Default:</strong> <code>16384</code>.</li> 3273<li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the maximum number of concurrent 3274streams permitted on an <code>Http2Session</code>. There is no default value which 3275implies, at least theoretically, 2<sup>32</sup>-1 streams may be open 3276concurrently at any given time in an <code>Http2Session</code>. The minimum value 3277is 0. The maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> 3278<code>4294967295</code>.</li> 3279<li><code>maxHeaderListSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the maximum size (uncompressed octets) 3280of header list that will be accepted. The minimum allowed value is 0. The 3281maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li> 3282<li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Alias for <code>maxHeaderListSize</code>.</li> 3283<li><code>enableConnectProtocol</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Specifies <code>true</code> if the "Extended Connect 3284Protocol" defined by <a href="https://tools.ietf.org/html/rfc8441">RFC 8441</a> is to be enabled. This setting is only 3285meaningful if sent by the server. Once the <code>enableConnectProtocol</code> setting 3286has been enabled for a given <code>Http2Session</code>, it cannot be disabled. 3287<strong>Default:</strong> <code>false</code>.</li> 3288</ul> 3289<p>All additional properties on the settings object are ignored.</p> 3290<h4>Error handling<span><a class="mark" href="#error-handling" id="error-handling">#</a></span><a aria-hidden="true" class="legacy" id="http2_error_handling"></a></h4> 3291<p>There are several types of error conditions that may arise when using the 3292<code>node:http2</code> module:</p> 3293<p>Validation errors occur when an incorrect argument, option, or setting value is 3294passed in. These will always be reported by a synchronous <code>throw</code>.</p> 3295<p>State errors occur when an action is attempted at an incorrect time (for 3296instance, attempting to send data on a stream after it has closed). These will 3297be reported using either a synchronous <code>throw</code> or via an <code>'error'</code> event on 3298the <code>Http2Stream</code>, <code>Http2Session</code> or HTTP/2 Server objects, depending on where 3299and when the error occurs.</p> 3300<p>Internal errors occur when an HTTP/2 session fails unexpectedly. These will be 3301reported via an <code>'error'</code> event on the <code>Http2Session</code> or HTTP/2 Server objects.</p> 3302<p>Protocol errors occur when various HTTP/2 protocol constraints are violated. 3303These will be reported using either a synchronous <code>throw</code> or via an <code>'error'</code> 3304event on the <code>Http2Stream</code>, <code>Http2Session</code> or HTTP/2 Server objects, depending 3305on where and when the error occurs.</p> 3306<h4>Invalid character handling in header names and values<span><a class="mark" href="#invalid-character-handling-in-header-names-and-values" id="invalid-character-handling-in-header-names-and-values">#</a></span><a aria-hidden="true" class="legacy" id="http2_invalid_character_handling_in_header_names_and_values"></a></h4> 3307<p>The HTTP/2 implementation applies stricter handling of invalid characters in 3308HTTP header names and values than the HTTP/1 implementation.</p> 3309<p>Header field names are <em>case-insensitive</em> and are transmitted over the wire 3310strictly as lower-case strings. The API provided by Node.js allows header 3311names to be set as mixed-case strings (e.g. <code>Content-Type</code>) but will convert 3312those to lower-case (e.g. <code>content-type</code>) upon transmission.</p> 3313<p>Header field-names <em>must only</em> contain one or more of the following ASCII 3314characters: <code>a</code>-<code>z</code>, <code>A</code>-<code>Z</code>, <code>0</code>-<code>9</code>, <code>!</code>, <code>#</code>, <code>$</code>, <code>%</code>, <code>&</code>, <code>'</code>, <code>*</code>, <code>+</code>, 3315<code>-</code>, <code>.</code>, <code>^</code>, <code>_</code>, <code>`</code> (backtick), <code>|</code>, and <code>~</code>.</p> 3316<p>Using invalid characters within an HTTP header field name will cause the 3317stream to be closed with a protocol error being reported.</p> 3318<p>Header field values are handled with more leniency but <em>should</em> not contain 3319new-line or carriage return characters and <em>should</em> be limited to US-ASCII 3320characters, per the requirements of the HTTP specification.</p> 3321<h4>Push streams on the client<span><a class="mark" href="#push-streams-on-the-client" id="push-streams-on-the-client">#</a></span><a aria-hidden="true" class="legacy" id="http2_push_streams_on_the_client"></a></h4> 3322<p>To receive pushed streams on the client, set a listener for the <code>'stream'</code> 3323event on the <code>ClientHttp2Session</code>:</p> 3324<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3325 3326<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost'</span>); 3327 3328client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">pushedStream, requestHeaders</span>) =></span> { 3329 pushedStream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'push'</span>, <span class="hljs-function">(<span class="hljs-params">responseHeaders</span>) =></span> { 3330 <span class="hljs-comment">// Process response headers</span> 3331 }); 3332 pushedStream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> { <span class="hljs-comment">/* handle pushed data */</span> }); 3333}); 3334 3335<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> });</code> <button class="copy-button">copy</button></pre> 3336<h4>Supporting the <code>CONNECT</code> method<span><a class="mark" href="#supporting-the-connect-method" id="supporting-the-connect-method">#</a></span><a aria-hidden="true" class="legacy" id="http2_supporting_the_connect_method"></a></h4> 3337<p>The <code>CONNECT</code> method is used to allow an HTTP/2 server to be used as a proxy 3338for TCP/IP connections.</p> 3339<p>A simple TCP Server:</p> 3340<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>); 3341 3342<span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { 3343 <span class="hljs-keyword">let</span> name = <span class="hljs-string">''</span>; 3344 socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>); 3345 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> name += chunk); 3346 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">`hello <span class="hljs-subst">${name}</span>`</span>)); 3347}); 3348 3349server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre> 3350<p>An HTTP/2 CONNECT proxy:</p> 3351<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3352<span class="hljs-keyword">const</span> { <span class="hljs-title class_">NGHTTP2</span>_REFUSED_STREAM } = http2.<span class="hljs-property">constants</span>; 3353<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>); 3354 3355<span class="hljs-keyword">const</span> proxy = http2.<span class="hljs-title function_">createServer</span>(); 3356proxy.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> { 3357 <span class="hljs-keyword">if</span> (headers[<span class="hljs-string">':method'</span>] !== <span class="hljs-string">'CONNECT'</span>) { 3358 <span class="hljs-comment">// Only accept CONNECT requests</span> 3359 stream.<span class="hljs-title function_">close</span>(<span class="hljs-title class_">NGHTTP2</span>_REFUSED_STREAM); 3360 <span class="hljs-keyword">return</span>; 3361 } 3362 <span class="hljs-keyword">const</span> auth = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">`tcp://<span class="hljs-subst">${headers[<span class="hljs-string">':authority'</span>]}</span>`</span>); 3363 <span class="hljs-comment">// It's a very good idea to verify that hostname and port are</span> 3364 <span class="hljs-comment">// things this proxy should be connecting to.</span> 3365 <span class="hljs-keyword">const</span> socket = net.<span class="hljs-title function_">connect</span>(auth.<span class="hljs-property">port</span>, auth.<span class="hljs-property">hostname</span>, <span class="hljs-function">() =></span> { 3366 stream.<span class="hljs-title function_">respond</span>(); 3367 socket.<span class="hljs-title function_">pipe</span>(stream); 3368 stream.<span class="hljs-title function_">pipe</span>(socket); 3369 }); 3370 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">error</span>) =></span> { 3371 stream.<span class="hljs-title function_">close</span>(http2.<span class="hljs-property">constants</span>.<span class="hljs-property">NGHTTP2_CONNECT_ERROR</span>); 3372 }); 3373}); 3374 3375proxy.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8001</span>);</code> <button class="copy-button">copy</button></pre> 3376<p>An HTTP/2 CONNECT client:</p> 3377<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3378 3379<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost:8001'</span>); 3380 3381<span class="hljs-comment">// Must not specify the ':path' and ':scheme' headers</span> 3382<span class="hljs-comment">// for CONNECT requests or an error will be thrown.</span> 3383<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ 3384 <span class="hljs-string">':method'</span>: <span class="hljs-string">'CONNECT'</span>, 3385 <span class="hljs-string">':authority'</span>: <span class="hljs-string">'localhost:8000'</span>, 3386}); 3387 3388req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers</span>) =></span> { 3389 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[http2.<span class="hljs-property">constants</span>.<span class="hljs-property">HTTP2_HEADER_STATUS</span>]); 3390}); 3391<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>; 3392req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>); 3393req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> data += chunk); 3394req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 3395 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`The server says: <span class="hljs-subst">${data}</span>`</span>); 3396 client.<span class="hljs-title function_">close</span>(); 3397}); 3398req.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Jane'</span>);</code> <button class="copy-button">copy</button></pre> 3399<h4>The extended <code>CONNECT</code> protocol<span><a class="mark" href="#the-extended-connect-protocol" id="the-extended-connect-protocol">#</a></span><a aria-hidden="true" class="legacy" id="http2_the_extended_connect_protocol"></a></h4> 3400<p><a href="https://tools.ietf.org/html/rfc8441">RFC 8441</a> defines an "Extended CONNECT Protocol" extension to HTTP/2 that 3401may be used to bootstrap the use of an <code>Http2Stream</code> using the <code>CONNECT</code> 3402method as a tunnel for other communication protocols (such as WebSockets).</p> 3403<p>The use of the Extended CONNECT Protocol is enabled by HTTP/2 servers by using 3404the <code>enableConnectProtocol</code> setting:</p> 3405<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3406<span class="hljs-keyword">const</span> settings = { <span class="hljs-attr">enableConnectProtocol</span>: <span class="hljs-literal">true</span> }; 3407<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>({ settings });</code> <button class="copy-button">copy</button></pre> 3408<p>Once the client receives the <code>SETTINGS</code> frame from the server indicating that 3409the extended CONNECT may be used, it may send <code>CONNECT</code> requests that use the 3410<code>':protocol'</code> HTTP/2 pseudo-header:</p> 3411<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3412<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost:8080'</span>); 3413client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> { 3414 <span class="hljs-keyword">if</span> (settings.<span class="hljs-property">enableConnectProtocol</span>) { 3415 <span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span class="hljs-string">':method'</span>: <span class="hljs-string">'CONNECT'</span>, <span class="hljs-string">':protocol'</span>: <span class="hljs-string">'foo'</span> }); 3416 <span class="hljs-comment">// ...</span> 3417 } 3418});</code> <button class="copy-button">copy</button></pre> 3419</section><section><h3>Compatibility API<span><a class="mark" href="#compatibility-api" id="compatibility-api">#</a></span><a aria-hidden="true" class="legacy" id="http2_compatibility_api"></a></h3> 3420<p>The Compatibility API has the goal of providing a similar developer experience 3421of HTTP/1 when using HTTP/2, making it possible to develop applications 3422that support both <a href="http.html">HTTP/1</a> and HTTP/2. This API targets only the 3423<strong>public API</strong> of the <a href="http.html">HTTP/1</a>. However many modules use internal 3424methods or state, and those <em>are not supported</em> as it is a completely 3425different implementation.</p> 3426<p>The following example creates an HTTP/2 server using the compatibility 3427API:</p> 3428<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3429<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 3430 res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>); 3431 res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>); 3432 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> }); 3433 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 3434});</code> <button class="copy-button">copy</button></pre> 3435<p>In order to create a mixed <a href="https.html">HTTPS</a> and HTTP/2 server, refer to the 3436<a href="#alpn-negotiation">ALPN negotiation</a> section. 3437Upgrading from non-tls HTTP/1 servers is not supported.</p> 3438<p>The HTTP/2 compatibility API is composed of <a href="#class-http2http2serverrequest"><code>Http2ServerRequest</code></a> and 3439<a href="#class-http2http2serverresponse"><code>Http2ServerResponse</code></a>. They aim at API compatibility with HTTP/1, but 3440they do not hide the differences between the protocols. As an example, 3441the status message for HTTP codes is ignored.</p> 3442<h4>ALPN negotiation<span><a class="mark" href="#alpn-negotiation" id="alpn-negotiation">#</a></span><a aria-hidden="true" class="legacy" id="http2_alpn_negotiation"></a></h4> 3443<p>ALPN negotiation allows supporting both <a href="https.html">HTTPS</a> and HTTP/2 over 3444the same socket. The <code>req</code> and <code>res</code> objects can be either HTTP/1 or 3445HTTP/2, and an application <strong>must</strong> restrict itself to the public API of 3446<a href="http.html">HTTP/1</a>, and detect if it is possible to use the more advanced 3447features of HTTP/2.</p> 3448<p>The following example creates a server that supports both protocols:</p> 3449<pre><code class="language-js"><span class="hljs-keyword">const</span> { createSecureServer } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3450<span class="hljs-keyword">const</span> { readFileSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 3451 3452<span class="hljs-keyword">const</span> cert = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'./cert.pem'</span>); 3453<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'./key.pem'</span>); 3454 3455<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureServer</span>( 3456 { cert, key, <span class="hljs-attr">allowHTTP1</span>: <span class="hljs-literal">true</span> }, 3457 onRequest, 3458).<span class="hljs-title function_">listen</span>(<span class="hljs-number">4443</span>); 3459 3460<span class="hljs-keyword">function</span> <span class="hljs-title function_">onRequest</span>(<span class="hljs-params">req, res</span>) { 3461 <span class="hljs-comment">// Detects if it is a HTTPS request or HTTP/2</span> 3462 <span class="hljs-keyword">const</span> { <span class="hljs-attr">socket</span>: { alpnProtocol } } = req.<span class="hljs-property">httpVersion</span> === <span class="hljs-string">'2.0'</span> ? 3463 req.<span class="hljs-property">stream</span>.<span class="hljs-property">session</span> : req; 3464 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'application/json'</span> }); 3465 res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 3466 alpnProtocol, 3467 <span class="hljs-attr">httpVersion</span>: req.<span class="hljs-property">httpVersion</span>, 3468 })); 3469}</code> <button class="copy-button">copy</button></pre> 3470<p>The <code>'request'</code> event works identically on both <a href="https.html">HTTPS</a> and 3471HTTP/2.</p> 3472<h4>Class: <code>http2.Http2ServerRequest</code><span><a class="mark" href="#class-http2http2serverrequest" id="class-http2http2serverrequest">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2_http2serverrequest"></a></h4> 3473<div class="api_metadata"> 3474<span>Added in: v8.4.0</span> 3475</div> 3476<ul> 3477<li>Extends: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li> 3478</ul> 3479<p>A <code>Http2ServerRequest</code> object is created by <a href="#class-http2server"><code>http2.Server</code></a> or 3480<a href="#class-http2secureserver"><code>http2.SecureServer</code></a> and passed as the first argument to the 3481<a href="#event-request"><code>'request'</code></a> event. It may be used to access a request status, headers, and 3482data.</p> 3483<h5>Event: <code>'aborted'</code><span><a class="mark" href="#event-aborted_1" id="event-aborted_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_aborted_1"></a></h5> 3484<div class="api_metadata"> 3485<span>Added in: v8.4.0</span> 3486</div> 3487<p>The <code>'aborted'</code> event is emitted whenever a <code>Http2ServerRequest</code> instance is 3488abnormally aborted in mid-communication.</p> 3489<p>The <code>'aborted'</code> event will only be emitted if the <code>Http2ServerRequest</code> writable 3490side has not been ended.</p> 3491<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_2" id="event-close_2">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_close_2"></a></h5> 3492<div class="api_metadata"> 3493<span>Added in: v8.4.0</span> 3494</div> 3495<p>Indicates that the underlying <a href="#class-http2stream"><code>Http2Stream</code></a> was closed. 3496Just like <code>'end'</code>, this event occurs only once per response.</p> 3497<h5><code>request.aborted</code><span><a class="mark" href="#requestaborted" id="requestaborted">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_aborted"></a></h5> 3498<div class="api_metadata"> 3499<span>Added in: v10.1.0</span> 3500</div> 3501<ul> 3502<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3503</ul> 3504<p>The <code>request.aborted</code> property will be <code>true</code> if the request has 3505been aborted.</p> 3506<h5><code>request.authority</code><span><a class="mark" href="#requestauthority" id="requestauthority">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_authority"></a></h5> 3507<div class="api_metadata"> 3508<span>Added in: v8.4.0</span> 3509</div> 3510<ul> 3511<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3512</ul> 3513<p>The request authority pseudo header field. Because HTTP/2 allows requests 3514to set either <code>:authority</code> or <code>host</code>, this value is derived from 3515<code>req.headers[':authority']</code> if present. Otherwise, it is derived from 3516<code>req.headers['host']</code>.</p> 3517<h5><code>request.complete</code><span><a class="mark" href="#requestcomplete" id="requestcomplete">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_complete"></a></h5> 3518<div class="api_metadata"> 3519<span>Added in: v12.10.0</span> 3520</div> 3521<ul> 3522<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3523</ul> 3524<p>The <code>request.complete</code> property will be <code>true</code> if the request has 3525been completed, aborted, or destroyed.</p> 3526<h5><code>request.connection</code><span><a class="mark" href="#requestconnection" id="requestconnection">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_connection"></a></h5> 3527<div class="api_metadata"> 3528<span>Added in: v8.4.0</span><span>Deprecated since: v13.0.0</span> 3529</div> 3530<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated. Use <a href="#requestsocket"><code>request.socket</code></a>.</div><p></p> 3531<ul> 3532<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type"><tls.TLSSocket></a></li> 3533</ul> 3534<p>See <a href="#requestsocket"><code>request.socket</code></a>.</p> 3535<h5><code>request.destroy([error])</code><span><a class="mark" href="#requestdestroyerror" id="requestdestroyerror">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_destroy_error"></a></h5> 3536<div class="api_metadata"> 3537<span>Added in: v8.4.0</span> 3538</div> 3539<ul> 3540<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3541</ul> 3542<p>Calls <code>destroy()</code> on the <a href="#class-http2stream"><code>Http2Stream</code></a> that received 3543the <a href="#class-http2http2serverrequest"><code>Http2ServerRequest</code></a>. If <code>error</code> is provided, an <code>'error'</code> event 3544is emitted and <code>error</code> is passed as an argument to any listeners on the event.</p> 3545<p>It does nothing if the stream was already destroyed.</p> 3546<h5><code>request.headers</code><span><a class="mark" href="#requestheaders" id="requestheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_headers"></a></h5> 3547<div class="api_metadata"> 3548<span>Added in: v8.4.0</span> 3549</div> 3550<ul> 3551<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 3552</ul> 3553<p>The request/response headers object.</p> 3554<p>Key-value pairs of header names and values. Header names are lower-cased.</p> 3555<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span> 3556<span class="hljs-comment">//</span> 3557<span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span> 3558<span class="hljs-comment">// host: '127.0.0.1:8000',</span> 3559<span class="hljs-comment">// accept: '*/*' }</span> 3560<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(request.<span class="hljs-property">headers</span>);</code> <button class="copy-button">copy</button></pre> 3561<p>See <a href="#headers-object">HTTP/2 Headers Object</a>.</p> 3562<p>In HTTP/2, the request path, host name, protocol, and method are represented as 3563special headers prefixed with the <code>:</code> character (e.g. <code>':path'</code>). These special 3564headers will be included in the <code>request.headers</code> object. Care must be taken not 3565to inadvertently modify these special headers or errors may occur. For instance, 3566removing all headers from the request will cause errors to occur:</p> 3567<pre><code class="language-js"><span class="hljs-title function_">removeAllHeaders</span>(request.<span class="hljs-property">headers</span>); 3568<span class="hljs-title function_">assert</span>(request.<span class="hljs-property">url</span>); <span class="hljs-comment">// Fails because the :path header has been removed</span></code> <button class="copy-button">copy</button></pre> 3569<h5><code>request.httpVersion</code><span><a class="mark" href="#requesthttpversion" id="requesthttpversion">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_httpversion"></a></h5> 3570<div class="api_metadata"> 3571<span>Added in: v8.4.0</span> 3572</div> 3573<ul> 3574<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3575</ul> 3576<p>In case of server request, the HTTP version sent by the client. In the case of 3577client response, the HTTP version of the connected-to server. Returns 3578<code>'2.0'</code>.</p> 3579<p>Also <code>message.httpVersionMajor</code> is the first integer and 3580<code>message.httpVersionMinor</code> is the second.</p> 3581<h5><code>request.method</code><span><a class="mark" href="#requestmethod" id="requestmethod">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_method"></a></h5> 3582<div class="api_metadata"> 3583<span>Added in: v8.4.0</span> 3584</div> 3585<ul> 3586<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3587</ul> 3588<p>The request method as a string. Read-only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.</p> 3589<h5><code>request.rawHeaders</code><span><a class="mark" href="#requestrawheaders" id="requestrawheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_rawheaders"></a></h5> 3590<div class="api_metadata"> 3591<span>Added in: v8.4.0</span> 3592</div> 3593<ul> 3594<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 3595</ul> 3596<p>The raw request/response headers list exactly as they were received.</p> 3597<p>The keys and values are in the same list. It is <em>not</em> a 3598list of tuples. So, the even-numbered offsets are key values, and the 3599odd-numbered offsets are the associated values.</p> 3600<p>Header names are not lowercased, and duplicates are not merged.</p> 3601<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span> 3602<span class="hljs-comment">//</span> 3603<span class="hljs-comment">// [ 'user-agent',</span> 3604<span class="hljs-comment">// 'this is invalid because there can be only one',</span> 3605<span class="hljs-comment">// 'User-Agent',</span> 3606<span class="hljs-comment">// 'curl/7.22.0',</span> 3607<span class="hljs-comment">// 'Host',</span> 3608<span class="hljs-comment">// '127.0.0.1:8000',</span> 3609<span class="hljs-comment">// 'ACCEPT',</span> 3610<span class="hljs-comment">// '*/*' ]</span> 3611<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(request.<span class="hljs-property">rawHeaders</span>);</code> <button class="copy-button">copy</button></pre> 3612<h5><code>request.rawTrailers</code><span><a class="mark" href="#requestrawtrailers" id="requestrawtrailers">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_rawtrailers"></a></h5> 3613<div class="api_metadata"> 3614<span>Added in: v8.4.0</span> 3615</div> 3616<ul> 3617<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 3618</ul> 3619<p>The raw request/response trailer keys and values exactly as they were 3620received. Only populated at the <code>'end'</code> event.</p> 3621<h5><code>request.scheme</code><span><a class="mark" href="#requestscheme" id="requestscheme">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_scheme"></a></h5> 3622<div class="api_metadata"> 3623<span>Added in: v8.4.0</span> 3624</div> 3625<ul> 3626<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3627</ul> 3628<p>The request scheme pseudo header field indicating the scheme 3629portion of the target URL.</p> 3630<h5><code>request.setTimeout(msecs, callback)</code><span><a class="mark" href="#requestsettimeoutmsecs-callback" id="requestsettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_settimeout_msecs_callback"></a></h5> 3631<div class="api_metadata"> 3632<span>Added in: v8.4.0</span> 3633</div> 3634<ul> 3635<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 3636<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 3637<li>Returns: <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li> 3638</ul> 3639<p>Sets the <a href="#class-http2stream"><code>Http2Stream</code></a>'s timeout value to <code>msecs</code>. If a callback is 3640provided, then it is added as a listener on the <code>'timeout'</code> event on 3641the response object.</p> 3642<p>If no <code>'timeout'</code> listener is added to the request, the response, or 3643the server, then <a href="#class-http2stream"><code>Http2Stream</code></a>s are destroyed when they time out. If a 3644handler is assigned to the request, the response, or the server's <code>'timeout'</code> 3645events, timed out sockets must be handled explicitly.</p> 3646<h5><code>request.socket</code><span><a class="mark" href="#requestsocket" id="requestsocket">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_socket"></a></h5> 3647<div class="api_metadata"> 3648<span>Added in: v8.4.0</span> 3649</div> 3650<ul> 3651<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type"><tls.TLSSocket></a></li> 3652</ul> 3653<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but 3654applies getters, setters, and methods based on HTTP/2 logic.</p> 3655<p><code>destroyed</code>, <code>readable</code>, and <code>writable</code> properties will be retrieved from and 3656set on <code>request.stream</code>.</p> 3657<p><code>destroy</code>, <code>emit</code>, <code>end</code>, <code>on</code> and <code>once</code> methods will be called on 3658<code>request.stream</code>.</p> 3659<p><code>setTimeout</code> method will be called on <code>request.stream.session</code>.</p> 3660<p><code>pause</code>, <code>read</code>, <code>resume</code>, and <code>write</code> will throw an error with code 3661<code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See <a href="#http2session-and-sockets"><code>Http2Session</code> and Sockets</a> for 3662more information.</p> 3663<p>All other interactions will be routed directly to the socket. With TLS support, 3664use <a href="tls.html#tlssocketgetpeercertificatedetailed"><code>request.socket.getPeerCertificate()</code></a> to obtain the client's 3665authentication details.</p> 3666<h5><code>request.stream</code><span><a class="mark" href="#requeststream" id="requeststream">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_stream"></a></h5> 3667<div class="api_metadata"> 3668<span>Added in: v8.4.0</span> 3669</div> 3670<ul> 3671<li><a href="http2.html#class-http2stream" class="type"><Http2Stream></a></li> 3672</ul> 3673<p>The <a href="#class-http2stream"><code>Http2Stream</code></a> object backing the request.</p> 3674<h5><code>request.trailers</code><span><a class="mark" href="#requesttrailers" id="requesttrailers">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_trailers"></a></h5> 3675<div class="api_metadata"> 3676<span>Added in: v8.4.0</span> 3677</div> 3678<ul> 3679<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 3680</ul> 3681<p>The request/response trailers object. Only populated at the <code>'end'</code> event.</p> 3682<h5><code>request.url</code><span><a class="mark" href="#requesturl" id="requesturl">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_url"></a></h5> 3683<div class="api_metadata"> 3684<span>Added in: v8.4.0</span> 3685</div> 3686<ul> 3687<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3688</ul> 3689<p>Request URL string. This contains only the URL that is present in the actual 3690HTTP request. If the request is:</p> 3691<pre><code class="language-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/status?name=ryan</span> <span class="hljs-meta">HTTP/1.1</span> 3692<span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code> <button class="copy-button">copy</button></pre> 3693<p>Then <code>request.url</code> will be:</p> 3694<!-- eslint-disable semi --> 3695<pre><code class="language-js"><span class="hljs-string">'/status?name=ryan'</span></code> <button class="copy-button">copy</button></pre> 3696<p>To parse the url into its parts, <code>new URL()</code> can be used:</p> 3697<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node</span> 3698<span class="hljs-meta prompt_">> </span><span class="language-bash">new URL(<span class="hljs-string">'/status?name=ryan'</span>, <span class="hljs-string">'http://example.com'</span>)</span> 3699URL { 3700 href: 'http://example.com/status?name=ryan', 3701 origin: 'http://example.com', 3702 protocol: 'http:', 3703 username: '', 3704 password: '', 3705 host: 'example.com', 3706 hostname: 'example.com', 3707 port: '', 3708 pathname: '/status', 3709 search: '?name=ryan', 3710 searchParams: URLSearchParams { 'name' => 'ryan' }, 3711 hash: '' 3712}</code> <button class="copy-button">copy</button></pre> 3713<h4>Class: <code>http2.Http2ServerResponse</code><span><a class="mark" href="#class-http2http2serverresponse" id="class-http2http2serverresponse">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2_http2serverresponse"></a></h4> 3714<div class="api_metadata"> 3715<span>Added in: v8.4.0</span> 3716</div> 3717<ul> 3718<li>Extends: <a href="stream.html#stream" class="type"><Stream></a></li> 3719</ul> 3720<p>This object is created internally by an HTTP server, not by the user. It is 3721passed as the second parameter to the <a href="#event-request"><code>'request'</code></a> event.</p> 3722<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_3" id="event-close_3">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_close_3"></a></h5> 3723<div class="api_metadata"> 3724<span>Added in: v8.4.0</span> 3725</div> 3726<p>Indicates that the underlying <a href="#class-http2stream"><code>Http2Stream</code></a> was terminated before 3727<a href="#responseenddata-encoding-callback"><code>response.end()</code></a> was called or able to flush.</p> 3728<h5>Event: <code>'finish'</code><span><a class="mark" href="#event-finish" id="event-finish">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_finish"></a></h5> 3729<div class="api_metadata"> 3730<span>Added in: v8.4.0</span> 3731</div> 3732<p>Emitted when the response has been sent. More specifically, this event is 3733emitted when the last segment of the response headers and body have been 3734handed off to the HTTP/2 multiplexing for transmission over the network. It 3735does not imply that the client has received anything yet.</p> 3736<p>After this event, no more events will be emitted on the response object.</p> 3737<h5><code>response.addTrailers(headers)</code><span><a class="mark" href="#responseaddtrailersheaders" id="responseaddtrailersheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_addtrailers_headers"></a></h5> 3738<div class="api_metadata"> 3739<span>Added in: v8.4.0</span> 3740</div> 3741<ul> 3742<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 3743</ul> 3744<p>This method adds HTTP trailing headers (a header but at the end of the 3745message) to the response.</p> 3746<p>Attempting to set a header field name or value that contains invalid characters 3747will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p> 3748<h5><code>response.connection</code><span><a class="mark" href="#responseconnection" id="responseconnection">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_connection"></a></h5> 3749<div class="api_metadata"> 3750<span>Added in: v8.4.0</span><span>Deprecated since: v13.0.0</span> 3751</div> 3752<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated. Use <a href="#responsesocket"><code>response.socket</code></a>.</div><p></p> 3753<ul> 3754<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type"><tls.TLSSocket></a></li> 3755</ul> 3756<p>See <a href="#responsesocket"><code>response.socket</code></a>.</p> 3757<h5><code>response.createPushResponse(headers, callback)</code><span><a class="mark" href="#responsecreatepushresponseheaders-callback" id="responsecreatepushresponseheaders-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_createpushresponse_headers_callback"></a></h5> 3758<div class="api_metadata"> 3759<details class="changelog"><summary>History</summary> 3760<table> 3761<tbody><tr><th>Version</th><th>Changes</th></tr> 3762<tr><td>v18.0.0</td> 3763<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3764<tr><td>v8.4.0</td> 3765<td><p><span>Added in: v8.4.0</span></p></td></tr> 3766</tbody></table> 3767</details> 3768</div> 3769<ul> 3770<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li> 3771<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Called once <code>http2stream.pushStream()</code> is finished, 3772or either when the attempt to create the pushed <code>Http2Stream</code> has failed or 3773has been rejected, or the state of <code>Http2ServerRequest</code> is closed prior to 3774calling the <code>http2stream.pushStream()</code> method 3775<ul> 3776<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3777<li><code>res</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a> The newly-created <code>Http2ServerResponse</code> 3778object</li> 3779</ul> 3780</li> 3781</ul> 3782<p>Call <a href="#http2streampushstreamheaders-options-callback"><code>http2stream.pushStream()</code></a> with the given headers, and wrap the 3783given <a href="#class-http2stream"><code>Http2Stream</code></a> on a newly created <code>Http2ServerResponse</code> as the callback 3784parameter if successful. When <code>Http2ServerRequest</code> is closed, the callback is 3785called with an error <code>ERR_HTTP2_INVALID_STREAM</code>.</p> 3786<h5><code>response.end([data[, encoding]][, callback])</code><span><a class="mark" href="#responseenddata-encoding-callback" id="responseenddata-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_end_data_encoding_callback"></a></h5> 3787<div class="api_metadata"> 3788<details class="changelog"><summary>History</summary> 3789<table> 3790<tbody><tr><th>Version</th><th>Changes</th></tr> 3791<tr><td>v10.0.0</td> 3792<td><p>This method now returns a reference to <code>ServerResponse</code>.</p></td></tr> 3793<tr><td>v8.4.0</td> 3794<td><p><span>Added in: v8.4.0</span></p></td></tr> 3795</tbody></table> 3796</details> 3797</div> 3798<ul> 3799<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> 3800<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3801<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 3802<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 3803</ul> 3804<p>This method signals to the server that all of the response headers and body 3805have been sent; that server should consider this message complete. 3806The method, <code>response.end()</code>, MUST be called on each response.</p> 3807<p>If <code>data</code> is specified, it is equivalent to calling 3808<a href="http.html#responsewritechunk-encoding-callback"><code>response.write(data, encoding)</code></a> followed by <code>response.end(callback)</code>.</p> 3809<p>If <code>callback</code> is specified, it will be called when the response stream 3810is finished.</p> 3811<h5><code>response.finished</code><span><a class="mark" href="#responsefinished" id="responsefinished">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_finished"></a></h5> 3812<div class="api_metadata"> 3813<span>Added in: v8.4.0</span><span>Deprecated since: v13.4.0, v12.16.0</span> 3814</div> 3815<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated. Use <a href="#responsewritableended"><code>response.writableEnded</code></a>.</div><p></p> 3816<ul> 3817<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3818</ul> 3819<p>Boolean value that indicates whether the response has completed. Starts 3820as <code>false</code>. After <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> executes, the value will be <code>true</code>.</p> 3821<h5><code>response.getHeader(name)</code><span><a class="mark" href="#responsegetheadername" id="responsegetheadername">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_getheader_name"></a></h5> 3822<div class="api_metadata"> 3823<span>Added in: v8.4.0</span> 3824</div> 3825<ul> 3826<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3827<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3828</ul> 3829<p>Reads out a header that has already been queued but not sent to the client. 3830The name is case-insensitive.</p> 3831<pre><code class="language-js"><span class="hljs-keyword">const</span> contentType = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'content-type'</span>);</code> <button class="copy-button">copy</button></pre> 3832<h5><code>response.getHeaderNames()</code><span><a class="mark" href="#responsegetheadernames" id="responsegetheadernames">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_getheadernames"></a></h5> 3833<div class="api_metadata"> 3834<span>Added in: v8.4.0</span> 3835</div> 3836<ul> 3837<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 3838</ul> 3839<p>Returns an array containing the unique names of the current outgoing headers. 3840All header names are lowercase.</p> 3841<pre><code class="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Foo'</span>, <span class="hljs-string">'bar'</span>); 3842response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class="hljs-string">'foo=bar'</span>, <span class="hljs-string">'bar=baz'</span>]); 3843 3844<span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">getHeaderNames</span>(); 3845<span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code> <button class="copy-button">copy</button></pre> 3846<h5><code>response.getHeaders()</code><span><a class="mark" href="#responsegetheaders" id="responsegetheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_getheaders"></a></h5> 3847<div class="api_metadata"> 3848<span>Added in: v8.4.0</span> 3849</div> 3850<ul> 3851<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 3852</ul> 3853<p>Returns a shallow copy of the current outgoing headers. Since a shallow copy 3854is used, array values may be mutated without additional calls to various 3855header-related http module methods. The keys of the returned object are the 3856header names and the values are the respective header values. All header names 3857are lowercase.</p> 3858<p>The object returned by the <code>response.getHeaders()</code> method <em>does not</em> 3859prototypically inherit from the JavaScript <code>Object</code>. This means that typical 3860<code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, and others 3861are not defined and <em>will not work</em>.</p> 3862<pre><code class="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Foo'</span>, <span class="hljs-string">'bar'</span>); 3863response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class="hljs-string">'foo=bar'</span>, <span class="hljs-string">'bar=baz'</span>]); 3864 3865<span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHeaders</span>(); 3866<span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</span></code> <button class="copy-button">copy</button></pre> 3867<h5><code>response.hasHeader(name)</code><span><a class="mark" href="#responsehasheadername" id="responsehasheadername">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_hasheader_name"></a></h5> 3868<div class="api_metadata"> 3869<span>Added in: v8.4.0</span> 3870</div> 3871<ul> 3872<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3873<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3874</ul> 3875<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the 3876outgoing headers. The header name matching is case-insensitive.</p> 3877<pre><code class="language-js"><span class="hljs-keyword">const</span> hasContentType = response.<span class="hljs-title function_">hasHeader</span>(<span class="hljs-string">'content-type'</span>);</code> <button class="copy-button">copy</button></pre> 3878<h5><code>response.headersSent</code><span><a class="mark" href="#responseheaderssent" id="responseheaderssent">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_headerssent"></a></h5> 3879<div class="api_metadata"> 3880<span>Added in: v8.4.0</span> 3881</div> 3882<ul> 3883<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3884</ul> 3885<p>True if headers were sent, false otherwise (read-only).</p> 3886<h5><code>response.removeHeader(name)</code><span><a class="mark" href="#responseremoveheadername" id="responseremoveheadername">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_removeheader_name"></a></h5> 3887<div class="api_metadata"> 3888<span>Added in: v8.4.0</span> 3889</div> 3890<ul> 3891<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3892</ul> 3893<p>Removes a header that has been queued for implicit sending.</p> 3894<pre><code class="language-js">response.<span class="hljs-title function_">removeHeader</span>(<span class="hljs-string">'Content-Encoding'</span>);</code> <button class="copy-button">copy</button></pre> 3895<h5><code>response.req</code><span><a class="mark" href="#responsereq" id="responsereq">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_req"></a></h5> 3896<div class="api_metadata"> 3897<span>Added in: v15.7.0</span> 3898</div> 3899<ul> 3900<li><a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li> 3901</ul> 3902<p>A reference to the original HTTP2 <code>request</code> object.</p> 3903<h5><code>response.sendDate</code><span><a class="mark" href="#responsesenddate" id="responsesenddate">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_senddate"></a></h5> 3904<div class="api_metadata"> 3905<span>Added in: v8.4.0</span> 3906</div> 3907<ul> 3908<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3909</ul> 3910<p>When true, the Date header will be automatically generated and sent in 3911the response if it is not already present in the headers. Defaults to true.</p> 3912<p>This should only be disabled for testing; HTTP requires the Date header 3913in responses.</p> 3914<h5><code>response.setHeader(name, value)</code><span><a class="mark" href="#responsesetheadername-value" id="responsesetheadername-value">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_setheader_name_value"></a></h5> 3915<div class="api_metadata"> 3916<span>Added in: v8.4.0</span> 3917</div> 3918<ul> 3919<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3920<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 3921</ul> 3922<p>Sets a single header value for implicit headers. If this header already exists 3923in the to-be-sent headers, its value will be replaced. Use an array of strings 3924here to send multiple headers with the same name.</p> 3925<pre><code class="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html; charset=utf-8'</span>);</code> <button class="copy-button">copy</button></pre> 3926<p>or</p> 3927<pre><code class="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class="hljs-string">'type=ninja'</span>, <span class="hljs-string">'language=javascript'</span>]);</code> <button class="copy-button">copy</button></pre> 3928<p>Attempting to set a header field name or value that contains invalid characters 3929will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p> 3930<p>When headers have been set with <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>, they will be merged 3931with any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, with the headers passed 3932to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p> 3933<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span> 3934<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 3935 res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html; charset=utf-8'</span>); 3936 res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>); 3937 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> }); 3938 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 3939});</code> <button class="copy-button">copy</button></pre> 3940<h5><code>response.setTimeout(msecs[, callback])</code><span><a class="mark" href="#responsesettimeoutmsecs-callback" id="responsesettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_settimeout_msecs_callback"></a></h5> 3941<div class="api_metadata"> 3942<span>Added in: v8.4.0</span> 3943</div> 3944<ul> 3945<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 3946<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 3947<li>Returns: <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li> 3948</ul> 3949<p>Sets the <a href="#class-http2stream"><code>Http2Stream</code></a>'s timeout value to <code>msecs</code>. If a callback is 3950provided, then it is added as a listener on the <code>'timeout'</code> event on 3951the response object.</p> 3952<p>If no <code>'timeout'</code> listener is added to the request, the response, or 3953the server, then <a href="#class-http2stream"><code>Http2Stream</code></a>s are destroyed when they time out. If a 3954handler is assigned to the request, the response, or the server's <code>'timeout'</code> 3955events, timed out sockets must be handled explicitly.</p> 3956<h5><code>response.socket</code><span><a class="mark" href="#responsesocket" id="responsesocket">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_socket"></a></h5> 3957<div class="api_metadata"> 3958<span>Added in: v8.4.0</span> 3959</div> 3960<ul> 3961<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type"><tls.TLSSocket></a></li> 3962</ul> 3963<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but 3964applies getters, setters, and methods based on HTTP/2 logic.</p> 3965<p><code>destroyed</code>, <code>readable</code>, and <code>writable</code> properties will be retrieved from and 3966set on <code>response.stream</code>.</p> 3967<p><code>destroy</code>, <code>emit</code>, <code>end</code>, <code>on</code> and <code>once</code> methods will be called on 3968<code>response.stream</code>.</p> 3969<p><code>setTimeout</code> method will be called on <code>response.stream.session</code>.</p> 3970<p><code>pause</code>, <code>read</code>, <code>resume</code>, and <code>write</code> will throw an error with code 3971<code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See <a href="#http2session-and-sockets"><code>Http2Session</code> and Sockets</a> for 3972more information.</p> 3973<p>All other interactions will be routed directly to the socket.</p> 3974<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>); 3975<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 3976 <span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>; 3977 <span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">remotePort</span>; 3978 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">`Your IP address is <span class="hljs-subst">${ip}</span> and your source port is <span class="hljs-subst">${port}</span>.`</span>); 3979}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code> <button class="copy-button">copy</button></pre> 3980<h5><code>response.statusCode</code><span><a class="mark" href="#responsestatuscode" id="responsestatuscode">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_statuscode"></a></h5> 3981<div class="api_metadata"> 3982<span>Added in: v8.4.0</span> 3983</div> 3984<ul> 3985<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 3986</ul> 3987<p>When using implicit headers (not calling <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> explicitly), 3988this property controls the status code that will be sent to the client when 3989the headers get flushed.</p> 3990<pre><code class="language-js">response.<span class="hljs-property">statusCode</span> = <span class="hljs-number">404</span>;</code> <button class="copy-button">copy</button></pre> 3991<p>After response header was sent to the client, this property indicates the 3992status code which was sent out.</p> 3993<h5><code>response.statusMessage</code><span><a class="mark" href="#responsestatusmessage" id="responsestatusmessage">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_statusmessage"></a></h5> 3994<div class="api_metadata"> 3995<span>Added in: v8.4.0</span> 3996</div> 3997<ul> 3998<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3999</ul> 4000<p>Status message is not supported by HTTP/2 (RFC 7540 8.1.2.4). It returns 4001an empty string.</p> 4002<h5><code>response.stream</code><span><a class="mark" href="#responsestream" id="responsestream">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_stream"></a></h5> 4003<div class="api_metadata"> 4004<span>Added in: v8.4.0</span> 4005</div> 4006<ul> 4007<li><a href="http2.html#class-http2stream" class="type"><Http2Stream></a></li> 4008</ul> 4009<p>The <a href="#class-http2stream"><code>Http2Stream</code></a> object backing the response.</p> 4010<h5><code>response.writableEnded</code><span><a class="mark" href="#responsewritableended" id="responsewritableended">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_writableended"></a></h5> 4011<div class="api_metadata"> 4012<span>Added in: v12.9.0</span> 4013</div> 4014<ul> 4015<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 4016</ul> 4017<p>Is <code>true</code> after <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> has been called. This property 4018does not indicate whether the data has been flushed, for this use 4019<a href="stream.html#writablewritablefinished"><code>writable.writableFinished</code></a> instead.</p> 4020<h5><code>response.write(chunk[, encoding][, callback])</code><span><a class="mark" href="#responsewritechunk-encoding-callback" id="responsewritechunk-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_write_chunk_encoding_callback"></a></h5> 4021<div class="api_metadata"> 4022<span>Added in: v8.4.0</span> 4023</div> 4024<ul> 4025<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> 4026<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 4027<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 4028<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 4029</ul> 4030<p>If this method is called and <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> has not been called, 4031it will switch to implicit header mode and flush the implicit headers.</p> 4032<p>This sends a chunk of the response body. This method may 4033be called multiple times to provide successive parts of the body.</p> 4034<p>In the <code>node:http</code> module, the response body is omitted when the 4035request is a HEAD request. Similarly, the <code>204</code> and <code>304</code> responses 4036<em>must not</em> include a message body.</p> 4037<p><code>chunk</code> can be a string or a buffer. If <code>chunk</code> is a string, 4038the second parameter specifies how to encode it into a byte stream. 4039By default the <code>encoding</code> is <code>'utf8'</code>. <code>callback</code> will be called when this chunk 4040of data is flushed.</p> 4041<p>This is the raw HTTP body and has nothing to do with higher-level multi-part 4042body encodings that may be used.</p> 4043<p>The first time <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> is called, it will send the buffered 4044header information and the first chunk of the body to the client. The second 4045time <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> is called, Node.js assumes data will be streamed, 4046and sends the new data separately. That is, the response is buffered up to the 4047first chunk of the body.</p> 4048<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel 4049buffer. Returns <code>false</code> if all or part of the data was queued in user memory. 4050<code>'drain'</code> will be emitted when the buffer is free again.</p> 4051<h5><code>response.writeContinue()</code><span><a class="mark" href="#responsewritecontinue" id="responsewritecontinue">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_writecontinue"></a></h5> 4052<div class="api_metadata"> 4053<span>Added in: v8.4.0</span> 4054</div> 4055<p>Sends a status <code>100 Continue</code> to the client, indicating that the request body 4056should be sent. See the <a href="#event-checkcontinue"><code>'checkContinue'</code></a> event on <code>Http2Server</code> and 4057<code>Http2SecureServer</code>.</p> 4058<h5><code>response.writeEarlyHints(hints)</code><span><a class="mark" href="#responsewriteearlyhintshints" id="responsewriteearlyhintshints">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_writeearlyhints_hints"></a></h5> 4059<div class="api_metadata"> 4060<span>Added in: v18.11.0</span> 4061</div> 4062<ul> 4063<li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 4064</ul> 4065<p>Sends a status <code>103 Early Hints</code> to the client with a Link header, 4066indicating that the user agent can preload/preconnect the linked resources. 4067The <code>hints</code> is an object containing the values of headers to be sent with 4068early hints message.</p> 4069<p><strong>Example</strong></p> 4070<pre><code class="language-js"><span class="hljs-keyword">const</span> earlyHintsLink = <span class="hljs-string">'</styles.css>; rel=preload; as=style'</span>; 4071response.<span class="hljs-title function_">writeEarlyHints</span>({ 4072 <span class="hljs-string">'link'</span>: earlyHintsLink, 4073}); 4074 4075<span class="hljs-keyword">const</span> earlyHintsLinks = [ 4076 <span class="hljs-string">'</styles.css>; rel=preload; as=style'</span>, 4077 <span class="hljs-string">'</scripts.js>; rel=preload; as=script'</span>, 4078]; 4079response.<span class="hljs-title function_">writeEarlyHints</span>({ 4080 <span class="hljs-string">'link'</span>: earlyHintsLinks, 4081});</code> <button class="copy-button">copy</button></pre> 4082<h5><code>response.writeHead(statusCode[, statusMessage][, headers])</code><span><a class="mark" href="#responsewriteheadstatuscode-statusmessage-headers" id="responsewriteheadstatuscode-statusmessage-headers">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_writehead_statuscode_statusmessage_headers"></a></h5> 4083<div class="api_metadata"> 4084<details class="changelog"><summary>History</summary> 4085<table> 4086<tbody><tr><th>Version</th><th>Changes</th></tr> 4087<tr><td>v11.10.0, v10.17.0</td> 4088<td><p>Return <code>this</code> from <code>writeHead()</code> to allow chaining with <code>end()</code>.</p></td></tr> 4089<tr><td>v8.4.0</td> 4090<td><p><span>Added in: v8.4.0</span></p></td></tr> 4091</tbody></table> 4092</details> 4093</div> 4094<ul> 4095<li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 4096<li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 4097<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a></li> 4098<li>Returns: <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li> 4099</ul> 4100<p>Sends a response header to the request. The status code is a 3-digit HTTP 4101status code, like <code>404</code>. The last argument, <code>headers</code>, are the response headers.</p> 4102<p>Returns a reference to the <code>Http2ServerResponse</code>, so that calls can be chained.</p> 4103<p>For compatibility with <a href="http.html">HTTP/1</a>, a human-readable <code>statusMessage</code> may be 4104passed as the second argument. However, because the <code>statusMessage</code> has no 4105meaning within HTTP/2, the argument will have no effect and a process warning 4106will be emitted.</p> 4107<pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-string">'hello world'</span>; 4108response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { 4109 <span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(body), 4110 <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>, 4111});</code> <button class="copy-button">copy</button></pre> 4112<p><code>Content-Length</code> is given in bytes not characters. The 4113<code>Buffer.byteLength()</code> API may be used to determine the number of bytes in a 4114given encoding. On outbound messages, Node.js does not check if Content-Length 4115and the length of the body being transmitted are equal or not. However, when 4116receiving messages, Node.js will automatically reject messages when the 4117<code>Content-Length</code> does not match the actual payload size.</p> 4118<p>This method may be called at most one time on a message before 4119<a href="#responseenddata-encoding-callback"><code>response.end()</code></a> is called.</p> 4120<p>If <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> or <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> are called before calling 4121this, the implicit/mutable headers will be calculated and call this function.</p> 4122<p>When headers have been set with <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>, they will be merged 4123with any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, with the headers passed 4124to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p> 4125<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span> 4126<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 4127 res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html; charset=utf-8'</span>); 4128 res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>); 4129 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> }); 4130 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 4131});</code> <button class="copy-button">copy</button></pre> 4132<p>Attempting to set a header field name or value that contains invalid characters 4133will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p> 4134</section><section><h3>Collecting HTTP/2 performance metrics<span><a class="mark" href="#collecting-http2-performance-metrics" id="collecting-http2-performance-metrics">#</a></span><a aria-hidden="true" class="legacy" id="http2_collecting_http_2_performance_metrics"></a></h3> 4135<p>The <a href="perf_hooks.html">Performance Observer</a> API can be used to collect basic performance 4136metrics for each <code>Http2Session</code> and <code>Http2Stream</code> instance.</p> 4137<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PerformanceObserver</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:perf_hooks'</span>); 4138 4139<span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">items</span>) =></span> { 4140 <span class="hljs-keyword">const</span> entry = items.<span class="hljs-title function_">getEntries</span>()[<span class="hljs-number">0</span>]; 4141 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(entry.<span class="hljs-property">entryType</span>); <span class="hljs-comment">// prints 'http2'</span> 4142 <span class="hljs-keyword">if</span> (entry.<span class="hljs-property">name</span> === <span class="hljs-string">'Http2Session'</span>) { 4143 <span class="hljs-comment">// Entry contains statistics about the Http2Session</span> 4144 } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (entry.<span class="hljs-property">name</span> === <span class="hljs-string">'Http2Stream'</span>) { 4145 <span class="hljs-comment">// Entry contains statistics about the Http2Stream</span> 4146 } 4147}); 4148obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'http2'</span>] });</code> <button class="copy-button">copy</button></pre> 4149<p>The <code>entryType</code> property of the <code>PerformanceEntry</code> will be equal to <code>'http2'</code>.</p> 4150<p>The <code>name</code> property of the <code>PerformanceEntry</code> will be equal to either 4151<code>'Http2Stream'</code> or <code>'Http2Session'</code>.</p> 4152<p>If <code>name</code> is equal to <code>Http2Stream</code>, the <code>PerformanceEntry</code> will contain the 4153following additional properties:</p> 4154<ul> 4155<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of <code>DATA</code> frame bytes received for this 4156<code>Http2Stream</code>.</li> 4157<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of <code>DATA</code> frame bytes sent for this 4158<code>Http2Stream</code>.</li> 4159<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The identifier of the associated <code>Http2Stream</code></li> 4160<li><code>timeToFirstByte</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of milliseconds elapsed between the 4161<code>PerformanceEntry</code> <code>startTime</code> and the reception of the first <code>DATA</code> frame.</li> 4162<li><code>timeToFirstByteSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of milliseconds elapsed between 4163the <code>PerformanceEntry</code> <code>startTime</code> and sending of the first <code>DATA</code> frame.</li> 4164<li><code>timeToFirstHeader</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of milliseconds elapsed between the 4165<code>PerformanceEntry</code> <code>startTime</code> and the reception of the first header.</li> 4166</ul> 4167<p>If <code>name</code> is equal to <code>Http2Session</code>, the <code>PerformanceEntry</code> will contain the 4168following additional properties:</p> 4169<ul> 4170<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes received for this <code>Http2Session</code>.</li> 4171<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes sent for this <code>Http2Session</code>.</li> 4172<li><code>framesReceived</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of HTTP/2 frames received by the 4173<code>Http2Session</code>.</li> 4174<li><code>framesSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of HTTP/2 frames sent by the <code>Http2Session</code>.</li> 4175<li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The maximum number of streams concurrently 4176open during the lifetime of the <code>Http2Session</code>.</li> 4177<li><code>pingRTT</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of milliseconds elapsed since the transmission 4178of a <code>PING</code> frame and the reception of its acknowledgment. Only present if 4179a <code>PING</code> frame has been sent on the <code>Http2Session</code>.</li> 4180<li><code>streamAverageDuration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The average duration (in milliseconds) for 4181all <code>Http2Stream</code> instances.</li> 4182<li><code>streamCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of <code>Http2Stream</code> instances processed by 4183the <code>Http2Session</code>.</li> 4184<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Either <code>'server'</code> or <code>'client'</code> to identify the type of 4185<code>Http2Session</code>.</li> 4186</ul> 4187</section><section><h3>Note on <code>:authority</code> and <code>host</code><span><a class="mark" href="#note-on-authority-and-host" id="note-on-authority-and-host">#</a></span><a aria-hidden="true" class="legacy" id="http2_note_on_authority_and_host"></a></h3> 4188<p>HTTP/2 requires requests to have either the <code>:authority</code> pseudo-header 4189or the <code>host</code> header. Prefer <code>:authority</code> when constructing an HTTP/2 4190request directly, and <code>host</code> when converting from HTTP/1 (in proxies, 4191for instance).</p> 4192<p>The compatibility API falls back to <code>host</code> if <code>:authority</code> is not 4193present. See <a href="#requestauthority"><code>request.authority</code></a> for more information. However, 4194if you don't use the compatibility API (or use <code>req.headers</code> directly), 4195you need to implement any fall-back behavior yourself.</p></section> 4196 <!-- API END --> 4197 </div> 4198 </div> 4199 </div> 4200</body> 4201</html> 4202