1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 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 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="nodejs.org:node-version" content="v22.14.0">
<title>HTTP/2 | Node.js v22.14.0 Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/http2.html">
<script async defer src="assets/api.js" type="text/javascript"></script>
<script>
const storedTheme = localStorage.getItem('theme');
// Follow operating system theme preference
if (storedTheme === null && window.matchMedia) {
const mq = window.matchMedia('(prefers-color-scheme: dark)');
if (mq.matches) {
document.documentElement.classList.add('dark-mode');
}
} else if (storedTheme === 'dark') {
document.documentElement.classList.add('dark-mode');
}
</script>
<style>@media(max-width:606px){.with-48-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:534px){.with-39-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:558px){.with-42-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:638px){.with-52-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:718px){.with-62-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:670px){.with-56-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:622px){.with-50-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:678px){.with-57-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:646px){.with-53-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:694px){.with-59-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:662px){.with-55-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:598px){.with-47-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:518px){.with-37-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:542px){.with-40-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:766px){.with-68-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:614px){.with-49-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:654px){.with-54-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}</style>
</head>
<body class="alt apidoc" id="api-section-http2">
<a href="#apicontent" class="skip-to-content">Skip to content</a>
<div id="content" class="clearfix">
<div role="navigation" id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2 active">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
<li><a href="typescript.html" class="nav-typescript">Modules: TypeScript</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
<li><a href="sqlite.html" class="nav-sqlite">SQLite</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="test.html" class="nav-test">Test runner</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul>
</div>
<div id="column1" data-id="http2" class="interior">
<header class="header">
<div class="header-container">
<h1>Node.js v22.14.0 documentation</h1>
<button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
<svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
<path fill="none" d="M0 0h24v24H0z" />
<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"/>
<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"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
<path d="M0 0h24v24H0z" fill="none" />
<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"/>
</svg>
</button>
</div>
<div id="gtoc">
<ul>
<li class="pinned-header">Node.js v22.14.0</li>
<li class="picker-header">
<a href="#toc-picker" aria-controls="toc-picker">
<span class="picker-arrow"></span>
Table of contents
</a>
<div class="picker" tabindex="-1"><div class="toc"><ul id="toc-picker">
<li><span class="stability_2"><a href="#http2">HTTP/2</a></span>
<ul>
<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li>
<li><a href="#core-api">Core API</a>
<ul>
<li><a href="#server-side-example">Server-side example</a></li>
<li><a href="#client-side-example">Client-side example</a></li>
<li><a href="#class-http2session">Class: <code>Http2Session</code></a>
<ul>
<li><a href="#http2session-and-sockets"><code>Http2Session</code> and sockets</a></li>
<li><a href="#event-close">Event: <code>'close'</code></a></li>
<li><a href="#event-connect">Event: <code>'connect'</code></a></li>
<li><a href="#event-error">Event: <code>'error'</code></a></li>
<li><a href="#event-frameerror">Event: <code>'frameError'</code></a></li>
<li><a href="#event-goaway">Event: <code>'goaway'</code></a></li>
<li><a href="#event-localsettings">Event: <code>'localSettings'</code></a></li>
<li><a href="#event-ping">Event: <code>'ping'</code></a></li>
<li><a href="#event-remotesettings">Event: <code>'remoteSettings'</code></a></li>
<li><a href="#event-stream">Event: <code>'stream'</code></a></li>
<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li>
<li><a href="#http2sessionalpnprotocol"><code>http2session.alpnProtocol</code></a></li>
<li><a href="#http2sessionclosecallback"><code>http2session.close([callback])</code></a></li>
<li><a href="#http2sessionclosed"><code>http2session.closed</code></a></li>
<li><a href="#http2sessionconnecting"><code>http2session.connecting</code></a></li>
<li><a href="#http2sessiondestroyerror-code"><code>http2session.destroy([error][, code])</code></a></li>
<li><a href="#http2sessiondestroyed"><code>http2session.destroyed</code></a></li>
<li><a href="#http2sessionencrypted"><code>http2session.encrypted</code></a></li>
<li><a href="#http2sessiongoawaycode-laststreamid-opaquedata"><code>http2session.goaway([code[, lastStreamID[, opaqueData]]])</code></a></li>
<li><a href="#http2sessionlocalsettings"><code>http2session.localSettings</code></a></li>
<li><a href="#http2sessionoriginset"><code>http2session.originSet</code></a></li>
<li><a href="#http2sessionpendingsettingsack"><code>http2session.pendingSettingsAck</code></a></li>
<li><a href="#http2sessionpingpayload-callback"><code>http2session.ping([payload, ]callback)</code></a></li>
<li><a href="#http2sessionref"><code>http2session.ref()</code></a></li>
<li><a href="#http2sessionremotesettings"><code>http2session.remoteSettings</code></a></li>
<li><a href="#http2sessionsetlocalwindowsizewindowsize"><code>http2session.setLocalWindowSize(windowSize)</code></a></li>
<li><a href="#http2sessionsettimeoutmsecs-callback"><code>http2session.setTimeout(msecs, callback)</code></a></li>
<li><a href="#http2sessionsocket"><code>http2session.socket</code></a></li>
<li><a href="#http2sessionstate"><code>http2session.state</code></a></li>
<li><a href="#http2sessionsettingssettings-callback"><code>http2session.settings([settings][, callback])</code></a></li>
<li><a href="#http2sessiontype"><code>http2session.type</code></a></li>
<li><a href="#http2sessionunref"><code>http2session.unref()</code></a></li>
</ul>
</li>
<li><a href="#class-serverhttp2session">Class: <code>ServerHttp2Session</code></a>
<ul>
<li><a href="#serverhttp2sessionaltsvcalt-originorstream"><code>serverhttp2session.altsvc(alt, originOrStream)</code></a></li>
<li><a href="#specifying-alternative-services">Specifying alternative services</a></li>
<li><a href="#serverhttp2sessionoriginorigins"><code>serverhttp2session.origin(...origins)</code></a></li>
</ul>
</li>
<li><a href="#class-clienthttp2session">Class: <code>ClientHttp2Session</code></a>
<ul>
<li><a href="#event-altsvc">Event: <code>'altsvc'</code></a></li>
<li><a href="#event-origin">Event: <code>'origin'</code></a></li>
<li><a href="#clienthttp2sessionrequestheaders-options"><code>clienthttp2session.request(headers[, options])</code></a></li>
</ul>
</li>
<li><a href="#class-http2stream">Class: <code>Http2Stream</code></a>
<ul>
<li><a href="#http2stream-lifecycle"><code>Http2Stream</code> Lifecycle</a>
<ul>
<li><a href="#creation">Creation</a></li>
<li><a href="#destruction">Destruction</a></li>
</ul>
</li>
<li><a href="#event-aborted">Event: <code>'aborted'</code></a></li>
<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
<li><a href="#event-error_1">Event: <code>'error'</code></a></li>
<li><a href="#event-frameerror_1">Event: <code>'frameError'</code></a></li>
<li><a href="#event-ready">Event: <code>'ready'</code></a></li>
<li><a href="#event-timeout_1">Event: <code>'timeout'</code></a></li>
<li><a href="#event-trailers">Event: <code>'trailers'</code></a></li>
<li><a href="#event-wanttrailers">Event: <code>'wantTrailers'</code></a></li>
<li><a href="#http2streamaborted"><code>http2stream.aborted</code></a></li>
<li><a href="#http2streambuffersize"><code>http2stream.bufferSize</code></a></li>
<li><a href="#http2streamclosecode-callback"><code>http2stream.close(code[, callback])</code></a></li>
<li><a href="#http2streamclosed"><code>http2stream.closed</code></a></li>
<li><a href="#http2streamdestroyed"><code>http2stream.destroyed</code></a></li>
<li><a href="#http2streamendafterheaders"><code>http2stream.endAfterHeaders</code></a></li>
<li><a href="#http2streamid"><code>http2stream.id</code></a></li>
<li><a href="#http2streampending"><code>http2stream.pending</code></a></li>
<li><a href="#http2streampriorityoptions"><code>http2stream.priority(options)</code></a></li>
<li><a href="#http2streamrstcode"><code>http2stream.rstCode</code></a></li>
<li><a href="#http2streamsentheaders"><code>http2stream.sentHeaders</code></a></li>
<li><a href="#http2streamsentinfoheaders"><code>http2stream.sentInfoHeaders</code></a></li>
<li><a href="#http2streamsenttrailers"><code>http2stream.sentTrailers</code></a></li>
<li><a href="#http2streamsession"><code>http2stream.session</code></a></li>
<li><a href="#http2streamsettimeoutmsecs-callback"><code>http2stream.setTimeout(msecs, callback)</code></a></li>
<li><a href="#http2streamstate"><code>http2stream.state</code></a></li>
<li><a href="#http2streamsendtrailersheaders"><code>http2stream.sendTrailers(headers)</code></a></li>
</ul>
</li>
<li><a href="#class-clienthttp2stream">Class: <code>ClientHttp2Stream</code></a>
<ul>
<li><a href="#event-continue">Event: <code>'continue'</code></a></li>
<li><a href="#event-headers">Event: <code>'headers'</code></a></li>
<li><a href="#event-push">Event: <code>'push'</code></a></li>
<li><a href="#event-response">Event: <code>'response'</code></a></li>
</ul>
</li>
<li><a href="#class-serverhttp2stream">Class: <code>ServerHttp2Stream</code></a>
<ul>
<li><a href="#http2streamadditionalheadersheaders"><code>http2stream.additionalHeaders(headers)</code></a></li>
<li><a href="#http2streamheaderssent"><code>http2stream.headersSent</code></a></li>
<li><a href="#http2streampushallowed"><code>http2stream.pushAllowed</code></a></li>
<li><a href="#http2streampushstreamheaders-options-callback"><code>http2stream.pushStream(headers[, options], callback)</code></a></li>
<li><a href="#http2streamrespondheaders-options"><code>http2stream.respond([headers[, options]])</code></a></li>
<li><a href="#http2streamrespondwithfdfd-headers-options"><code>http2stream.respondWithFD(fd[, headers[, options]])</code></a></li>
<li><a href="#http2streamrespondwithfilepath-headers-options"><code>http2stream.respondWithFile(path[, headers[, options]])</code></a></li>
</ul>
</li>
<li><a href="#class-http2server">Class: <code>Http2Server</code></a>
<ul>
<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li>
<li><a href="#event-connection">Event: <code>'connection'</code></a></li>
<li><a href="#event-request">Event: <code>'request'</code></a></li>
<li><a href="#event-session">Event: <code>'session'</code></a></li>
<li><a href="#event-sessionerror">Event: <code>'sessionError'</code></a></li>
<li><a href="#event-stream_1">Event: <code>'stream'</code></a></li>
<li><a href="#event-timeout_2">Event: <code>'timeout'</code></a></li>
<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li>
<li><span class="stability_1"><a href="#serversymbolasyncdispose"><code>server[Symbol.asyncDispose]()</code></a></span></li>
<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li>
<li><a href="#servertimeout"><code>server.timeout</code></a></li>
<li><a href="#serverupdatesettingssettings"><code>server.updateSettings([settings])</code></a></li>
</ul>
</li>
<li><a href="#class-http2secureserver">Class: <code>Http2SecureServer</code></a>
<ul>
<li><a href="#event-checkcontinue_1">Event: <code>'checkContinue'</code></a></li>
<li><a href="#event-connection_1">Event: <code>'connection'</code></a></li>
<li><a href="#event-request_1">Event: <code>'request'</code></a></li>
<li><a href="#event-session_1">Event: <code>'session'</code></a></li>
<li><a href="#event-sessionerror_1">Event: <code>'sessionError'</code></a></li>
<li><a href="#event-stream_2">Event: <code>'stream'</code></a></li>
<li><a href="#event-timeout_3">Event: <code>'timeout'</code></a></li>
<li><a href="#event-unknownprotocol">Event: <code>'unknownProtocol'</code></a></li>
<li><a href="#serverclosecallback_1"><code>server.close([callback])</code></a></li>
<li><a href="#serversettimeoutmsecs-callback_1"><code>server.setTimeout([msecs][, callback])</code></a></li>
<li><a href="#servertimeout_1"><code>server.timeout</code></a></li>
<li><a href="#serverupdatesettingssettings_1"><code>server.updateSettings([settings])</code></a></li>
</ul>
</li>
<li><a href="#http2createserveroptions-onrequesthandler"><code>http2.createServer([options][, onRequestHandler])</code></a></li>
<li><a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer(options[, onRequestHandler])</code></a></li>
<li><a href="#http2connectauthority-options-listener"><code>http2.connect(authority[, options][, listener])</code></a></li>
<li><a href="#http2constants"><code>http2.constants</code></a>
<ul>
<li><a href="#error-codes-for-rst_stream-and-goaway">Error codes for <code>RST_STREAM</code> and <code>GOAWAY</code></a></li>
</ul>
</li>
<li><a href="#http2getdefaultsettings"><code>http2.getDefaultSettings()</code></a></li>
<li><a href="#http2getpackedsettingssettings"><code>http2.getPackedSettings([settings])</code></a></li>
<li><a href="#http2getunpackedsettingsbuf"><code>http2.getUnpackedSettings(buf)</code></a></li>
<li><a href="#http2performserverhandshakesocket-options"><code>http2.performServerHandshake(socket[, options])</code></a></li>
<li><a href="#http2sensitiveheaders"><code>http2.sensitiveHeaders</code></a></li>
<li><a href="#headers-object">Headers object</a>
<ul>
<li><a href="#sensitive-headers">Sensitive headers</a></li>
</ul>
</li>
<li><a href="#settings-object">Settings object</a></li>
<li><a href="#error-handling">Error handling</a></li>
<li><a href="#invalid-character-handling-in-header-names-and-values">Invalid character handling in header names and values</a></li>
<li><a href="#push-streams-on-the-client">Push streams on the client</a></li>
<li><a href="#supporting-the-connect-method">Supporting the <code>CONNECT</code> method</a></li>
<li><a href="#the-extended-connect-protocol">The extended <code>CONNECT</code> protocol</a></li>
</ul>
</li>
<li><a href="#compatibility-api">Compatibility API</a>
<ul>
<li><a href="#alpn-negotiation">ALPN negotiation</a></li>
<li><a href="#class-http2http2serverrequest">Class: <code>http2.Http2ServerRequest</code></a>
<ul>
<li><a href="#event-aborted_1">Event: <code>'aborted'</code></a></li>
<li><a href="#event-close_2">Event: <code>'close'</code></a></li>
<li><a href="#requestaborted"><code>request.aborted</code></a></li>
<li><a href="#requestauthority"><code>request.authority</code></a></li>
<li><a href="#requestcomplete"><code>request.complete</code></a></li>
<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li>
<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a></li>
<li><a href="#requestheaders"><code>request.headers</code></a></li>
<li><a href="#requesthttpversion"><code>request.httpVersion</code></a></li>
<li><a href="#requestmethod"><code>request.method</code></a></li>
<li><a href="#requestrawheaders"><code>request.rawHeaders</code></a></li>
<li><a href="#requestrawtrailers"><code>request.rawTrailers</code></a></li>
<li><a href="#requestscheme"><code>request.scheme</code></a></li>
<li><a href="#requestsettimeoutmsecs-callback"><code>request.setTimeout(msecs, callback)</code></a></li>
<li><a href="#requestsocket"><code>request.socket</code></a></li>
<li><a href="#requeststream"><code>request.stream</code></a></li>
<li><a href="#requesttrailers"><code>request.trailers</code></a></li>
<li><a href="#requesturl"><code>request.url</code></a></li>
</ul>
</li>
<li><a href="#class-http2http2serverresponse">Class: <code>http2.Http2ServerResponse</code></a>
<ul>
<li><a href="#event-close_3">Event: <code>'close'</code></a></li>
<li><a href="#event-finish">Event: <code>'finish'</code></a></li>
<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li>
<li><a href="#responseappendheadername-value"><code>response.appendHeader(name, value)</code></a></li>
<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li>
<li><a href="#responsecreatepushresponseheaders-callback"><code>response.createPushResponse(headers, callback)</code></a></li>
<li><a href="#responseenddata-encoding-callback"><code>response.end([data[, encoding]][, callback])</code></a></li>
<li><span class="stability_0"><a href="#responsefinished"><code>response.finished</code></a></span></li>
<li><a href="#responsegetheadername"><code>response.getHeader(name)</code></a></li>
<li><a href="#responsegetheadernames"><code>response.getHeaderNames()</code></a></li>
<li><a href="#responsegetheaders"><code>response.getHeaders()</code></a></li>
<li><a href="#responsehasheadername"><code>response.hasHeader(name)</code></a></li>
<li><a href="#responseheaderssent"><code>response.headersSent</code></a></li>
<li><a href="#responseremoveheadername"><code>response.removeHeader(name)</code></a></li>
<li><a href="#responsereq"><code>response.req</code></a></li>
<li><a href="#responsesenddate"><code>response.sendDate</code></a></li>
<li><a href="#responsesetheadername-value"><code>response.setHeader(name, value)</code></a></li>
<li><a href="#responsesettimeoutmsecs-callback"><code>response.setTimeout(msecs[, callback])</code></a></li>
<li><a href="#responsesocket"><code>response.socket</code></a></li>
<li><a href="#responsestatuscode"><code>response.statusCode</code></a></li>
<li><a href="#responsestatusmessage"><code>response.statusMessage</code></a></li>
<li><a href="#responsestream"><code>response.stream</code></a></li>
<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li>
<li><a href="#responsewritechunk-encoding-callback"><code>response.write(chunk[, encoding][, callback])</code></a></li>
<li><a href="#responsewritecontinue"><code>response.writeContinue()</code></a></li>
<li><a href="#responsewriteearlyhintshints"><code>response.writeEarlyHints(hints)</code></a></li>
<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#collecting-http2-performance-metrics">Collecting HTTP/2 performance metrics</a></li>
<li><a href="#note-on-authority-and-host">Note on <code>:authority</code> and <code>host</code></a></li>
</ul>
</li>
</ul></div></div>
</li>
<li class="picker-header">
<a href="#gtoc-picker" aria-controls="gtoc-picker">
<span class="picker-arrow"></span>
Index
</a>
<div class="picker" tabindex="-1" id="gtoc-picker"><ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
<li>
<a href="index.html">Index</a>
</li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2 active">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
<li><a href="typescript.html" class="nav-typescript">Modules: TypeScript</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
<li><a href="sqlite.html" class="nav-sqlite">SQLite</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="test.html" class="nav-test">Test runner</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul></div>
</li>
<li class="picker-header">
<a href="#alt-docs" aria-controls="alt-docs">
<span class="picker-arrow"></span>
Other versions
</a>
<div class="picker" tabindex="-1"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v23.x/api/http2.html">23.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v22.x/api/http2.html">22.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v21.x/api/http2.html">21.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v20.x/api/http2.html">20.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v19.x/api/http2.html">19.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v18.x/api/http2.html">18.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v17.x/api/http2.html">17.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v16.x/api/http2.html">16.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v15.x/api/http2.html">15.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v14.x/api/http2.html">14.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v13.x/api/http2.html">13.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v12.x/api/http2.html">12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v11.x/api/http2.html">11.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v10.x/api/http2.html">10.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v9.x/api/http2.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/http2.html">8.x</a></li></ol></div>
</li>
<li class="picker-header">
<a href="#options-picker" aria-controls="options-picker">
<span class="picker-arrow"></span>
Options
</a>
<div class="picker" tabindex="-1">
<ul id="options-picker">
<li>
<a href="all.html">View on single page</a>
</li>
<li>
<a href="http2.json">View as JSON</a>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/http2.md">Edit on GitHub</a></li>
</ul>
</div>
</li>
</ul>
</div>
<hr>
</header>
<details role="navigation" id="toc" open><summary>Table of contents</summary><ul>
<li><span class="stability_2"><a href="#http2">HTTP/2</a></span>
<ul>
<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li>
<li><a href="#core-api">Core API</a>
<ul>
<li><a href="#server-side-example">Server-side example</a></li>
<li><a href="#client-side-example">Client-side example</a></li>
<li><a href="#class-http2session">Class: <code>Http2Session</code></a>
<ul>
<li><a href="#http2session-and-sockets"><code>Http2Session</code> and sockets</a></li>
<li><a href="#event-close">Event: <code>'close'</code></a></li>
<li><a href="#event-connect">Event: <code>'connect'</code></a></li>
<li><a href="#event-error">Event: <code>'error'</code></a></li>
<li><a href="#event-frameerror">Event: <code>'frameError'</code></a></li>
<li><a href="#event-goaway">Event: <code>'goaway'</code></a></li>
<li><a href="#event-localsettings">Event: <code>'localSettings'</code></a></li>
<li><a href="#event-ping">Event: <code>'ping'</code></a></li>
<li><a href="#event-remotesettings">Event: <code>'remoteSettings'</code></a></li>
<li><a href="#event-stream">Event: <code>'stream'</code></a></li>
<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li>
<li><a href="#http2sessionalpnprotocol"><code>http2session.alpnProtocol</code></a></li>
<li><a href="#http2sessionclosecallback"><code>http2session.close([callback])</code></a></li>
<li><a href="#http2sessionclosed"><code>http2session.closed</code></a></li>
<li><a href="#http2sessionconnecting"><code>http2session.connecting</code></a></li>
<li><a href="#http2sessiondestroyerror-code"><code>http2session.destroy([error][, code])</code></a></li>
<li><a href="#http2sessiondestroyed"><code>http2session.destroyed</code></a></li>
<li><a href="#http2sessionencrypted"><code>http2session.encrypted</code></a></li>
<li><a href="#http2sessiongoawaycode-laststreamid-opaquedata"><code>http2session.goaway([code[, lastStreamID[, opaqueData]]])</code></a></li>
<li><a href="#http2sessionlocalsettings"><code>http2session.localSettings</code></a></li>
<li><a href="#http2sessionoriginset"><code>http2session.originSet</code></a></li>
<li><a href="#http2sessionpendingsettingsack"><code>http2session.pendingSettingsAck</code></a></li>
<li><a href="#http2sessionpingpayload-callback"><code>http2session.ping([payload, ]callback)</code></a></li>
<li><a href="#http2sessionref"><code>http2session.ref()</code></a></li>
<li><a href="#http2sessionremotesettings"><code>http2session.remoteSettings</code></a></li>
<li><a href="#http2sessionsetlocalwindowsizewindowsize"><code>http2session.setLocalWindowSize(windowSize)</code></a></li>
<li><a href="#http2sessionsettimeoutmsecs-callback"><code>http2session.setTimeout(msecs, callback)</code></a></li>
<li><a href="#http2sessionsocket"><code>http2session.socket</code></a></li>
<li><a href="#http2sessionstate"><code>http2session.state</code></a></li>
<li><a href="#http2sessionsettingssettings-callback"><code>http2session.settings([settings][, callback])</code></a></li>
<li><a href="#http2sessiontype"><code>http2session.type</code></a></li>
<li><a href="#http2sessionunref"><code>http2session.unref()</code></a></li>
</ul>
</li>
<li><a href="#class-serverhttp2session">Class: <code>ServerHttp2Session</code></a>
<ul>
<li><a href="#serverhttp2sessionaltsvcalt-originorstream"><code>serverhttp2session.altsvc(alt, originOrStream)</code></a></li>
<li><a href="#specifying-alternative-services">Specifying alternative services</a></li>
<li><a href="#serverhttp2sessionoriginorigins"><code>serverhttp2session.origin(...origins)</code></a></li>
</ul>
</li>
<li><a href="#class-clienthttp2session">Class: <code>ClientHttp2Session</code></a>
<ul>
<li><a href="#event-altsvc">Event: <code>'altsvc'</code></a></li>
<li><a href="#event-origin">Event: <code>'origin'</code></a></li>
<li><a href="#clienthttp2sessionrequestheaders-options"><code>clienthttp2session.request(headers[, options])</code></a></li>
</ul>
</li>
<li><a href="#class-http2stream">Class: <code>Http2Stream</code></a>
<ul>
<li><a href="#http2stream-lifecycle"><code>Http2Stream</code> Lifecycle</a>
<ul>
<li><a href="#creation">Creation</a></li>
<li><a href="#destruction">Destruction</a></li>
</ul>
</li>
<li><a href="#event-aborted">Event: <code>'aborted'</code></a></li>
<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
<li><a href="#event-error_1">Event: <code>'error'</code></a></li>
<li><a href="#event-frameerror_1">Event: <code>'frameError'</code></a></li>
<li><a href="#event-ready">Event: <code>'ready'</code></a></li>
<li><a href="#event-timeout_1">Event: <code>'timeout'</code></a></li>
<li><a href="#event-trailers">Event: <code>'trailers'</code></a></li>
<li><a href="#event-wanttrailers">Event: <code>'wantTrailers'</code></a></li>
<li><a href="#http2streamaborted"><code>http2stream.aborted</code></a></li>
<li><a href="#http2streambuffersize"><code>http2stream.bufferSize</code></a></li>
<li><a href="#http2streamclosecode-callback"><code>http2stream.close(code[, callback])</code></a></li>
<li><a href="#http2streamclosed"><code>http2stream.closed</code></a></li>
<li><a href="#http2streamdestroyed"><code>http2stream.destroyed</code></a></li>
<li><a href="#http2streamendafterheaders"><code>http2stream.endAfterHeaders</code></a></li>
<li><a href="#http2streamid"><code>http2stream.id</code></a></li>
<li><a href="#http2streampending"><code>http2stream.pending</code></a></li>
<li><a href="#http2streampriorityoptions"><code>http2stream.priority(options)</code></a></li>
<li><a href="#http2streamrstcode"><code>http2stream.rstCode</code></a></li>
<li><a href="#http2streamsentheaders"><code>http2stream.sentHeaders</code></a></li>
<li><a href="#http2streamsentinfoheaders"><code>http2stream.sentInfoHeaders</code></a></li>
<li><a href="#http2streamsenttrailers"><code>http2stream.sentTrailers</code></a></li>
<li><a href="#http2streamsession"><code>http2stream.session</code></a></li>
<li><a href="#http2streamsettimeoutmsecs-callback"><code>http2stream.setTimeout(msecs, callback)</code></a></li>
<li><a href="#http2streamstate"><code>http2stream.state</code></a></li>
<li><a href="#http2streamsendtrailersheaders"><code>http2stream.sendTrailers(headers)</code></a></li>
</ul>
</li>
<li><a href="#class-clienthttp2stream">Class: <code>ClientHttp2Stream</code></a>
<ul>
<li><a href="#event-continue">Event: <code>'continue'</code></a></li>
<li><a href="#event-headers">Event: <code>'headers'</code></a></li>
<li><a href="#event-push">Event: <code>'push'</code></a></li>
<li><a href="#event-response">Event: <code>'response'</code></a></li>
</ul>
</li>
<li><a href="#class-serverhttp2stream">Class: <code>ServerHttp2Stream</code></a>
<ul>
<li><a href="#http2streamadditionalheadersheaders"><code>http2stream.additionalHeaders(headers)</code></a></li>
<li><a href="#http2streamheaderssent"><code>http2stream.headersSent</code></a></li>
<li><a href="#http2streampushallowed"><code>http2stream.pushAllowed</code></a></li>
<li><a href="#http2streampushstreamheaders-options-callback"><code>http2stream.pushStream(headers[, options], callback)</code></a></li>
<li><a href="#http2streamrespondheaders-options"><code>http2stream.respond([headers[, options]])</code></a></li>
<li><a href="#http2streamrespondwithfdfd-headers-options"><code>http2stream.respondWithFD(fd[, headers[, options]])</code></a></li>
<li><a href="#http2streamrespondwithfilepath-headers-options"><code>http2stream.respondWithFile(path[, headers[, options]])</code></a></li>
</ul>
</li>
<li><a href="#class-http2server">Class: <code>Http2Server</code></a>
<ul>
<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li>
<li><a href="#event-connection">Event: <code>'connection'</code></a></li>
<li><a href="#event-request">Event: <code>'request'</code></a></li>
<li><a href="#event-session">Event: <code>'session'</code></a></li>
<li><a href="#event-sessionerror">Event: <code>'sessionError'</code></a></li>
<li><a href="#event-stream_1">Event: <code>'stream'</code></a></li>
<li><a href="#event-timeout_2">Event: <code>'timeout'</code></a></li>
<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li>
<li><span class="stability_1"><a href="#serversymbolasyncdispose"><code>server[Symbol.asyncDispose]()</code></a></span></li>
<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li>
<li><a href="#servertimeout"><code>server.timeout</code></a></li>
<li><a href="#serverupdatesettingssettings"><code>server.updateSettings([settings])</code></a></li>
</ul>
</li>
<li><a href="#class-http2secureserver">Class: <code>Http2SecureServer</code></a>
<ul>
<li><a href="#event-checkcontinue_1">Event: <code>'checkContinue'</code></a></li>
<li><a href="#event-connection_1">Event: <code>'connection'</code></a></li>
<li><a href="#event-request_1">Event: <code>'request'</code></a></li>
<li><a href="#event-session_1">Event: <code>'session'</code></a></li>
<li><a href="#event-sessionerror_1">Event: <code>'sessionError'</code></a></li>
<li><a href="#event-stream_2">Event: <code>'stream'</code></a></li>
<li><a href="#event-timeout_3">Event: <code>'timeout'</code></a></li>
<li><a href="#event-unknownprotocol">Event: <code>'unknownProtocol'</code></a></li>
<li><a href="#serverclosecallback_1"><code>server.close([callback])</code></a></li>
<li><a href="#serversettimeoutmsecs-callback_1"><code>server.setTimeout([msecs][, callback])</code></a></li>
<li><a href="#servertimeout_1"><code>server.timeout</code></a></li>
<li><a href="#serverupdatesettingssettings_1"><code>server.updateSettings([settings])</code></a></li>
</ul>
</li>
<li><a href="#http2createserveroptions-onrequesthandler"><code>http2.createServer([options][, onRequestHandler])</code></a></li>
<li><a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer(options[, onRequestHandler])</code></a></li>
<li><a href="#http2connectauthority-options-listener"><code>http2.connect(authority[, options][, listener])</code></a></li>
<li><a href="#http2constants"><code>http2.constants</code></a>
<ul>
<li><a href="#error-codes-for-rst_stream-and-goaway">Error codes for <code>RST_STREAM</code> and <code>GOAWAY</code></a></li>
</ul>
</li>
<li><a href="#http2getdefaultsettings"><code>http2.getDefaultSettings()</code></a></li>
<li><a href="#http2getpackedsettingssettings"><code>http2.getPackedSettings([settings])</code></a></li>
<li><a href="#http2getunpackedsettingsbuf"><code>http2.getUnpackedSettings(buf)</code></a></li>
<li><a href="#http2performserverhandshakesocket-options"><code>http2.performServerHandshake(socket[, options])</code></a></li>
<li><a href="#http2sensitiveheaders"><code>http2.sensitiveHeaders</code></a></li>
<li><a href="#headers-object">Headers object</a>
<ul>
<li><a href="#sensitive-headers">Sensitive headers</a></li>
</ul>
</li>
<li><a href="#settings-object">Settings object</a></li>
<li><a href="#error-handling">Error handling</a></li>
<li><a href="#invalid-character-handling-in-header-names-and-values">Invalid character handling in header names and values</a></li>
<li><a href="#push-streams-on-the-client">Push streams on the client</a></li>
<li><a href="#supporting-the-connect-method">Supporting the <code>CONNECT</code> method</a></li>
<li><a href="#the-extended-connect-protocol">The extended <code>CONNECT</code> protocol</a></li>
</ul>
</li>
<li><a href="#compatibility-api">Compatibility API</a>
<ul>
<li><a href="#alpn-negotiation">ALPN negotiation</a></li>
<li><a href="#class-http2http2serverrequest">Class: <code>http2.Http2ServerRequest</code></a>
<ul>
<li><a href="#event-aborted_1">Event: <code>'aborted'</code></a></li>
<li><a href="#event-close_2">Event: <code>'close'</code></a></li>
<li><a href="#requestaborted"><code>request.aborted</code></a></li>
<li><a href="#requestauthority"><code>request.authority</code></a></li>
<li><a href="#requestcomplete"><code>request.complete</code></a></li>
<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li>
<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a></li>
<li><a href="#requestheaders"><code>request.headers</code></a></li>
<li><a href="#requesthttpversion"><code>request.httpVersion</code></a></li>
<li><a href="#requestmethod"><code>request.method</code></a></li>
<li><a href="#requestrawheaders"><code>request.rawHeaders</code></a></li>
<li><a href="#requestrawtrailers"><code>request.rawTrailers</code></a></li>
<li><a href="#requestscheme"><code>request.scheme</code></a></li>
<li><a href="#requestsettimeoutmsecs-callback"><code>request.setTimeout(msecs, callback)</code></a></li>
<li><a href="#requestsocket"><code>request.socket</code></a></li>
<li><a href="#requeststream"><code>request.stream</code></a></li>
<li><a href="#requesttrailers"><code>request.trailers</code></a></li>
<li><a href="#requesturl"><code>request.url</code></a></li>
</ul>
</li>
<li><a href="#class-http2http2serverresponse">Class: <code>http2.Http2ServerResponse</code></a>
<ul>
<li><a href="#event-close_3">Event: <code>'close'</code></a></li>
<li><a href="#event-finish">Event: <code>'finish'</code></a></li>
<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li>
<li><a href="#responseappendheadername-value"><code>response.appendHeader(name, value)</code></a></li>
<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li>
<li><a href="#responsecreatepushresponseheaders-callback"><code>response.createPushResponse(headers, callback)</code></a></li>
<li><a href="#responseenddata-encoding-callback"><code>response.end([data[, encoding]][, callback])</code></a></li>
<li><span class="stability_0"><a href="#responsefinished"><code>response.finished</code></a></span></li>
<li><a href="#responsegetheadername"><code>response.getHeader(name)</code></a></li>
<li><a href="#responsegetheadernames"><code>response.getHeaderNames()</code></a></li>
<li><a href="#responsegetheaders"><code>response.getHeaders()</code></a></li>
<li><a href="#responsehasheadername"><code>response.hasHeader(name)</code></a></li>
<li><a href="#responseheaderssent"><code>response.headersSent</code></a></li>
<li><a href="#responseremoveheadername"><code>response.removeHeader(name)</code></a></li>
<li><a href="#responsereq"><code>response.req</code></a></li>
<li><a href="#responsesenddate"><code>response.sendDate</code></a></li>
<li><a href="#responsesetheadername-value"><code>response.setHeader(name, value)</code></a></li>
<li><a href="#responsesettimeoutmsecs-callback"><code>response.setTimeout(msecs[, callback])</code></a></li>
<li><a href="#responsesocket"><code>response.socket</code></a></li>
<li><a href="#responsestatuscode"><code>response.statusCode</code></a></li>
<li><a href="#responsestatusmessage"><code>response.statusMessage</code></a></li>
<li><a href="#responsestream"><code>response.stream</code></a></li>
<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li>
<li><a href="#responsewritechunk-encoding-callback"><code>response.write(chunk[, encoding][, callback])</code></a></li>
<li><a href="#responsewritecontinue"><code>response.writeContinue()</code></a></li>
<li><a href="#responsewriteearlyhintshints"><code>response.writeEarlyHints(hints)</code></a></li>
<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#collecting-http2-performance-metrics">Collecting HTTP/2 performance metrics</a></li>
<li><a href="#note-on-authority-and-host">Note on <code>:authority</code> and <code>host</code></a></li>
</ul>
</li>
</ul></details>
<div role="main" id="apicontent">
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.0.0</td>
<td><p>Requests with the <code>host</code> header (with or without <code>:authority</code>) can now be sent/received.</p></td></tr>
<tr><td>v15.3.0, v14.17.0</td>
<td><p>It is possible to abort a request with an AbortSignal.</p></td></tr>
<tr><td>v10.10.0</td>
<td><p>HTTP/2 is now Stable. Previously, it had been Experimental.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v22.14.0/lib/http2.js">lib/http2.js</a></p>
<p>The <code>node:http2</code> module provides an implementation of the <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> protocol.
It can be accessed using:</p>
<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>
<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>
<p>It is possible for Node.js to be built without including support for the
<code>node:crypto</code> module. In such cases, attempting to <code>import</code> from <code>node:http2</code> or
calling <code>require('node:http2')</code> will result in an error being thrown.</p>
<p>When using CommonJS, the error thrown can be caught using try/catch:</p>
<pre><code class="language-js cjs"><span class="hljs-keyword">let</span> http2;
<span class="hljs-keyword">try</span> {
http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'http2 support is disabled!'</span>);
}</code> <button class="copy-button">copy</button></pre>
<p>When using the lexical ESM <code>import</code> keyword, the error can only be
caught if a handler for <code>process.on('uncaughtException')</code> is registered
<em>before</em> any attempt to load the module is made (using, for instance,
a preload module).</p>
<p>When using ESM, if there is a chance that the code may be run on a build
of Node.js where crypto support is not enabled, consider using the
<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>
<pre><code class="language-js mjs"><span class="hljs-keyword">let</span> http2;
<span class="hljs-keyword">try</span> {
http2 = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:http2'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'http2 support is disabled!'</span>);
}</code> <button class="copy-button">copy</button></pre>
</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>
<p>The Core API provides a low-level interface designed specifically around
support for HTTP/2 protocol features. It is specifically <em>not</em> designed for
compatibility with the existing <a href="http.html">HTTP/1</a> module API. However,
the <a href="#compatibility-api">Compatibility API</a> is.</p>
<p>The <code>http2</code> Core API is much more symmetric between client and server than the
<code>http</code> API. For instance, most events, like <code>'error'</code>, <code>'connect'</code> and
<code>'stream'</code>, can be emitted either by client-side code or server-side code.</p>
<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>
<p>The following illustrates a simple HTTP/2 server using the Core API.
Since there are no browsers known that support
<a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the use of
<a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a> is necessary when communicating
with browser clients.</p>
<pre class="with-48-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createSecureServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureServer</span>({
<span class="hljs-attr">key</span>: <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-privkey.pem'</span>),
<span class="hljs-attr">cert</span>: <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-cert.pem'</span>),
});
server.<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));
server.<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> {
<span class="hljs-comment">// stream is a Duplex</span>
stream.<span class="hljs-title function_">respond</span>({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>);
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>({
<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-privkey.pem'</span>),
<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-cert.pem'</span>),
});
server.<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));
server.<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> {
<span class="hljs-comment">// stream is a Duplex</span>
stream.<span class="hljs-title function_">respond</span>({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>);
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</code><button class="copy-button">copy</button></pre>
<p>To generate the certificate and key for this example, run:</p>
<pre><code class="language-bash">openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj <span class="hljs-string">'/CN=localhost'</span> \
-keyout localhost-privkey.pem -out localhost-cert.pem</code> <button class="copy-button">copy</button></pre>
<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>
<p>The following illustrates an HTTP/2 client:</p>
<pre class="with-39-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> client = <span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:8443'</span>, {
<span class="hljs-attr">ca</span>: <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-cert.pem'</span>),
});
client.<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));
<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> });
req.<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> {
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">in</span> headers) {
<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>);
}
});
req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
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> { data += chunk; });
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
<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>);
client.<span class="hljs-title function_">close</span>();
});
req.<span class="hljs-title function_">end</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:8443'</span>, {
<span class="hljs-attr">ca</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-cert.pem'</span>),
});
client.<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));
<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> });
req.<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> {
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">in</span> headers) {
<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>);
}
});
req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
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> { data += chunk; });
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
<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>);
client.<span class="hljs-title function_">close</span>();
});
req.<span class="hljs-title function_">end</span>();</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li>
</ul>
<p>Instances of the <code>http2.Http2Session</code> class represent an active communications
session between an HTTP/2 client and server. Instances of this class are <em>not</em>
intended to be constructed directly by user code.</p>
<p>Each <code>Http2Session</code> instance will exhibit slightly different behaviors
depending on whether it is operating as a server or a client. The
<code>http2session.type</code> property can be used to determine the mode in which an
<code>Http2Session</code> is operating. On the server side, user code should rarely
have occasion to work with the <code>Http2Session</code> object directly, with most
actions typically taken through interactions with either the <code>Http2Server</code> or
<code>Http2Stream</code> objects.</p>
<p>User code will not create <code>Http2Session</code> instances directly. Server-side
<code>Http2Session</code> instances are created by the <code>Http2Server</code> instance when a
new HTTP/2 connection is received. Client-side <code>Http2Session</code> instances are
created using the <code>http2.connect()</code> method.</p>
<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>
<p>Every <code>Http2Session</code> instance is associated with exactly one <a href="net.html#class-netsocket"><code>net.Socket</code></a> or
<a href="tls.html#class-tlstlssocket"><code>tls.TLSSocket</code></a> when it is created. When either the <code>Socket</code> or the
<code>Http2Session</code> are destroyed, both will be destroyed.</p>
<p>Because of the specific serialization and processing requirements imposed
by the HTTP/2 protocol, it is not recommended for user code to read data from
or write data to a <code>Socket</code> instance bound to a <code>Http2Session</code>. Doing so can
put the HTTP/2 session into an indeterminate state causing the session and
the socket to become unusable.</p>
<p>Once a <code>Socket</code> has been bound to an <code>Http2Session</code>, user code should rely
solely on the API of the <code>Http2Session</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>The <code>'close'</code> event is emitted once the <code>Http2Session</code> has been destroyed. Its
listener does not expect any arguments.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>session</code> <a href="http2.html#class-http2session" class="type"><Http2Session></a></li>
<li><code>socket</code> <a href="net.html#class-netsocket" class="type"><net.Socket></a></li>
</ul>
<p>The <code>'connect'</code> event is emitted once the <code>Http2Session</code> has been successfully
connected to the remote peer and communication may begin.</p>
<p>User code will typically not listen for this event directly.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p>The <code>'error'</code> event is emitted when an error occurs during the processing of
an <code>Http2Session</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<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>
<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>
<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
stream).</li>
</ul>
<p>The <code>'frameError'</code> event is emitted when an error occurs while attempting to
send a frame on the session. If the frame that could not be sent is associated
with a specific <code>Http2Stream</code>, an attempt to emit a <code>'frameError'</code> event on the
<code>Http2Stream</code> is made.</p>
<p>If the <code>'frameError'</code> event is associated with a stream, the stream will be
closed and destroyed immediately following the <code>'frameError'</code> event. If the
event is not associated with a stream, the <code>Http2Session</code> will be shut down
immediately following the <code>'frameError'</code> event.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<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>
<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
processed (or <code>0</code> if no ID is specified).</li>
<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>
frame, a <code>Buffer</code> instance will be passed containing that data.</li>
</ul>
<p>The <code>'goaway'</code> event is emitted when a <code>GOAWAY</code> frame is received.</p>
<p>The <code>Http2Session</code> instance will be shut down automatically when the <code>'goaway'</code>
event is emitted.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<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>
</ul>
<p>The <code>'localSettings'</code> event is emitted when an acknowledgment <code>SETTINGS</code> frame
has been received.</p>
<p>When using <code>http2session.settings()</code> to submit new settings, the modified
settings do not take effect until the <code>'localSettings'</code> event is emitted.</p>
<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> });
session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'localSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> {
<span class="hljs-comment">/* Use the new settings */</span>
});</code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v10.12.0</span>
</div>
<ul>
<li><code>payload</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> The <code>PING</code> frame 8-byte payload</li>
</ul>
<p>The <code>'ping'</code> event is emitted whenever a <code>PING</code> frame is received from the
connected peer.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<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>
</ul>
<p>The <code>'remoteSettings'</code> event is emitted when a new <code>SETTINGS</code> frame is received
from the connected peer.</p>
<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> {
<span class="hljs-comment">/* Use the new settings */</span>
});</code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>stream</code> <a href="http2.html#class-http2stream" class="type"><Http2Stream></a> A reference to the stream</li>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li>
<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>
<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
their respective values.</li>
</ul>
<p>The <code>'stream'</code> event is emitted when a new <code>Http2Stream</code> is created.</p>
<pre><code class="language-js">session.<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> {
<span class="hljs-keyword">const</span> method = headers[<span class="hljs-string">':method'</span>];
<span class="hljs-keyword">const</span> path = headers[<span class="hljs-string">':path'</span>];
<span class="hljs-comment">// ...</span>
stream.<span class="hljs-title function_">respond</span>({
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
});
stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>On the server side, user code will typically not listen for this event directly,
and would instead register a handler for the <code>'stream'</code> event emitted by the
<code>net.Server</code> or <code>tls.Server</code> instances returned by <code>http2.createServer()</code> and
<code>http2.createSecureServer()</code>, respectively, as in the example below:</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-comment">// Create an unencrypted HTTP/2 server</span>
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<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> {
stream.<span class="hljs-title function_">respond</span>({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
});
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));
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>);
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-comment">// Create an unencrypted HTTP/2 server</span>
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<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> {
stream.<span class="hljs-title function_">respond</span>({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
});
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));
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>);
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><button class="copy-button">copy</button></pre>
<p>Even though HTTP/2 streams and network sockets are not in a 1:1 correspondence,
a network error will destroy each individual stream and must be handled on the
stream level, as shown above.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>After the <code>http2session.setTimeout()</code> method is used to set the timeout period
for this <code>Http2Session</code>, the <code>'timeout'</code> event is emitted if there is no
activity on the <code>Http2Session</code> after the configured number of milliseconds.
Its listener does not expect any arguments.</p>
<pre><code class="language-js">session.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">2000</span>);
session.<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>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<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>
</ul>
<p>Value will be <code>undefined</code> if the <code>Http2Session</code> is not yet connected to a
socket, <code>h2c</code> if the <code>Http2Session</code> is not connected to a <code>TLSSocket</code>, or
will return the value of the connected <code>TLSSocket</code>'s own <code>alpnProtocol</code>
property.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
</ul>
<p>Gracefully closes the <code>Http2Session</code>, allowing any existing streams to
complete on their own and preventing new <code>Http2Stream</code> instances from being
created. Once closed, <code>http2session.destroy()</code> <em>might</em> be called if there
are no open <code>Http2Stream</code> instances.</p>
<p>If specified, the <code>callback</code> function is registered as a handler for the
<code>'close'</code> event.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Will be <code>true</code> if this <code>Http2Session</code> instance has been closed, otherwise
<code>false</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Will be <code>true</code> if this <code>Http2Session</code> instance is still connecting, will be set
to <code>false</code> before emitting <code>connect</code> event and/or calling the <code>http2.connect</code>
callback.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<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
due to an error.</li>
<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.
If unspecified, and <code>error</code> is not undefined, the default is <code>INTERNAL_ERROR</code>,
otherwise defaults to <code>NO_ERROR</code>.</li>
</ul>
<p>Immediately terminates the <code>Http2Session</code> and the associated <code>net.Socket</code> or
<code>tls.TLSSocket</code>.</p>
<p>Once destroyed, the <code>Http2Session</code> will emit the <code>'close'</code> event. If <code>error</code>
is not undefined, an <code>'error'</code> event will be emitted immediately before the
<code>'close'</code> event.</p>
<p>If there are any remaining open <code>Http2Streams</code> associated with the
<code>Http2Session</code>, those will also be destroyed.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Will be <code>true</code> if this <code>Http2Session</code> instance has been destroyed and must no
longer be used, otherwise <code>false</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<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>
</ul>
<p>Value is <code>undefined</code> if the <code>Http2Session</code> session socket has not yet been
connected, <code>true</code> if the <code>Http2Session</code> is connected with a <code>TLSSocket</code>,
and <code>false</code> if the <code>Http2Session</code> is connected to any other kind of socket
or stream.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<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>
<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>
<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>
instance containing additional data to be carried within the <code>GOAWAY</code> frame.</li>
</ul>
<p>Transmits a <code>GOAWAY</code> frame to the connected peer <em>without</em> shutting down the
<code>Http2Session</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li>
</ul>
<p>A prototype-less object describing the current local settings of this
<code>Http2Session</code>. The local settings are local to <em>this</em> <code>Http2Session</code> instance.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<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>
</ul>
<p>If the <code>Http2Session</code> is connected to a <code>TLSSocket</code>, the <code>originSet</code> property
will return an <code>Array</code> of origins for which the <code>Http2Session</code> may be
considered authoritative.</p>
<p>The <code>originSet</code> property is only available when using a secure TLS connection.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Indicates whether the <code>Http2Session</code> is currently waiting for acknowledgment of
a sent <code>SETTINGS</code> frame. Will be <code>true</code> after calling the
<code>http2session.settings()</code> method. Will be <code>false</code> once all sent <code>SETTINGS</code>
frames have been acknowledged.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<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>
<tr><td>v8.9.3</td>
<td><p><span>Added in: v8.9.3</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Sends a <code>PING</code> frame to the connected HTTP/2 peer. A <code>callback</code> function must
be provided. The method will return <code>true</code> if the <code>PING</code> was sent, <code>false</code>
otherwise.</p>
<p>The maximum number of outstanding (unacknowledged) pings is determined by the
<code>maxOutstandingPings</code> configuration option. The default maximum is 10.</p>
<p>If provided, the <code>payload</code> must be a <code>Buffer</code>, <code>TypedArray</code>, or <code>DataView</code>
containing 8 bytes of data that will be transmitted with the <code>PING</code> and
returned with the ping acknowledgment.</p>
<p>The callback will be invoked with three arguments: an error argument that will
be <code>null</code> if the <code>PING</code> was successfully acknowledged, a <code>duration</code> argument
that reports the number of milliseconds elapsed since the ping was sent and the
acknowledgment was received, and a <code>Buffer</code> containing the 8-byte <code>PING</code>
payload.</p>
<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> {
<span class="hljs-keyword">if</span> (!err) {
<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>);
<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>);
}
});</code> <button class="copy-button">copy</button></pre>
<p>If the <code>payload</code> argument is not specified, the default payload will be the
64-bit timestamp (little endian) marking the start of the <code>PING</code> duration.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<p>Calls <a href="net.html#socketref"><code>ref()</code></a> on this <code>Http2Session</code>
instance's underlying <a href="net.html#class-netsocket"><code>net.Socket</code></a>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li>
</ul>
<p>A prototype-less object describing the current remote settings of this
<code>Http2Session</code>. The remote settings are set by the <em>connected</em> HTTP/2 peer.</p>
<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>
<div class="api_metadata">
<span>Added in: v15.3.0, v14.18.0</span>
</div>
<ul>
<li><code>windowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>Sets the local endpoint's window size.
The <code>windowSize</code> is the total window size to set, not
the delta.</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
<span class="hljs-keyword">const</span> expectedWindowSize = <span class="hljs-number">2</span> ** <span class="hljs-number">20</span>;
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
<span class="hljs-comment">// Set local window size to be 2 ** 20</span>
session.<span class="hljs-title function_">setLocalWindowSize</span>(expectedWindowSize);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
<span class="hljs-keyword">const</span> expectedWindowSize = <span class="hljs-number">2</span> ** <span class="hljs-number">20</span>;
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
<span class="hljs-comment">// Set local window size to be 2 ** 20</span>
session.<span class="hljs-title function_">setLocalWindowSize</span>(expectedWindowSize);
});</code><button class="copy-button">copy</button></pre>
<p>For http2 clients the proper event is either <code>'connect'</code> or <code>'remoteSettings'</code>.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<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>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
</ul>
<p>Used to set a callback function that is called when there is no activity on
the <code>Http2Session</code> after <code>msecs</code> milliseconds. The given <code>callback</code> is
registered as a listener on the <code>'timeout'</code> event.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type"><tls.TLSSocket></a></li>
</ul>
<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but
limits available methods to ones safe to use with HTTP/2.</p>
<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
an error with code <code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See
<a href="#http2session-and-sockets"><code>Http2Session</code> and Sockets</a> for more information.</p>
<p><code>setTimeout</code> method will be called on this <code>Http2Session</code>.</p>
<p>All other interactions will be routed directly to the socket.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>Provides miscellaneous information about the current state of the
<code>Http2Session</code>.</p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<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)
flow control window size for the <code>Http2Session</code>.</li>
<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
that have been received since the last flow control <code>WINDOW_UPDATE</code>.</li>
<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
next time a new <code>Http2Stream</code> is created by this <code>Http2Session</code>.</li>
<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
send without receiving a <code>WINDOW_UPDATE</code>.</li>
<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>
for which a <code>HEADERS</code> or <code>DATA</code> frame was most recently received.</li>
<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>
may send without receiving a <code>WINDOW_UPDATE</code>.</li>
<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
outbound queue for this <code>Http2Session</code>.</li>
<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
outbound header compression state table.</li>
<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
inbound header compression state table.</li>
</ul>
</li>
</ul>
<p>An object describing the current status of this <code>Http2Session</code>.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<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>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li>
<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
right away if the session is already connected.
<ul>
<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>
<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a> The updated <code>settings</code> object.</li>
<li><code>duration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
</ul>
</li>
</ul>
<p>Updates the current local settings for this <code>Http2Session</code> and sends a new
<code>SETTINGS</code> frame to the connected HTTP/2 peer.</p>
<p>Once called, the <code>http2session.pendingSettingsAck</code> property will be <code>true</code>
while the session is waiting for the remote peer to acknowledge the new
settings.</p>
<p>The new settings will not become effective until the <code>SETTINGS</code> acknowledgment
is received and the <code>'localSettings'</code> event is emitted. It is possible to send
multiple <code>SETTINGS</code> frames while acknowledgment is still pending.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>The <code>http2session.type</code> will be equal to
<code>http2.constants.NGHTTP2_SESSION_SERVER</code> if this <code>Http2Session</code> instance is a
server, and <code>http2.constants.NGHTTP2_SESSION_CLIENT</code> if the instance is a
client.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<p>Calls <a href="net.html#socketunref"><code>unref()</code></a> on this <code>Http2Session</code>
instance's underlying <a href="net.html#class-netsocket"><code>net.Socket</code></a>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends: <a href="http2.html#class-http2session" class="type"><Http2Session></a></li>
</ul>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<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
defined by <a href="https://tools.ietf.org/html/rfc7838">RFC 7838</a>.</li>
<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
the origin (or an <code>Object</code> with an <code>origin</code> property) or the numeric
identifier of an active <code>Http2Stream</code> as given by the <code>http2stream.id</code>
property.</li>
</ul>
<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>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
<span class="hljs-comment">// Set altsvc for origin https://example.org:80</span>
session.<span class="hljs-title function_">altsvc</span>(<span class="hljs-string">'h2=":8000"'</span>, <span class="hljs-string">'https://example.org:80'</span>);
});
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
<span class="hljs-comment">// Set altsvc for a specific stream</span>
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>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
<span class="hljs-comment">// Set altsvc for origin https://example.org:80</span>
session.<span class="hljs-title function_">altsvc</span>(<span class="hljs-string">'h2=":8000"'</span>, <span class="hljs-string">'https://example.org:80'</span>);
});
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
<span class="hljs-comment">// Set altsvc for a specific stream</span>
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>);
});</code><button class="copy-button">copy</button></pre>
<p>Sending an <code>ALTSVC</code> frame with a specific stream ID indicates that the alternate
service is associated with the origin of the given <code>Http2Stream</code>.</p>
<p>The <code>alt</code> and origin string <em>must</em> contain only ASCII bytes and are
strictly interpreted as a sequence of ASCII bytes. The special value <code>'clear'</code>
may be passed to clear any previously set alternative service for a given
domain.</p>
<p>When a string is passed for the <code>originOrStream</code> argument, it will be parsed as
a URL and the origin will be derived. For instance, the origin for the
HTTP URL <code>'https://example.org/foo/bar'</code> is the ASCII string
<code>'https://example.org'</code>. An error will be thrown if either the given string
cannot be parsed as a URL or if a valid origin cannot be derived.</p>
<p>A <code>URL</code> object, or any object with an <code>origin</code> property, may be passed as
<code>originOrStream</code>, in which case the value of the <code>origin</code> property will be
used. The value of the <code>origin</code> property <em>must</em> be a properly serialized
ASCII origin.</p>
<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>
<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
ASCII string containing a comma-delimited list of "alternative" protocols
associated with a specific host and port.</p>
<p>For example, the value <code>'h2="example.org:81"'</code> indicates that the HTTP/2
protocol is available on the host <code>'example.org'</code> on TCP/IP port 81. The
host and port <em>must</em> be contained within the quote (<code>"</code>) characters.</p>
<p>Multiple alternatives may be specified, for instance: <code>'h2="example.org:81", h2=":82"'</code>.</p>
<p>The protocol identifier (<code>'h2'</code> in the examples) may be any valid
<a href="https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids">ALPN Protocol ID</a>.</p>
<p>The syntax of these values is not validated by the Node.js implementation and
are passed through as provided by the user or received from the peer.</p>
<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>
<div class="api_metadata">
<span>Added in: v10.12.0</span>
</div>
<ul>
<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
separate arguments.</li>
</ul>
<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
to advertise the set of origins for which the server is capable of providing
authoritative responses.</p>
<pre class="with-48-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createSecureServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptionsSomehow</span>();
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureServer</span>(options);
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
stream.<span class="hljs-title function_">respond</span>();
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
session.<span class="hljs-title function_">origin</span>(<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptionsSomehow</span>();
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
stream.<span class="hljs-title function_">respond</span>();
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
session.<span class="hljs-title function_">origin</span>(<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>);
});</code><button class="copy-button">copy</button></pre>
<p>When a string is passed as an <code>origin</code>, it will be parsed as a URL and the
origin will be derived. For instance, the origin for the HTTP URL
<code>'https://example.org/foo/bar'</code> is the ASCII string
<code>'https://example.org'</code>. An error will be thrown if either the given string
cannot be parsed as a URL or if a valid origin cannot be derived.</p>
<p>A <code>URL</code> object, or any object with an <code>origin</code> property, may be passed as
an <code>origin</code>, in which case the value of the <code>origin</code> property will be
used. The value of the <code>origin</code> property <em>must</em> be a properly serialized
ASCII origin.</p>
<p>Alternatively, the <code>origins</code> option may be used when creating a new HTTP/2
server using the <code>http2.createSecureServer()</code> method:</p>
<pre class="with-48-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createSecureServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptionsSomehow</span>();
options.<span class="hljs-property">origins</span> = [<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>];
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureServer</span>(options);
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
stream.<span class="hljs-title function_">respond</span>();
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptionsSomehow</span>();
options.<span class="hljs-property">origins</span> = [<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>];
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
stream.<span class="hljs-title function_">respond</span>();
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends: <a href="http2.html#class-http2session" class="type"><Http2Session></a></li>
</ul>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>streamId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>The <code>'altsvc'</code> event is emitted whenever an <code>ALTSVC</code> frame is received by
the client. The event is emitted with the <code>ALTSVC</code> value, origin, and stream
ID. If no <code>origin</code> is provided in the <code>ALTSVC</code> frame, <code>origin</code> will
be an empty string.</p>
<pre class="with-52-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> client = <span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://example.org'</span>);
client.<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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(alt);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(origin);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(streamId);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://example.org'</span>);
client.<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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(alt);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(origin);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(streamId);
});</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v10.12.0</span>
</div>
<ul>
<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li>
</ul>
<p>The <code>'origin'</code> event is emitted whenever an <code>ORIGIN</code> frame is received by
the client. The event is emitted with an array of <code>origin</code> strings. The
<code>http2session.originSet</code> will be updated to include the received
origins.</p>
<pre class="with-52-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> client = <span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://example.org'</span>);
client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'origin'</span>, <span class="hljs-function">(<span class="hljs-params">origins</span>) =></span> {
<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++)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(origins[n]);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://example.org'</span>);
client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'origin'</span>, <span class="hljs-function">(<span class="hljs-params">origins</span>) =></span> {
<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++)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(origins[n]);
});</code><button class="copy-button">copy</button></pre>
<p>The <code>'origin'</code> event is only emitted when using a secure TLS connection.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>
<p><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></p>
</li>
<li>
<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p>
<ul>
<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
be closed initially, such as when sending a <code>GET</code> request that should not
expect a payload body.</li>
<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,
the created stream is made the sole direct dependency of the parent, with
all other existing dependents made a dependent of the newly created stream.
<strong>Default:</strong> <code>false</code>.</li>
<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
created stream is dependent on.</li>
<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
to other streams with the same <code>parent</code>. The value is a number between <code>1</code>
and <code>256</code> (inclusive).</li>
<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
<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An AbortSignal that may be used to abort an ongoing
request.</li>
</ul>
</li>
<li>
<p>Returns: <a href="http2.html#class-clienthttp2stream" class="type"><ClientHttp2Stream></a></p>
</li>
</ul>
<p>For HTTP/2 Client <code>Http2Session</code> instances only, the <code>http2session.request()</code>
creates and returns an <code>Http2Stream</code> instance that can be used to send an
HTTP/2 request to the connected server.</p>
<p>When a <code>ClientHttp2Session</code> is first created, the socket may not yet be
connected. if <code>clienthttp2session.request()</code> is called during this time, the
actual request will be deferred until the socket is ready to go.
If the <code>session</code> is closed before the actual request be executed, an
<code>ERR_HTTP2_GOAWAY_SESSION</code> is thrown.</p>
<p>This method is only available if <code>http2session.type</code> is equal to
<code>http2.constants.NGHTTP2_SESSION_CLIENT</code>.</p>
<pre class="with-62-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { connect, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> clientSession = <span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:1234'</span>);
<span class="hljs-keyword">const</span> {
<span class="hljs-title class_">HTTP2</span>_HEADER_PATH,
<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS,
} = constants;
<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> });
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]);
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> });
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { <span class="hljs-comment">/* .. */</span> });
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> clientSession = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:1234'</span>);
<span class="hljs-keyword">const</span> {
<span class="hljs-title class_">HTTP2</span>_HEADER_PATH,
<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS,
} = http2.<span class="hljs-property">constants</span>;
<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> });
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]);
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> });
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { <span class="hljs-comment">/* .. */</span> });
});</code><button class="copy-button">copy</button></pre>
<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event
is emitted immediately after queuing the last chunk of payload data to be sent.
The <code>http2stream.sendTrailers()</code> method can then be called to send trailing
headers to the peer.</p>
<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
close when the final <code>DATA</code> frame is transmitted. User code must call either
<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
<code>Http2Stream</code>.</p>
<p>When <code>options.signal</code> is set with an <code>AbortSignal</code> and then <code>abort</code> on the
corresponding <code>AbortController</code> is called, the request will emit an <code>'error'</code>
event with an <code>AbortError</code> error.</p>
<p>The <code>:method</code> and <code>:path</code> pseudo-headers are not specified within <code>headers</code>,
they respectively default to:</p>
<ul>
<li><code>:method</code> = <code>'GET'</code></li>
<li><code>:path</code> = <code>/</code></li>
</ul>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends: <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>Each instance of the <code>Http2Stream</code> class represents a bidirectional HTTP/2
communications stream over an <code>Http2Session</code> instance. Any single <code>Http2Session</code>
may have up to 2<sup>31</sup>-1 <code>Http2Stream</code> instances over its lifetime.</p>
<p>User code will not construct <code>Http2Stream</code> instances directly. Rather, these
are created, managed, and provided to user code through the <code>Http2Session</code>
instance. On the server, <code>Http2Stream</code> instances are created either in response
to an incoming HTTP request (and handed off to user code via the <code>'stream'</code>
event), or in response to a call to the <code>http2stream.pushStream()</code> method.
On the client, <code>Http2Stream</code> instances are created and returned when either the
<code>http2session.request()</code> method is called, or in response to an incoming
<code>'push'</code> event.</p>
<p>The <code>Http2Stream</code> class is a base for the <a href="#class-serverhttp2stream"><code>ServerHttp2Stream</code></a> and
<a href="#class-clienthttp2stream"><code>ClientHttp2Stream</code></a> classes, each of which is used specifically by either
the Server or Client side, respectively.</p>
<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
<code>Duplex</code> is used to send data to the connected peer, while the <code>Readable</code> side
is used to receive data sent by the connected peer.</p>
<p>The default text character encoding for an <code>Http2Stream</code> is UTF-8. When using an
<code>Http2Stream</code> to send text, use the <code>'content-type'</code> header to set the character
encoding.</p>
<pre><code class="language-js">stream.<span class="hljs-title function_">respond</span>({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
});</code> <button class="copy-button">copy</button></pre>
<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>
<h6>Creation<span><a class="mark" href="#creation" id="creation">#</a></span><a aria-hidden="true" class="legacy" id="http2_creation"></a></h6>
<p>On the server side, instances of <a href="#class-serverhttp2stream"><code>ServerHttp2Stream</code></a> are created either
when:</p>
<ul>
<li>A new HTTP/2 <code>HEADERS</code> frame with a previously unused stream ID is received;</li>
<li>The <code>http2stream.pushStream()</code> method is called.</li>
</ul>
<p>On the client side, instances of <a href="#class-clienthttp2stream"><code>ClientHttp2Stream</code></a> are created when the
<code>http2session.request()</code> method is called.</p>
<p>On the client, the <code>Http2Stream</code> instance returned by <code>http2session.request()</code>
may not be immediately ready for use if the parent <code>Http2Session</code> has not yet
been fully established. In such cases, operations called on the <code>Http2Stream</code>
will be buffered until the <code>'ready'</code> event is emitted. User code should rarely,
if ever, need to handle the <code>'ready'</code> event directly. The ready status of an
<code>Http2Stream</code> can be determined by checking the value of <code>http2stream.id</code>. If
the value is <code>undefined</code>, the stream is not yet ready for use.</p>
<h6>Destruction<span><a class="mark" href="#destruction" id="destruction">#</a></span><a aria-hidden="true" class="legacy" id="http2_destruction"></a></h6>
<p>All <a href="#class-http2stream"><code>Http2Stream</code></a> instances are destroyed either when:</p>
<ul>
<li>An <code>RST_STREAM</code> frame for the stream is received by the connected peer,
and (for client streams only) pending data has been read.</li>
<li>The <code>http2stream.close()</code> method is called, and (for client streams only)
pending data has been read.</li>
<li>The <code>http2stream.destroy()</code> or <code>http2session.destroy()</code> methods are called.</li>
</ul>
<p>When an <code>Http2Stream</code> instance is destroyed, an attempt will be made to send an
<code>RST_STREAM</code> frame to the connected peer.</p>
<p>When the <code>Http2Stream</code> instance is destroyed, the <code>'close'</code> event will
be emitted. Because <code>Http2Stream</code> is an instance of <code>stream.Duplex</code>, the
<code>'end'</code> event will also be emitted if the stream data is currently flowing.
The <code>'error'</code> event may also be emitted if <code>http2stream.destroy()</code> was called
with an <code>Error</code> passed as the first argument.</p>
<p>After the <code>Http2Stream</code> has been destroyed, the <code>http2stream.destroyed</code>
property will be <code>true</code> and the <code>http2stream.rstCode</code> property will specify the
<code>RST_STREAM</code> error code. The <code>Http2Stream</code> instance is no longer usable once
destroyed.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>The <code>'aborted'</code> event is emitted whenever a <code>Http2Stream</code> instance is
abnormally aborted in mid-communication.
Its listener does not expect any arguments.</p>
<p>The <code>'aborted'</code> event will only be emitted if the <code>Http2Stream</code> writable side
has not been ended.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>The <code>'close'</code> event is emitted when the <code>Http2Stream</code> is destroyed. Once
this event is emitted, the <code>Http2Stream</code> instance is no longer usable.</p>
<p>The HTTP/2 error code used when closing the stream can be retrieved using
the <code>http2stream.rstCode</code> property. If the code is any value other than
<code>NGHTTP2_NO_ERROR</code> (<code>0</code>), an <code>'error'</code> event will have also been emitted.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p>The <code>'error'</code> event is emitted when an error occurs during the processing of
an <code>Http2Stream</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<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>
<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>
<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
stream).</li>
</ul>
<p>The <code>'frameError'</code> event is emitted when an error occurs while attempting to
send a frame. When invoked, the handler function will receive an integer
argument identifying the frame type, and an integer argument identifying the
error code. The <code>Http2Stream</code> instance will be destroyed immediately after the
<code>'frameError'</code> event is emitted.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>The <code>'ready'</code> event is emitted when the <code>Http2Stream</code> has been opened, has
been assigned an <code>id</code>, and can be used. The listener does not expect any
arguments.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>The <code>'timeout'</code> event is emitted after no activity is received for this
<code>Http2Stream</code> within the number of milliseconds set using
<code>http2stream.setTimeout()</code>.
Its listener does not expect any arguments.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li>
<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>
</ul>
<p>The <code>'trailers'</code> event is emitted when a block of headers associated with
trailing header fields is received. The listener callback is passed the
<a href="#headers-object">HTTP/2 Headers Object</a> and flags associated with the headers.</p>
<p>This event might not be emitted if <code>http2stream.end()</code> is called
before trailers are received and the incoming data is not being read or
listened for.</p>
<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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers);
});</code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<p>The <code>'wantTrailers'</code> event is emitted when the <code>Http2Stream</code> has queued the
final <code>DATA</code> frame to be sent on a frame and the <code>Http2Stream</code> is ready to send
trailing headers. When initiating a request or response, the <code>waitForTrailers</code>
option must be set for this event to be emitted.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Set to <code>true</code> if the <code>Http2Stream</code> instance was aborted abnormally. When set,
the <code>'aborted'</code> event will have been emitted.</p>
<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>
<div class="api_metadata">
<span>Added in: v11.2.0, v10.16.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>This property shows the number of characters currently buffered to be written.
See <a href="net.html#socketbuffersize"><code>net.Socket.bufferSize</code></a> for details.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<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>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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.
<strong>Default:</strong> <code>http2.constants.NGHTTP2_NO_ERROR</code> (<code>0x00</code>).</li>
<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
<code>'close'</code> event.</li>
</ul>
<p>Closes the <code>Http2Stream</code> instance by sending an <code>RST_STREAM</code> frame to the
connected HTTP/2 peer.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has been closed.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has been destroyed and is no longer
usable.</p>
<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>
<div class="api_metadata">
<span>Added in: v10.11.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Set to <code>true</code> if the <code>END_STREAM</code> flag was set in the request or response
HEADERS frame received, indicating that no additional data should be received
and the readable side of the <code>Http2Stream</code> will be closed.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<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>
</ul>
<p>The numeric stream identifier of this <code>Http2Stream</code> instance. Set to <code>undefined</code>
if the stream identifier has not yet been assigned.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has not yet been assigned a
numeric stream identifier.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<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,
this stream is made the sole direct dependency of the parent, with
all other existing dependents made a dependent of this stream. <strong>Default:</strong>
<code>false</code>.</li>
<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
is dependent on.</li>
<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
to other streams with the same <code>parent</code>. The value is a number between <code>1</code>
and <code>256</code> (inclusive).</li>
<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
sending a <code>PRIORITY</code> frame to the connected peer.</li>
</ul>
</li>
</ul>
<p>Updates the priority for this <code>Http2Stream</code> instance.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<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
destroyed after either receiving an <code>RST_STREAM</code> frame from the connected peer,
calling <code>http2stream.close()</code>, or <code>http2stream.destroy()</code>. Will be
<code>undefined</code> if the <code>Http2Stream</code> has not been closed.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.5.0</span>
</div>
<ul>
<li><a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
</ul>
<p>An object containing the outbound headers sent for this <code>Http2Stream</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.5.0</span>
</div>
<ul>
<li><a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object[]></a></li>
</ul>
<p>An array of objects containing the outbound informational (additional) headers
sent for this <code>Http2Stream</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v9.5.0</span>
</div>
<ul>
<li><a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
</ul>
<p>An object containing the outbound trailers sent for this <code>HttpStream</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="http2.html#class-http2session" class="type"><Http2Session></a></li>
</ul>
<p>A reference to the <code>Http2Session</code> instance that owns this <code>Http2Stream</code>. The
value will be <code>undefined</code> after the <code>Http2Stream</code> instance is destroyed.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<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>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
</ul>
<pre class="with-56-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { connect, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> client = <span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://example.org:8000'</span>);
<span class="hljs-keyword">const</span> { <span class="hljs-title class_">NGHTTP2</span>_CANCEL } = constants;
<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> });
<span class="hljs-comment">// Cancel the stream if there's no activity after 5 seconds</span>
req.<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><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://example.org:8000'</span>);
<span class="hljs-keyword">const</span> { <span class="hljs-title class_">NGHTTP2</span>_CANCEL } = http2.<span class="hljs-property">constants</span>;
<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> });
<span class="hljs-comment">// Cancel the stream if there's no activity after 5 seconds</span>
req.<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>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>Provides miscellaneous information about the current state of the
<code>Http2Stream</code>.</p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<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
for this <code>Http2Stream</code> without receiving a <code>WINDOW_UPDATE</code>.</li>
<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
<code>Http2Stream</code> as determined by <code>nghttp2</code>.</li>
<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>
<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
remotely.</li>
<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>
instances that depend on this <code>Http2Stream</code> as specified using
<code>PRIORITY</code> frames.</li>
<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>
</ul>
</li>
</ul>
<p>A current state of this <code>Http2Stream</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
</ul>
<p>Sends a trailing <code>HEADERS</code> frame to the connected HTTP/2 peer. This method
will cause the <code>Http2Stream</code> to be immediately closed and must only be
called after the <code>'wantTrailers'</code> event has been emitted. When sending a
request or sending a response, the <code>options.waitForTrailers</code> option must be set
in order to keep the <code>Http2Stream</code> open after the final <code>DATA</code> frame so that
trailers can be sent.</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
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> });
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">xyz</span>: <span class="hljs-string">'abc'</span> });
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello World'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
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> });
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">xyz</span>: <span class="hljs-string">'abc'</span> });
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello World'</span>);
});</code><button class="copy-button">copy</button></pre>
<p>The HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header
fields (e.g. <code>':method'</code>, <code>':path'</code>, etc).</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends <a href="http2.html#class-http2stream" class="type"><Http2Stream></a></li>
</ul>
<p>The <code>ClientHttp2Stream</code> class is an extension of <code>Http2Stream</code> that is
used exclusively on HTTP/2 Clients. <code>Http2Stream</code> instances on the client
provide events such as <code>'response'</code> and <code>'push'</code> that are only relevant on
the client.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.5.0</span>
</div>
<p>Emitted when the server sends a <code>100 Continue</code> status, usually because
the request contained <code>Expect: 100-continue</code>. This is an instruction that
the client should send the request body.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>The <code>'headers'</code> event is emitted when an additional block of headers is received
for a stream, such as when a block of <code>1xx</code> informational headers is received.
The listener callback is passed the <a href="#headers-object">HTTP/2 Headers Object</a> and flags
associated with the headers.</p>
<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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers);
});</code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>The <code>'push'</code> event is emitted when response headers for a Server Push stream
are received. The listener callback is passed the <a href="#headers-object">HTTP/2 Headers Object</a> and
flags associated with the headers.</p>
<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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers);
});</code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>The <code>'response'</code> event is emitted when a response <code>HEADERS</code> frame has been
received for this stream from the connected HTTP/2 server. The listener is
invoked with two arguments: an <code>Object</code> containing the received
<a href="#headers-object">HTTP/2 Headers Object</a>, and flags associated with the headers.</p>
<pre class="with-50-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> client = <span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost'</span>);
<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> });
req.<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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-string">':status'</span>]);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost'</span>);
<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> });
req.<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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-string">':status'</span>]);
});</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends: <a href="http2.html#class-http2stream" class="type"><Http2Stream></a></li>
</ul>
<p>The <code>ServerHttp2Stream</code> class is an extension of <a href="#class-http2stream"><code>Http2Stream</code></a> that is
used exclusively on HTTP/2 Servers. <code>Http2Stream</code> instances on the server
provide additional methods such as <code>http2stream.pushStream()</code> and
<code>http2stream.respond()</code> that are only relevant on the server.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
</ul>
<p>Sends an additional informational <code>HEADERS</code> frame to the connected HTTP/2 peer.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>True if headers were sent, false otherwise (read-only).</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Read-only property mapped to the <code>SETTINGS_ENABLE_PUSH</code> flag of the remote
client's most recent <code>SETTINGS</code> frame. Will be <code>true</code> if the remote peer
accepts push streams, <code>false</code> otherwise. Settings are the same for every
<code>Http2Stream</code> in the same <code>Http2Session</code>.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<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>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<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,
the created stream is made the sole direct dependency of the parent, with
all other existing dependents made a dependent of the newly created stream.
<strong>Default:</strong> <code>false</code>.</li>
<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
created stream is dependent on.</li>
</ul>
</li>
<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
initiated.
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>pushStream</code> <a href="http2.html#class-serverhttp2stream" class="type"><ServerHttp2Stream></a> The returned <code>pushStream</code> object.</li>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> Headers object the <code>pushStream</code> was
initiated with.</li>
</ul>
</li>
</ul>
<p>Initiates a push stream. The callback is invoked with the new <code>Http2Stream</code>
instance created for the push stream passed as the second argument, or an
<code>Error</code> passed as the first argument.</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
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> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
pushStream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
pushStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some pushed data'</span>);
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
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> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
pushStream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
pushStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some pushed data'</span>);
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
});</code><button class="copy-button">copy</button></pre>
<p>Setting the weight of a push stream is not allowed in the <code>HEADERS</code> frame. Pass
a <code>weight</code> value to <code>http2stream.priority</code> with the <code>silent</code> option set to
<code>true</code> to enable server-side bandwidth balancing between concurrent streams.</p>
<p>Calling <code>http2stream.pushStream()</code> from within a pushed stream is not permitted
and will throw an error.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.5.0, v12.19.0</td>
<td><p>Allow explicitly setting date headers.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<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
include payload data.</li>
<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
<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
</ul>
</li>
</ul>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
});</code><button class="copy-button">copy</button></pre>
<p>Initiates a response. When the <code>options.waitForTrailers</code> option is set, the
<code>'wantTrailers'</code> event will be emitted immediately after queuing the last chunk
of payload data to be sent. The <code>http2stream.sendTrailers()</code> method can then be
used to sent trailing header fields to the peer.</p>
<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
close when the final <code>DATA</code> frame is transmitted. User code must call either
<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
<code>Http2Stream</code>.</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
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> });
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
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> });
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
});</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.5.0, v12.19.0</td>
<td><p>Allow explicitly setting date headers.</p></td></tr>
<tr><td>v12.12.0</td>
<td><p>The <code>fd</code> option may now be a <code>FileHandle</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>Any readable file descriptor, not necessarily for a regular file, is supported now.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<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
<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
<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>
<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>
</ul>
</li>
</ul>
<p>Initiates a response whose data is read from the given file descriptor. No
validation is performed on the given file descriptor. If an error occurs while
attempting to read data using the file descriptor, the <code>Http2Stream</code> will be
closed using an <code>RST_STREAM</code> frame using the standard <code>INTERNAL_ERROR</code> code.</p>
<p>When used, the <code>Http2Stream</code> object's <code>Duplex</code> interface will be closed
automatically.</p>
<pre class="with-57-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">import</span> { openSync, fstatSync, closeSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
<span class="hljs-keyword">const</span> fd = <span class="hljs-title function_">openSync</span>(<span class="hljs-string">'/some/file'</span>, <span class="hljs-string">'r'</span>);
<span class="hljs-keyword">const</span> stat = <span class="hljs-title function_">fstatSync</span>(fd);
<span class="hljs-keyword">const</span> headers = {
<span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
<span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(),
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
};
stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers);
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> <span class="hljs-title function_">closeSync</span>(fd));
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
<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>);
<span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</span>(fd);
<span class="hljs-keyword">const</span> headers = {
<span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
<span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(),
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
};
stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers);
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));
});</code><button class="copy-button">copy</button></pre>
<p>The optional <code>options.statCheck</code> function may be specified to give user code
an opportunity to set additional content headers based on the <code>fs.Stat</code> details
of the given fd. If the <code>statCheck</code> function is provided, the
<code>http2stream.respondWithFD()</code> method will perform an <code>fs.fstat()</code> call to
collect details on the provided file descriptor.</p>
<p>The <code>offset</code> and <code>length</code> options may be used to limit the response to a
specific range subset. This can be used, for instance, to support HTTP Range
requests.</p>
<p>The file descriptor or <code>FileHandle</code> is not closed when the stream is closed,
so it will need to be closed manually once it is no longer needed.
Using the same file descriptor concurrently for multiple streams
is not supported and may result in data loss. Re-using a file descriptor
after a stream has finished is supported.</p>
<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event
will be emitted immediately after queuing the last chunk of payload data to be
sent. The <code>http2stream.sendTrailers()</code> method can then be used to sent trailing
header fields to the peer.</p>
<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
close when the final <code>DATA</code> frame is transmitted. User code <em>must</em> call either
<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
<code>Http2Stream</code>.</p>
<pre class="with-57-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">import</span> { openSync, fstatSync, closeSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
<span class="hljs-keyword">const</span> fd = <span class="hljs-title function_">openSync</span>(<span class="hljs-string">'/some/file'</span>, <span class="hljs-string">'r'</span>);
<span class="hljs-keyword">const</span> stat = <span class="hljs-title function_">fstatSync</span>(fd);
<span class="hljs-keyword">const</span> headers = {
<span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
<span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(),
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
};
stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers, { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
});
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> <span class="hljs-title function_">closeSync</span>(fd));
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
<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>);
<span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</span>(fd);
<span class="hljs-keyword">const</span> headers = {
<span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
<span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(),
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
};
stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers, { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
});
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));
});</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.5.0, v12.19.0</td>
<td><p>Allow explicitly setting date headers.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>Any readable file, not necessarily a regular file, is supported now.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<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
error before send.</li>
<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
<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
<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>
<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>
</ul>
</li>
</ul>
<p>Sends a regular file as the response. The <code>path</code> must specify a regular file
or an <code>'error'</code> event will be emitted on the <code>Http2Stream</code> object.</p>
<p>When used, the <code>Http2Stream</code> object's <code>Duplex</code> interface will be closed
automatically.</p>
<p>The optional <code>options.statCheck</code> function may be specified to give user code
an opportunity to set additional content headers based on the <code>fs.Stat</code> details
of the given file:</p>
<p>If an error occurs while attempting to read the file data, the <code>Http2Stream</code>
will be closed using an <code>RST_STREAM</code> frame using the standard <code>INTERNAL_ERROR</code>
code. If the <code>onError</code> callback is defined, then it will be called. Otherwise
the stream will be destroyed.</p>
<p>Example using a file path:</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
<span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<span class="hljs-params">stat, headers</span>) {
headers[<span class="hljs-string">'last-modified'</span>] = stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>();
}
<span class="hljs-keyword">function</span> <span class="hljs-title function_">onError</span>(<span class="hljs-params">err</span>) {
<span class="hljs-comment">// stream.respond() can throw if the stream has been destroyed by</span>
<span class="hljs-comment">// the other side.</span>
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) {
stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">404</span> });
} <span class="hljs-keyword">else</span> {
stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">500</span> });
}
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-comment">// Perform actual error handling.</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
}
stream.<span class="hljs-title function_">end</span>();
}
stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
{ statCheck, onError });
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
<span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<span class="hljs-params">stat, headers</span>) {
headers[<span class="hljs-string">'last-modified'</span>] = stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>();
}
<span class="hljs-keyword">function</span> <span class="hljs-title function_">onError</span>(<span class="hljs-params">err</span>) {
<span class="hljs-comment">// stream.respond() can throw if the stream has been destroyed by</span>
<span class="hljs-comment">// the other side.</span>
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) {
stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">404</span> });
} <span class="hljs-keyword">else</span> {
stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">500</span> });
}
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-comment">// Perform actual error handling.</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
}
stream.<span class="hljs-title function_">end</span>();
}
stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
{ statCheck, onError });
});</code><button class="copy-button">copy</button></pre>
<p>The <code>options.statCheck</code> function may also be used to cancel the send operation
by returning <code>false</code>. For instance, a conditional request may check the stat
results to determine if the file has been modified to return an appropriate
<code>304</code> response:</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
<span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<span class="hljs-params">stat, headers</span>) {
<span class="hljs-comment">// Check the stat here...</span>
stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">304</span> });
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hljs-comment">// Cancel the send operation</span>
}
stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
{ statCheck });
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
<span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<span class="hljs-params">stat, headers</span>) {
<span class="hljs-comment">// Check the stat here...</span>
stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">304</span> });
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hljs-comment">// Cancel the send operation</span>
}
stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
{ statCheck });
});</code><button class="copy-button">copy</button></pre>
<p>The <code>content-length</code> header field will be automatically set.</p>
<p>The <code>offset</code> and <code>length</code> options may be used to limit the response to a
specific range subset. This can be used, for instance, to support HTTP Range
requests.</p>
<p>The <code>options.onError</code> function may also be used to handle all the errors
that could happen before the delivery of the file is initiated. The
default behavior is to destroy the stream.</p>
<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event
will be emitted immediately after queuing the last chunk of payload data to be
sent. The <code>http2stream.sendTrailers()</code> method can then be used to sent trailing
header fields to the peer.</p>
<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
close when the final <code>DATA</code> frame is transmitted. User code must call either
<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
<code>Http2Stream</code>.</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
{ <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
});
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
{ <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
});
});</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends: <a href="net.html#class-netserver" class="type"><net.Server></a></li>
</ul>
<p>Instances of <code>Http2Server</code> are created using the <code>http2.createServer()</code>
function. The <code>Http2Server</code> class is not exported directly by the
<code>node:http2</code> module.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.5.0</span>
</div>
<ul>
<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li>
<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li>
</ul>
<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
supplied a callback function, the <code>'checkContinue'</code> event is emitted each time
a request with an HTTP <code>Expect: 100-continue</code> is received. If this event is
not listened for, the server will automatically respond with a status
<code>100 Continue</code> as appropriate.</p>
<p>Handling this event involves calling <a href="#responsewritecontinue"><code>response.writeContinue()</code></a> if the
client should continue to send the request body, or generating an appropriate
HTTP response (e.g. 400 Bad Request) if the client should not continue to send
the request body.</p>
<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will
not be emitted.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>This event is emitted when a new TCP stream is established. <code>socket</code> is
typically an object of type <a href="net.html#class-netsocket"><code>net.Socket</code></a>. Usually users will not want to
access this event.</p>
<p>This event can also be explicitly emitted by users to inject connections
into the HTTP server. In that case, any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream can be passed.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li>
<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li>
</ul>
<p>Emitted each time there is a request. There may be multiple requests
per session. See the <a href="#compatibility-api">Compatibility API</a>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type"><ServerHttp2Session></a></li>
</ul>
<p>The <code>'session'</code> event is emitted when a new <code>Http2Session</code> is created by the
<code>Http2Server</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type"><ServerHttp2Session></a></li>
</ul>
<p>The <code>'sessionError'</code> event is emitted when an <code>'error'</code> event is emitted by
an <code>Http2Session</code> object associated with the <code>Http2Server</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>stream</code> <a href="http2.html#class-http2stream" class="type"><Http2Stream></a> A reference to the stream</li>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li>
<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>
<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
their respective values.</li>
</ul>
<p>The <code>'stream'</code> event is emitted when a <code>'stream'</code> event has been emitted by
an <code>Http2Session</code> associated with the server.</p>
<p>See also <a href="#event-stream"><code>Http2Session</code>'s <code>'stream'</code> event</a>.</p>
<pre class="with-53-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> {
<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD,
<span class="hljs-title class_">HTTP2</span>_HEADER_PATH,
<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS,
<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE,
} = constants;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<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> {
<span class="hljs-keyword">const</span> method = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD];
<span class="hljs-keyword">const</span> path = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_PATH];
<span class="hljs-comment">// ...</span>
stream.<span class="hljs-title function_">respond</span>({
[<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]: <span class="hljs-number">200</span>,
[<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
});
stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> {
<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD,
<span class="hljs-title class_">HTTP2</span>_HEADER_PATH,
<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS,
<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE,
} = http2.<span class="hljs-property">constants</span>;
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<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> {
<span class="hljs-keyword">const</span> method = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD];
<span class="hljs-keyword">const</span> path = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_PATH];
<span class="hljs-comment">// ...</span>
stream.<span class="hljs-title function_">respond</span>({
[<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]: <span class="hljs-number">200</span>,
[<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
});
stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
});</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.0.0</td>
<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for
a given number of milliseconds set using <code>http2server.setTimeout()</code>.
<strong>Default:</strong> 0 (no timeout)</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
</ul>
<p>Stops the server from establishing new sessions. This does not prevent new
request streams from being created due to the persistent nature of HTTP/2
sessions. To gracefully shut down the server, call <a href="#http2sessionclosecallback"><code>http2session.close()</code></a> on
all active sessions.</p>
<p>If <code>callback</code> is provided, it is not invoked until all active sessions have been
closed, although the server has already stopped allowing new sessions. See
<a href="net.html#serverclosecallback"><code>net.Server.close()</code></a> for more details.</p>
<h5><code>server[Symbol.asyncDispose]()</code><span><a class="mark" href="#serversymbolasyncdispose" id="serversymbolasyncdispose">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_symbol_asyncdispose"></a></h5>
<div class="api_metadata">
<span>Added in: v20.4.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<p>Calls <a href="#serverclosecallback"><code>server.close()</code></a> and returns a promise that fulfills when the
server has closed.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<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>
<tr><td>v13.0.0</td>
<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="http2.html#class-http2server" class="type"><Http2Server></a></li>
</ul>
<p>Used to set the timeout value for http2 server requests,
and sets a callback function that is called when there is no activity
on the <code>Http2Server</code> after <code>msecs</code> milliseconds.</p>
<p>The given callback is registered as a listener on the <code>'timeout'</code> event.</p>
<p>In case if <code>callback</code> is not a function, a new <code>ERR_INVALID_ARG_TYPE</code>
error will be thrown.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.0.0</td>
<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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>
</ul>
<p>The number of milliseconds of inactivity before a socket is presumed
to have timed out.</p>
<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p>
<p>The socket timeout logic is set up on connection, so changing this
value only affects new connections to the server, not any existing connections.</p>
<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>
<div class="api_metadata">
<span>Added in: v15.1.0, v14.17.0</span>
</div>
<ul>
<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li>
</ul>
<p>Used to update the server with the provided settings.</p>
<p>Throws <code>ERR_HTTP2_INVALID_SETTING_VALUE</code> for invalid <code>settings</code> values.</p>
<p>Throws <code>ERR_INVALID_ARG_TYPE</code> for invalid <code>settings</code> argument.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends: <a href="tls.html#class-tlsserver" class="type"><tls.Server></a></li>
</ul>
<p>Instances of <code>Http2SecureServer</code> are created using the
<code>http2.createSecureServer()</code> function. The <code>Http2SecureServer</code> class is not
exported directly by the <code>node:http2</code> module.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.5.0</span>
</div>
<ul>
<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li>
<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li>
</ul>
<p>If a <a href="#event-request"><code>'request'</code></a> listener is registered or <a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a>
is supplied a callback function, the <code>'checkContinue'</code> event is emitted each
time a request with an HTTP <code>Expect: 100-continue</code> is received. If this event
is not listened for, the server will automatically respond with a status
<code>100 Continue</code> as appropriate.</p>
<p>Handling this event involves calling <a href="#responsewritecontinue"><code>response.writeContinue()</code></a> if the
client should continue to send the request body, or generating an appropriate
HTTP response (e.g. 400 Bad Request) if the client should not continue to send
the request body.</p>
<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will
not be emitted.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>This event is emitted when a new TCP stream is established, before the TLS
handshake begins. <code>socket</code> is typically an object of type <a href="net.html#class-netsocket"><code>net.Socket</code></a>.
Usually users will not want to access this event.</p>
<p>This event can also be explicitly emitted by users to inject connections
into the HTTP server. In that case, any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream can be passed.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li>
<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li>
</ul>
<p>Emitted each time there is a request. There may be multiple requests
per session. See the <a href="#compatibility-api">Compatibility API</a>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type"><ServerHttp2Session></a></li>
</ul>
<p>The <code>'session'</code> event is emitted when a new <code>Http2Session</code> is created by the
<code>Http2SecureServer</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type"><ServerHttp2Session></a></li>
</ul>
<p>The <code>'sessionError'</code> event is emitted when an <code>'error'</code> event is emitted by
an <code>Http2Session</code> object associated with the <code>Http2SecureServer</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>stream</code> <a href="http2.html#class-http2stream" class="type"><Http2Stream></a> A reference to the stream</li>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li>
<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>
<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
their respective values.</li>
</ul>
<p>The <code>'stream'</code> event is emitted when a <code>'stream'</code> event has been emitted by
an <code>Http2Session</code> associated with the server.</p>
<p>See also <a href="#event-stream"><code>Http2Session</code>'s <code>'stream'</code> event</a>.</p>
<pre class="with-59-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createSecureServer, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> {
<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD,
<span class="hljs-title class_">HTTP2</span>_HEADER_PATH,
<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS,
<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE,
} = constants;
<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getOptionsSomehow</span>();
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureServer</span>(options);
server.<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> {
<span class="hljs-keyword">const</span> method = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD];
<span class="hljs-keyword">const</span> path = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_PATH];
<span class="hljs-comment">// ...</span>
stream.<span class="hljs-title function_">respond</span>({
[<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]: <span class="hljs-number">200</span>,
[<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
});
stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> {
<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD,
<span class="hljs-title class_">HTTP2</span>_HEADER_PATH,
<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS,
<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE,
} = http2.<span class="hljs-property">constants</span>;
<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getOptionsSomehow</span>();
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
server.<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> {
<span class="hljs-keyword">const</span> method = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD];
<span class="hljs-keyword">const</span> path = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_PATH];
<span class="hljs-comment">// ...</span>
stream.<span class="hljs-title function_">respond</span>({
[<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]: <span class="hljs-number">200</span>,
[<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
});
stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
});</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for
a given number of milliseconds set using <code>http2secureServer.setTimeout()</code>.
<strong>Default:</strong> 2 minutes.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.0.0</td>
<td><p>This event will only be emitted if the client did not transmit an ALPN extension during the TLS handshake.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>The <code>'unknownProtocol'</code> event is emitted when a connecting client fails to
negotiate an allowed protocol (i.e. HTTP/2 or HTTP/1.1). The event handler
receives the socket for handling. If no listener is registered for this event,
the connection is terminated. A timeout may be specified using the
<code>'unknownProtocolTimeout'</code> option passed to <a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a>.</p>
<p>In earlier versions of Node.js, this event would be emitted if <code>allowHTTP1</code> is
<code>false</code> and, during the TLS handshake, the client either does not send an ALPN
extension or sends an ALPN extension that does not include HTTP/2 (<code>h2</code>). Newer
versions of Node.js only emit this event if <code>allowHTTP1</code> is <code>false</code> and the
client does not send an ALPN extension. If the client sends an ALPN extension
that does not include HTTP/2 (or HTTP/1.1 if <code>allowHTTP1</code> is <code>true</code>), the TLS
handshake will fail and no secure connection will be established.</p>
<p>See the <a href="#compatibility-api">Compatibility API</a>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
</ul>
<p>Stops the server from establishing new sessions. This does not prevent new
request streams from being created due to the persistent nature of HTTP/2
sessions. To gracefully shut down the server, call <a href="#http2sessionclosecallback"><code>http2session.close()</code></a> on
all active sessions.</p>
<p>If <code>callback</code> is provided, it is not invoked until all active sessions have been
closed, although the server has already stopped allowing new sessions. See
<a href="tls.html#serverclosecallback"><code>tls.Server.close()</code></a> for more details.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<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>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="http2.html#class-http2secureserver" class="type"><Http2SecureServer></a></li>
</ul>
<p>Used to set the timeout value for http2 secure server requests,
and sets a callback function that is called when there is no activity
on the <code>Http2SecureServer</code> after <code>msecs</code> milliseconds.</p>
<p>The given callback is registered as a listener on the <code>'timeout'</code> event.</p>
<p>In case if <code>callback</code> is not a function, a new <code>ERR_INVALID_ARG_TYPE</code>
error will be thrown.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.0.0</td>
<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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>
</ul>
<p>The number of milliseconds of inactivity before a socket is presumed
to have timed out.</p>
<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p>
<p>The socket timeout logic is set up on connection, so changing this
value only affects new connections to the server, not any existing connections.</p>
<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>
<div class="api_metadata">
<span>Added in: v15.1.0, v14.17.0</span>
</div>
<ul>
<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li>
</ul>
<p>Used to update the server with the provided settings.</p>
<p>Throws <code>ERR_HTTP2_INVALID_SETTING_VALUE</code> for invalid <code>settings</code> values.</p>
<p>Throws <code>ERR_INVALID_ARG_TYPE</code> for invalid <code>settings</code> argument.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.10.0</td>
<td><p>Added <code>streamResetBurst</code> and <code>streamResetRate</code>.</p></td></tr>
<tr><td>v13.0.0</td>
<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>
<tr><td>v13.3.0, v12.16.0</td>
<td><p>Added <code>maxSessionRejectedStreams</code> option with a default of 100.</p></td></tr>
<tr><td>v13.3.0, v12.16.0</td>
<td><p>Added <code>maxSessionInvalidFrames</code> option with a default of 1000.</p></td></tr>
<tr><td>v12.4.0</td>
<td><p>The <code>options</code> parameter now supports <code>net.createServer()</code> options.</p></td></tr>
<tr><td>v15.10.0, v14.16.0, v12.21.0, v10.24.0</td>
<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr>
<tr><td>v14.4.0, v12.18.0, v10.21.0</td>
<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr>
<tr><td>v9.6.0</td>
<td><p>Added the <code>Http1IncomingMessage</code> and <code>Http1ServerResponse</code> option.</p></td></tr>
<tr><td>v8.9.3</td>
<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr>
<tr><td>v8.9.3</td>
<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<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
for deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li>
<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
<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li>
<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>
is permitted to use. The value is expressed in terms of number of megabytes,
e.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>.
This is a credit based limit, existing <code>Http2Stream</code>s may cause this
limit to be exceeded, but new <code>Http2Stream</code> instances will be rejected
while this limit is exceeded. The current number of <code>Http2Stream</code> sessions,
the current memory use of the header compression tables, current data
queued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all
counted towards the current limit. <strong>Default:</strong> <code>10</code>.</li>
<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.
This is similar to <a href="http.html#servermaxheaderscount"><code>server.maxHeadersCount</code></a> or
<a href="http.html#requestmaxheaderscount"><code>request.maxHeadersCount</code></a> in the <code>node:http</code> module. The minimum value
is <code>4</code>. <strong>Default:</strong> <code>128</code>.</li>
<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,
unacknowledged pings. <strong>Default:</strong> <code>10</code>.</li>
<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
serialized, compressed block of headers. Attempts to send headers that
exceed this limit will result in a <code>'frameError'</code> event being emitted
and the stream being closed and destroyed.
While this sets the maximum allowed size to the entire block of headers,
<code>nghttp2</code> (the internal http2 library) has a limit of <code>65536</code>
for each decompressed key/value pair.</li>
<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
padding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong>
<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of:
<ul>
<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li>
<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding,
determined by the internal implementation, is applied.</li>
<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough
padding to ensure that the total frame length, including the 9-byte
header, is a multiple of 8. For each frame, there is a maximum allowed
number of padding bytes that is determined by current flow control state
and settings. If this maximum is less than the calculated amount needed to
ensure alignment, the maximum is used and the total frame length is not
necessarily aligned at 8 bytes.</li>
</ul>
</li>
<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
streams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will
be overridden if the remote peer sets its own value for
<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li>
<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
frames that will be tolerated before the session is closed.
<strong>Default:</strong> <code>1000</code>.</li>
<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
upon creation streams that will be tolerated before the session is closed.
Each rejection is associated with an <code>NGHTTP2_ENHANCE_YOUR_CALM</code>
error that should tell the peer to not open any more streams, continuing
to open streams is therefore regarded as a sign of a misbehaving peer.
<strong>Default:</strong> <code>100</code>.</li>
<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a> The initial settings to send to the
remote peer upon connection.</li>
<li><code>streamResetBurst</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> and <code>streamResetRate</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the rate
limit for the incoming stream reset (RST_STREAM frame). Both settings must
be set to have any effect, and default to 1000 and 33 respectively.</li>
<li><code>remoteCustomSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> The array of integer values determines the
settings types, which are included in the <code>CustomSettings</code>-property of
the received remoteSettings. Please see the <code>CustomSettings</code>-property of
the <code>Http2Settings</code> object for more information,
on the allowed setting types.</li>
<li><code>Http1IncomingMessage</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Specifies the
<code>IncomingMessage</code> class to used for HTTP/1 fallback. Useful for extending
the original <code>http.IncomingMessage</code>. <strong>Default:</strong> <code>http.IncomingMessage</code>.</li>
<li><code>Http1ServerResponse</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a> Specifies the <code>ServerResponse</code>
class to used for HTTP/1 fallback. Useful for extending the original
<code>http.ServerResponse</code>. <strong>Default:</strong> <code>http.ServerResponse</code>.</li>
<li><code>Http2ServerRequest</code> <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a> Specifies the
<code>Http2ServerRequest</code> class to use.
Useful for extending the original <code>Http2ServerRequest</code>.
<strong>Default:</strong> <code>Http2ServerRequest</code>.</li>
<li><code>Http2ServerResponse</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a> Specifies the
<code>Http2ServerResponse</code> class to use.
Useful for extending the original <code>Http2ServerResponse</code>.
<strong>Default:</strong> <code>Http2ServerResponse</code>.</li>
<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
a server should wait when an <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> is emitted. If the
socket has not been destroyed by that time the server will destroy it.
<strong>Default:</strong> <code>10000</code>.</li>
<li>...: Any <a href="net.html#netcreateserveroptions-connectionlistener"><code>net.createServer()</code></a> option can be provided.</li>
</ul>
</li>
<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>
<li>Returns: <a href="http2.html#class-http2server" class="type"><Http2Server></a></li>
</ul>
<p>Returns a <code>net.Server</code> instance that creates and manages <code>Http2Session</code>
instances.</p>
<p>Since there are no browsers known that support
<a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the use of
<a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a> is necessary when communicating
with browser clients.</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-comment">// Create an unencrypted HTTP/2 server.</span>
<span class="hljs-comment">// Since there are no browsers known that support</span>
<span class="hljs-comment">// unencrypted HTTP/2, the use of `createSecureServer()`</span>
<span class="hljs-comment">// is necessary when communicating with browser clients.</span>
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<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> {
stream.<span class="hljs-title function_">respond</span>({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>);
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-comment">// Create an unencrypted HTTP/2 server.</span>
<span class="hljs-comment">// Since there are no browsers known that support</span>
<span class="hljs-comment">// unencrypted HTTP/2, the use of `http2.createSecureServer()`</span>
<span class="hljs-comment">// is necessary when communicating with browser clients.</span>
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<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> {
stream.<span class="hljs-title function_">respond</span>({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>);
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.0.0</td>
<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>
<tr><td>v13.3.0, v12.16.0</td>
<td><p>Added <code>maxSessionRejectedStreams</code> option with a default of 100.</p></td></tr>
<tr><td>v13.3.0, v12.16.0</td>
<td><p>Added <code>maxSessionInvalidFrames</code> option with a default of 1000.</p></td></tr>
<tr><td>v15.10.0, v14.16.0, v12.21.0, v10.24.0</td>
<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr>
<tr><td>v14.4.0, v12.18.0, v10.21.0</td>
<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr>
<tr><td>v10.12.0</td>
<td><p>Added the <code>origins</code> option to automatically send an <code>ORIGIN</code> frame on <code>Http2Session</code> startup.</p></td></tr>
<tr><td>v8.9.3</td>
<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr>
<tr><td>v8.9.3</td>
<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<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
HTTP/2 will be downgraded to HTTP/1.x when set to <code>true</code>.
See the <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> event. See <a href="#alpn-negotiation">ALPN negotiation</a>.
<strong>Default:</strong> <code>false</code>.</li>
<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
for deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li>
<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
<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li>
<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>
is permitted to use. The value is expressed in terms of number of megabytes,
e.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>. This is a
credit based limit, existing <code>Http2Stream</code>s may cause this
limit to be exceeded, but new <code>Http2Stream</code> instances will be rejected
while this limit is exceeded. The current number of <code>Http2Stream</code> sessions,
the current memory use of the header compression tables, current data
queued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all
counted towards the current limit. <strong>Default:</strong> <code>10</code>.</li>
<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.
This is similar to <a href="http.html#servermaxheaderscount"><code>server.maxHeadersCount</code></a> or
<a href="http.html#requestmaxheaderscount"><code>request.maxHeadersCount</code></a> in the <code>node:http</code> module. The minimum value
is <code>4</code>. <strong>Default:</strong> <code>128</code>.</li>
<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,
unacknowledged pings. <strong>Default:</strong> <code>10</code>.</li>
<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
serialized, compressed block of headers. Attempts to send headers that
exceed this limit will result in a <code>'frameError'</code> event being emitted
and the stream being closed and destroyed.</li>
<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
padding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong>
<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of:
<ul>
<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li>
<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding,
determined by the internal implementation, is applied.</li>
<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough
padding to ensure that the total frame length, including the
9-byte header, is a multiple of 8. For each frame, there is a maximum
allowed number of padding bytes that is determined by current flow control
state and settings. If this maximum is less than the calculated amount
needed to ensure alignment, the maximum is used and the total frame length
is not necessarily aligned at 8 bytes.</li>
</ul>
</li>
<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
streams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will
be overridden if the remote peer sets its own value for
<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li>
<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
frames that will be tolerated before the session is closed.
<strong>Default:</strong> <code>1000</code>.</li>
<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
upon creation streams that will be tolerated before the session is closed.
Each rejection is associated with an <code>NGHTTP2_ENHANCE_YOUR_CALM</code>
error that should tell the peer to not open any more streams, continuing
to open streams is therefore regarded as a sign of a misbehaving peer.
<strong>Default:</strong> <code>100</code>.</li>
<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a> The initial settings to send to the
remote peer upon connection.</li>
<li><code>remoteCustomSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> The array of integer values determines the
settings types, which are included in the <code>customSettings</code>-property of the
received remoteSettings. Please see the <code>customSettings</code>-property of the
<code>Http2Settings</code> object for more information, on the allowed setting types.</li>
<li>...: Any <a href="tls.html#tlscreateserveroptions-secureconnectionlistener"><code>tls.createServer()</code></a> options can be provided. For
servers, the identity options (<code>pfx</code> or <code>key</code>/<code>cert</code>) are usually required.</li>
<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>
frame immediately following creation of a new server <code>Http2Session</code>.</li>
<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
a server should wait when an <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> event is emitted. If
the socket has not been destroyed by that time the server will destroy it.
<strong>Default:</strong> <code>10000</code>.</li>
</ul>
</li>
<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>
<li>Returns: <a href="http2.html#class-http2secureserver" class="type"><Http2SecureServer></a></li>
</ul>
<p>Returns a <code>tls.Server</code> instance that creates and manages <code>Http2Session</code>
instances.</p>
<pre class="with-48-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createSecureServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">key</span>: <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-key.pem'</span>),
<span class="hljs-attr">cert</span>: <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-cert.pem'</span>),
};
<span class="hljs-comment">// Create a secure HTTP/2 server</span>
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureServer</span>(options);
server.<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> {
stream.<span class="hljs-title function_">respond</span>({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>);
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-key.pem'</span>),
<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-cert.pem'</span>),
};
<span class="hljs-comment">// Create a secure HTTP/2 server</span>
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
server.<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> {
stream.<span class="hljs-title function_">respond</span>({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
});
stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'<h1>Hello World</h1>'</span>);
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.0.0</td>
<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>
<tr><td>v15.10.0, v14.16.0, v12.21.0, v10.24.0</td>
<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr>
<tr><td>v14.4.0, v12.18.0, v10.21.0</td>
<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr>
<tr><td>v8.9.3</td>
<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr>
<tr><td>v8.9.3</td>
<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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
be in the form of a minimal, valid URL with the <code>http://</code> or <code>https://</code>
prefix, host name, and IP port (if a non-default port is used). Userinfo
(user ID and password), path, querystring, and fragment details in the
URL will be ignored.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<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
for deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li>
<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
<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li>
<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>
is permitted to use. The value is expressed in terms of number of megabytes,
e.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>.
This is a credit based limit, existing <code>Http2Stream</code>s may cause this
limit to be exceeded, but new <code>Http2Stream</code> instances will be rejected
while this limit is exceeded. The current number of <code>Http2Stream</code> sessions,
the current memory use of the header compression tables, current data
queued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all
counted towards the current limit. <strong>Default:</strong> <code>10</code>.</li>
<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.
This is similar to <a href="http.html#servermaxheaderscount"><code>server.maxHeadersCount</code></a> or
<a href="http.html#requestmaxheaderscount"><code>request.maxHeadersCount</code></a> in the <code>node:http</code> module. The minimum value
is <code>1</code>. <strong>Default:</strong> <code>128</code>.</li>
<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,
unacknowledged pings. <strong>Default:</strong> <code>10</code>.</li>
<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
streams the client will accept at any given time. Once the current number of
currently reserved push streams exceeds reaches this limit, new push streams
sent by the server will be automatically rejected. The minimum allowed value
is 0. The maximum allowed value is 2<sup>32</sup>-1. A negative value sets
this option to the maximum allowed value. <strong>Default:</strong> <code>200</code>.</li>
<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
serialized, compressed block of headers. Attempts to send headers that
exceed this limit will result in a <code>'frameError'</code> event being emitted
and the stream being closed and destroyed.</li>
<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
padding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong>
<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of:
<ul>
<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li>
<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding,
determined by the internal implementation, is applied.</li>
<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough
padding to ensure that the total frame length, including the
9-byte header, is a multiple of 8. For each frame, there is a maximum
allowed number of padding bytes that is determined by current flow control
state and settings. If this maximum is less than the calculated amount
needed to ensure alignment, the maximum is used and the total frame length
is not necessarily aligned at 8 bytes.</li>
</ul>
</li>
<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
streams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will
be overridden if the remote peer sets its own value for
<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li>
<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
<code>authority</code>. Value may be either <code>'http:'</code> or <code>'https:'</code>. <strong>Default:</strong>
<code>'https:'</code></li>
<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a> The initial settings to send to the
remote peer upon connection.</li>
<li><code>remoteCustomSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> The array of integer values determines the
settings types, which are included in the <code>CustomSettings</code>-property of the
received remoteSettings. Please see the <code>CustomSettings</code>-property of the
<code>Http2Settings</code> object for more information, on the allowed setting types.</li>
<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>
instance passed to <code>connect</code> and the <code>options</code> object, and returns any
<a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream that is to be used as the connection for this session.</li>
<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>
<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
a server should wait when an <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> event is emitted. If
the socket has not been destroyed by that time the server will destroy it.
<strong>Default:</strong> <code>10000</code>.</li>
</ul>
</li>
<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
<a href="#event-connect"><code>'connect'</code></a> event.</li>
<li>Returns: <a href="http2.html#class-clienthttp2session" class="type"><ClientHttp2Session></a></li>
</ul>
<p>Returns a <code>ClientHttp2Session</code> instance.</p>
<pre class="with-55-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> client = <span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:1234'</span>);
<span class="hljs-comment">/* Use the client */</span>
client.<span class="hljs-title function_">close</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:1234'</span>);
<span class="hljs-comment">/* Use the client */</span>
client.<span class="hljs-title function_">close</span>();</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<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>
<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>
<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for
a given number of milliseconds set using <code>http2server.setTimeout()</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Returns: <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li>
</ul>
<p>Returns an object containing the default settings for an <code>Http2Session</code>
instance. This method returns a new object instance every time it is called
so instances returned may be safely modified for use.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>settings</code> <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li>
<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
<p>Returns a <code>Buffer</code> instance containing serialized representation of the given
HTTP/2 settings as specified in the <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> specification. This is intended
for use with the <code>HTTP2-Settings</code> header field.</p>
<pre class="with-47-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { getPackedSettings } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> packed = <span class="hljs-title function_">getPackedSettings</span>({ <span class="hljs-attr">enablePush</span>: <span class="hljs-literal">false</span> });
<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>));
<span class="hljs-comment">// Prints: AAIAAAAA</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<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> });
<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>));
<span class="hljs-comment">// Prints: AAIAAAAA</span></code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<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>
<li>Returns: <a href="http2.html#settings-object" class="type"><HTTP/2 Settings Object></a></li>
</ul>
<p>Returns a <a href="#settings-object">HTTP/2 Settings Object</a> containing the deserialized settings from
the given <code>Buffer</code> as generated by <code>http2.getPackedSettings()</code>.</p>
<h4><code>http2.performServerHandshake(socket[, options])</code><span><a class="mark" href="#http2performserverhandshakesocket-options" id="http2performserverhandshakesocket-options">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_performserverhandshake_socket_options"></a></h4>
<div class="api_metadata">
<span>Added in: v21.7.0, v20.12.0</span>
</div>
<ul>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li>...: Any <a href="#http2createserveroptions-onrequesthandler"><code>http2.createServer()</code></a> option can be provided.</li>
</ul>
</li>
<li>Returns: <a href="http2.html#class-serverhttp2session" class="type"><ServerHttp2Session></a></li>
</ul>
<p>Create an HTTP/2 server session from an existing socket.</p>
<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>
<div class="api_metadata">
<span>Added in: v15.0.0, v14.18.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a></li>
</ul>
<p>This symbol can be set as a property on the HTTP/2 headers object with an array
value in order to provide a list of headers considered sensitive.
See <a href="#sensitive-headers">Sensitive headers</a> for more details.</p>
<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>
<p>Headers are represented as own-properties on JavaScript objects. The property
keys will be serialized to lower-case. Property values should be strings (if
they are not they will be coerced to strings) or an <code>Array</code> of strings (in order
to send more than one value per header field).</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
<span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
<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>],
};
stream.<span class="hljs-title function_">respond</span>(headers);</code> <button class="copy-button">copy</button></pre>
<p>Header objects passed to callback functions will have a <code>null</code> prototype. This
means that normal JavaScript object methods such as
<code>Object.prototype.toString()</code> and <code>Object.prototype.hasOwnProperty()</code> will
not work.</p>
<p>For incoming headers:</p>
<ul>
<li>The <code>:status</code> header is converted to <code>number</code>.</li>
<li>Duplicates of <code>:status</code>, <code>:method</code>, <code>:authority</code>, <code>:scheme</code>, <code>:path</code>,
<code>:protocol</code>, <code>age</code>, <code>authorization</code>, <code>access-control-allow-credentials</code>,
<code>access-control-max-age</code>, <code>access-control-request-method</code>, <code>content-encoding</code>,
<code>content-language</code>, <code>content-length</code>, <code>content-location</code>, <code>content-md5</code>,
<code>content-range</code>, <code>content-type</code>, <code>date</code>, <code>dnt</code>, <code>etag</code>, <code>expires</code>, <code>from</code>,
<code>host</code>, <code>if-match</code>, <code>if-modified-since</code>, <code>if-none-match</code>, <code>if-range</code>,
<code>if-unmodified-since</code>, <code>last-modified</code>, <code>location</code>, <code>max-forwards</code>,
<code>proxy-authorization</code>, <code>range</code>, <code>referer</code>,<code>retry-after</code>, <code>tk</code>,
<code>upgrade-insecure-requests</code>, <code>user-agent</code> or <code>x-content-type-options</code> are
discarded.</li>
<li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li>
<li>For duplicate <code>cookie</code> headers, the values are joined together with '; '.</li>
<li>For all other headers, the values are joined together with ', '.</li>
</ul>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>();
server.<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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-string">':path'</span>]);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers.<span class="hljs-property">ABC</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
server.<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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-string">':path'</span>]);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers.<span class="hljs-property">ABC</span>);
});</code><button class="copy-button">copy</button></pre>
<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>
<p>HTTP2 headers can be marked as sensitive, which means that the HTTP/2
header compression algorithm will never index them. This can make sense for
header values with low entropy and that may be considered valuable to an
attacker, for example <code>Cookie</code> or <code>Authorization</code>. To achieve this, add
the header name to the <code>[http2.sensitiveHeaders]</code> property as an array:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
<span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
<span class="hljs-string">'cookie'</span>: <span class="hljs-string">'some-cookie'</span>,
<span class="hljs-string">'other-sensitive-header'</span>: <span class="hljs-string">'very secret data'</span>,
[http2.<span class="hljs-property">sensitiveHeaders</span>]: [<span class="hljs-string">'cookie'</span>, <span class="hljs-string">'other-sensitive-header'</span>],
};
stream.<span class="hljs-title function_">respond</span>(headers);</code> <button class="copy-button">copy</button></pre>
<p>For some headers, such as <code>Authorization</code> and short <code>Cookie</code> headers,
this flag is set automatically.</p>
<p>This property is also set for received headers. It will contain the names of
all headers marked as sensitive, including ones marked that way automatically.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v12.12.0</td>
<td><p>The <code>maxConcurrentStreams</code> setting is stricter.</p></td></tr>
<tr><td>v8.9.3</td>
<td><p>The <code>maxHeaderListSize</code> setting is now strictly enforced.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>The <code>http2.getDefaultSettings()</code>, <code>http2.getPackedSettings()</code>,
<code>http2.createServer()</code>, <code>http2.createSecureServer()</code>,
<code>http2session.settings()</code>, <code>http2session.localSettings</code>, and
<code>http2session.remoteSettings</code> APIs either return or receive as input an
object that defines configuration settings for an <code>Http2Session</code> object.
These objects are ordinary JavaScript objects containing the following
properties.</p>
<ul>
<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
header compression. The minimum allowed value is 0. The maximum allowed value
is 2<sup>32</sup>-1. <strong>Default:</strong> <code>4096</code>.</li>
<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
permitted on the <code>Http2Session</code> instances. <strong>Default:</strong> <code>true</code>.</li>
<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
bytes for stream-level flow control. The minimum allowed value is 0. The
maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
<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
payload. The minimum allowed value is 16,384. The maximum allowed value is
2<sup>24</sup>-1. <strong>Default:</strong> <code>16384</code>.</li>
<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
streams permitted on an <code>Http2Session</code>. There is no default value which
implies, at least theoretically, 2<sup>32</sup>-1 streams may be open
concurrently at any given time in an <code>Http2Session</code>. The minimum value
is 0. The maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong>
<code>4294967295</code>.</li>
<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)
of header list that will be accepted. The minimum allowed value is 0. The
maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
<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>
<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
Protocol" defined by <a href="https://tools.ietf.org/html/rfc8441">RFC 8441</a> is to be enabled. This setting is only
meaningful if sent by the server. Once the <code>enableConnectProtocol</code> setting
has been enabled for a given <code>Http2Session</code>, it cannot be disabled.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>customSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Specifies additional settings, yet not implemented
in node and the underlying libraries. The key of the object defines the
numeric value of the settings type (as defined in the "HTTP/2 SETTINGS"
registry established by [RFC 7540]) and the values the actual numeric value
of the settings.
The settings type has to be an integer in the range from 1 to 2^16-1.
It should not be a settings type already handled by node, i.e. currently
it should be greater than 6, although it is not an error.
The values need to be unsigned integers in the range from 0 to 2^32-1.
Currently, a maximum of up 10 custom settings is supported.
It is only supported for sending SETTINGS, or for receiving settings values
specified in the <code>remoteCustomSettings</code> options of the server or client
object. Do not mix the <code>customSettings</code>-mechanism for a settings id with
interfaces for the natively handled settings, in case a setting becomes
natively supported in a future node version.</li>
</ul>
<p>All additional properties on the settings object are ignored.</p>
<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>
<p>There are several types of error conditions that may arise when using the
<code>node:http2</code> module:</p>
<p>Validation errors occur when an incorrect argument, option, or setting value is
passed in. These will always be reported by a synchronous <code>throw</code>.</p>
<p>State errors occur when an action is attempted at an incorrect time (for
instance, attempting to send data on a stream after it has closed). These will
be reported using either a synchronous <code>throw</code> or via an <code>'error'</code> event on
the <code>Http2Stream</code>, <code>Http2Session</code> or HTTP/2 Server objects, depending on where
and when the error occurs.</p>
<p>Internal errors occur when an HTTP/2 session fails unexpectedly. These will be
reported via an <code>'error'</code> event on the <code>Http2Session</code> or HTTP/2 Server objects.</p>
<p>Protocol errors occur when various HTTP/2 protocol constraints are violated.
These will be reported using either a synchronous <code>throw</code> or via an <code>'error'</code>
event on the <code>Http2Stream</code>, <code>Http2Session</code> or HTTP/2 Server objects, depending
on where and when the error occurs.</p>
<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>
<p>The HTTP/2 implementation applies stricter handling of invalid characters in
HTTP header names and values than the HTTP/1 implementation.</p>
<p>Header field names are <em>case-insensitive</em> and are transmitted over the wire
strictly as lower-case strings. The API provided by Node.js allows header
names to be set as mixed-case strings (e.g. <code>Content-Type</code>) but will convert
those to lower-case (e.g. <code>content-type</code>) upon transmission.</p>
<p>Header field-names <em>must only</em> contain one or more of the following ASCII
characters: <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>,
<code>-</code>, <code>.</code>, <code>^</code>, <code>_</code>, <code>`</code> (backtick), <code>|</code>, and <code>~</code>.</p>
<p>Using invalid characters within an HTTP header field name will cause the
stream to be closed with a protocol error being reported.</p>
<p>Header field values are handled with more leniency but <em>should</em> not contain
new-line or carriage return characters and <em>should</em> be limited to US-ASCII
characters, per the requirements of the HTTP specification.</p>
<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>
<p>To receive pushed streams on the client, set a listener for the <code>'stream'</code>
event on the <code>ClientHttp2Session</code>:</p>
<pre class="with-37-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> client = <span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost'</span>);
client.<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> {
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> {
<span class="hljs-comment">// Process response headers</span>
});
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> });
});
<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><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost'</span>);
client.<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> {
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> {
<span class="hljs-comment">// Process response headers</span>
});
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> });
});
<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>
<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>
<p>The <code>CONNECT</code> method is used to allow an HTTP/2 server to be used as a proxy
for TCP/IP connections.</p>
<p>A simple TCP Server:</p>
<pre class="with-40-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:net'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
<span class="hljs-keyword">let</span> name = <span class="hljs-string">''</span>;
socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
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);
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>));
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>);
<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> {
<span class="hljs-keyword">let</span> name = <span class="hljs-string">''</span>;
socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
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);
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>));
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><button class="copy-button">copy</button></pre>
<p>An HTTP/2 CONNECT proxy:</p>
<pre class="with-68-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> { <span class="hljs-title class_">NGHTTP2</span>_REFUSED_STREAM, <span class="hljs-title class_">NGHTTP2</span>_CONNECT_ERROR } = constants;
<span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:net'</span>;
<span class="hljs-keyword">const</span> proxy = <span class="hljs-title function_">createServer</span>();
proxy.<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> {
<span class="hljs-keyword">if</span> (headers[<span class="hljs-string">':method'</span>] !== <span class="hljs-string">'CONNECT'</span>) {
<span class="hljs-comment">// Only accept CONNECT requests</span>
stream.<span class="hljs-title function_">close</span>(<span class="hljs-title class_">NGHTTP2</span>_REFUSED_STREAM);
<span class="hljs-keyword">return</span>;
}
<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>);
<span class="hljs-comment">// It's a very good idea to verify that hostname and port are</span>
<span class="hljs-comment">// things this proxy should be connecting to.</span>
<span class="hljs-keyword">const</span> socket = <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> {
stream.<span class="hljs-title function_">respond</span>();
socket.<span class="hljs-title function_">pipe</span>(stream);
stream.<span class="hljs-title function_">pipe</span>(socket);
});
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> {
stream.<span class="hljs-title function_">close</span>(<span class="hljs-title class_">NGHTTP2</span>_CONNECT_ERROR);
});
});
proxy.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8001</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> { <span class="hljs-title class_">NGHTTP2</span>_REFUSED_STREAM } = http2.<span class="hljs-property">constants</span>;
<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>);
<span class="hljs-keyword">const</span> proxy = http2.<span class="hljs-title function_">createServer</span>();
proxy.<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> {
<span class="hljs-keyword">if</span> (headers[<span class="hljs-string">':method'</span>] !== <span class="hljs-string">'CONNECT'</span>) {
<span class="hljs-comment">// Only accept CONNECT requests</span>
stream.<span class="hljs-title function_">close</span>(<span class="hljs-title class_">NGHTTP2</span>_REFUSED_STREAM);
<span class="hljs-keyword">return</span>;
}
<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>);
<span class="hljs-comment">// It's a very good idea to verify that hostname and port are</span>
<span class="hljs-comment">// things this proxy should be connecting to.</span>
<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> {
stream.<span class="hljs-title function_">respond</span>();
socket.<span class="hljs-title function_">pipe</span>(stream);
stream.<span class="hljs-title function_">pipe</span>(socket);
});
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> {
stream.<span class="hljs-title function_">close</span>(http2.<span class="hljs-property">constants</span>.<span class="hljs-property">NGHTTP2_CONNECT_ERROR</span>);
});
});
proxy.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8001</span>);</code><button class="copy-button">copy</button></pre>
<p>An HTTP/2 CONNECT client:</p>
<pre class="with-48-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { connect, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> client = <span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost:8001'</span>);
<span class="hljs-comment">// Must not specify the ':path' and ':scheme' headers</span>
<span class="hljs-comment">// for CONNECT requests or an error will be thrown.</span>
<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">':authority'</span>: <span class="hljs-string">'localhost:8000'</span>,
});
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[constants.<span class="hljs-property">HTTP2_HEADER_STATUS</span>]);
});
<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
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> data += chunk);
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
<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>);
client.<span class="hljs-title function_">close</span>();
});
req.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Jane'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost:8001'</span>);
<span class="hljs-comment">// Must not specify the ':path' and ':scheme' headers</span>
<span class="hljs-comment">// for CONNECT requests or an error will be thrown.</span>
<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">':authority'</span>: <span class="hljs-string">'localhost:8000'</span>,
});
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers</span>) =></span> {
<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>]);
});
<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
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> data += chunk);
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
<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>);
client.<span class="hljs-title function_">close</span>();
});
req.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Jane'</span>);</code><button class="copy-button">copy</button></pre>
<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>
<p><a href="https://tools.ietf.org/html/rfc8441">RFC 8441</a> defines an "Extended CONNECT Protocol" extension to HTTP/2 that
may be used to bootstrap the use of an <code>Http2Stream</code> using the <code>CONNECT</code>
method as a tunnel for other communication protocols (such as WebSockets).</p>
<p>The use of the Extended CONNECT Protocol is enabled by HTTP/2 servers by using
the <code>enableConnectProtocol</code> setting:</p>
<pre class="with-49-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> settings = { <span class="hljs-attr">enableConnectProtocol</span>: <span class="hljs-literal">true</span> };
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>({ settings });</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> settings = { <span class="hljs-attr">enableConnectProtocol</span>: <span class="hljs-literal">true</span> };
<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>({ settings });</code><button class="copy-button">copy</button></pre>
<p>Once the client receives the <code>SETTINGS</code> frame from the server indicating that
the extended CONNECT may be used, it may send <code>CONNECT</code> requests that use the
<code>':protocol'</code> HTTP/2 pseudo-header:</p>
<pre class="with-54-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> client = <span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost:8080'</span>);
client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> {
<span class="hljs-keyword">if</span> (settings.<span class="hljs-property">enableConnectProtocol</span>) {
<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> });
<span class="hljs-comment">// ...</span>
}
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost:8080'</span>);
client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> {
<span class="hljs-keyword">if</span> (settings.<span class="hljs-property">enableConnectProtocol</span>) {
<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> });
<span class="hljs-comment">// ...</span>
}
});</code><button class="copy-button">copy</button></pre>
</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>
<p>The Compatibility API has the goal of providing a similar developer experience
of HTTP/1 when using HTTP/2, making it possible to develop applications
that support both <a href="http.html">HTTP/1</a> and HTTP/2. This API targets only the
<strong>public API</strong> of the <a href="http.html">HTTP/1</a>. However many modules use internal
methods or state, and those <em>are not supported</em> as it is a completely
different implementation.</p>
<p>The following example creates an HTTP/2 server using the compatibility
API:</p>
<pre class="with-49-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>);
res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
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> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<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> {
res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>);
res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
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> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});</code><button class="copy-button">copy</button></pre>
<p>In order to create a mixed <a href="https.html">HTTPS</a> and HTTP/2 server, refer to the
<a href="#alpn-negotiation">ALPN negotiation</a> section.
Upgrading from non-tls HTTP/1 servers is not supported.</p>
<p>The HTTP/2 compatibility API is composed of <a href="#class-http2http2serverrequest"><code>Http2ServerRequest</code></a> and
<a href="#class-http2http2serverresponse"><code>Http2ServerResponse</code></a>. They aim at API compatibility with HTTP/1, but
they do not hide the differences between the protocols. As an example,
the status message for HTTP codes is ignored.</p>
<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>
<p>ALPN negotiation allows supporting both <a href="https.html">HTTPS</a> and HTTP/2 over
the same socket. The <code>req</code> and <code>res</code> objects can be either HTTP/1 or
HTTP/2, and an application <strong>must</strong> restrict itself to the public API of
<a href="http.html">HTTP/1</a>, and detect if it is possible to use the more advanced
features of HTTP/2.</p>
<p>The following example creates a server that supports both protocols:</p>
<pre class="with-53-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createSecureServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> cert = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'./cert.pem'</span>);
<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'./key.pem'</span>);
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureServer</span>(
{ cert, key, <span class="hljs-attr">allowHTTP1</span>: <span class="hljs-literal">true</span> },
onRequest,
).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
<span class="hljs-keyword">function</span> <span class="hljs-title function_">onRequest</span>(<span class="hljs-params">req, res</span>) {
<span class="hljs-comment">// Detects if it is a HTTPS request or HTTP/2</span>
<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> ?
req.<span class="hljs-property">stream</span>.<span class="hljs-property">session</span> : req;
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> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({
alpnProtocol,
<span class="hljs-attr">httpVersion</span>: req.<span class="hljs-property">httpVersion</span>,
}));
}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { createSecureServer } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<span class="hljs-keyword">const</span> { readFileSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-keyword">const</span> cert = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'./cert.pem'</span>);
<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'./key.pem'</span>);
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureServer</span>(
{ cert, key, <span class="hljs-attr">allowHTTP1</span>: <span class="hljs-literal">true</span> },
onRequest,
).<span class="hljs-title function_">listen</span>(<span class="hljs-number">4443</span>);
<span class="hljs-keyword">function</span> <span class="hljs-title function_">onRequest</span>(<span class="hljs-params">req, res</span>) {
<span class="hljs-comment">// Detects if it is a HTTPS request or HTTP/2</span>
<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> ?
req.<span class="hljs-property">stream</span>.<span class="hljs-property">session</span> : req;
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> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({
alpnProtocol,
<span class="hljs-attr">httpVersion</span>: req.<span class="hljs-property">httpVersion</span>,
}));
}</code><button class="copy-button">copy</button></pre>
<p>The <code>'request'</code> event works identically on both <a href="https.html">HTTPS</a> and
HTTP/2.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li>
</ul>
<p>A <code>Http2ServerRequest</code> object is created by <a href="#class-http2server"><code>http2.Server</code></a> or
<a href="#class-http2secureserver"><code>http2.SecureServer</code></a> and passed as the first argument to the
<a href="#event-request"><code>'request'</code></a> event. It may be used to access a request status, headers, and
data.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>The <code>'aborted'</code> event is emitted whenever a <code>Http2ServerRequest</code> instance is
abnormally aborted in mid-communication.</p>
<p>The <code>'aborted'</code> event will only be emitted if the <code>Http2ServerRequest</code> writable
side has not been ended.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>Indicates that the underlying <a href="#class-http2stream"><code>Http2Stream</code></a> was closed.
Just like <code>'end'</code>, this event occurs only once per response.</p>
<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>
<div class="api_metadata">
<span>Added in: v10.1.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>The <code>request.aborted</code> property will be <code>true</code> if the request has
been aborted.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>The request authority pseudo header field. Because HTTP/2 allows requests
to set either <code>:authority</code> or <code>host</code>, this value is derived from
<code>req.headers[':authority']</code> if present. Otherwise, it is derived from
<code>req.headers['host']</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v12.10.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>The <code>request.complete</code> property will be <code>true</code> if the request has
been completed, aborted, or destroyed.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span><span>Deprecated since: v13.0.0</span>
</div>
<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>
<ul>
<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type"><tls.TLSSocket></a></li>
</ul>
<p>See <a href="#requestsocket"><code>request.socket</code></a>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p>Calls <code>destroy()</code> on the <a href="#class-http2stream"><code>Http2Stream</code></a> that received
the <a href="#class-http2http2serverrequest"><code>Http2ServerRequest</code></a>. If <code>error</code> is provided, an <code>'error'</code> event
is emitted and <code>error</code> is passed as an argument to any listeners on the event.</p>
<p>It does nothing if the stream was already destroyed.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
</ul>
<p>The request/response headers object.</p>
<p>Key-value pairs of header names and values. Header names are lower-cased.</p>
<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span>
<span class="hljs-comment">// host: '127.0.0.1:8000',</span>
<span class="hljs-comment">// accept: '*/*' }</span>
<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>
<p>See <a href="#headers-object">HTTP/2 Headers Object</a>.</p>
<p>In HTTP/2, the request path, host name, protocol, and method are represented as
special headers prefixed with the <code>:</code> character (e.g. <code>':path'</code>). These special
headers will be included in the <code>request.headers</code> object. Care must be taken not
to inadvertently modify these special headers or errors may occur. For instance,
removing all headers from the request will cause errors to occur:</p>
<pre><code class="language-js"><span class="hljs-title function_">removeAllHeaders</span>(request.<span class="hljs-property">headers</span>);
<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>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>In case of server request, the HTTP version sent by the client. In the case of
client response, the HTTP version of the connected-to server. Returns
<code>'2.0'</code>.</p>
<p>Also <code>message.httpVersionMajor</code> is the first integer and
<code>message.httpVersionMinor</code> is the second.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>The request method as a string. Read-only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li>
</ul>
<p>The raw request/response headers list exactly as they were received.</p>
<p>The keys and values are in the same list. It is <em>not</em> a
list of tuples. So, the even-numbered offsets are key values, and the
odd-numbered offsets are the associated values.</p>
<p>Header names are not lowercased, and duplicates are not merged.</p>
<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// [ 'user-agent',</span>
<span class="hljs-comment">// 'this is invalid because there can be only one',</span>
<span class="hljs-comment">// 'User-Agent',</span>
<span class="hljs-comment">// 'curl/7.22.0',</span>
<span class="hljs-comment">// 'Host',</span>
<span class="hljs-comment">// '127.0.0.1:8000',</span>
<span class="hljs-comment">// 'ACCEPT',</span>
<span class="hljs-comment">// '*/*' ]</span>
<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>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li>
</ul>
<p>The raw request/response trailer keys and values exactly as they were
received. Only populated at the <code>'end'</code> event.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>The request scheme pseudo header field indicating the scheme
portion of the target URL.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li>
</ul>
<p>Sets the <a href="#class-http2stream"><code>Http2Stream</code></a>'s timeout value to <code>msecs</code>. If a callback is
provided, then it is added as a listener on the <code>'timeout'</code> event on
the response object.</p>
<p>If no <code>'timeout'</code> listener is added to the request, the response, or
the server, then <a href="#class-http2stream"><code>Http2Stream</code></a>s are destroyed when they time out. If a
handler is assigned to the request, the response, or the server's <code>'timeout'</code>
events, timed out sockets must be handled explicitly.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type"><tls.TLSSocket></a></li>
</ul>
<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but
applies getters, setters, and methods based on HTTP/2 logic.</p>
<p><code>destroyed</code>, <code>readable</code>, and <code>writable</code> properties will be retrieved from and
set on <code>request.stream</code>.</p>
<p><code>destroy</code>, <code>emit</code>, <code>end</code>, <code>on</code> and <code>once</code> methods will be called on
<code>request.stream</code>.</p>
<p><code>setTimeout</code> method will be called on <code>request.stream.session</code>.</p>
<p><code>pause</code>, <code>read</code>, <code>resume</code>, and <code>write</code> will throw an error with code
<code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See <a href="#http2session-and-sockets"><code>Http2Session</code> and Sockets</a> for
more information.</p>
<p>All other interactions will be routed directly to the socket. With TLS support,
use <a href="tls.html#tlssocketgetpeercertificatedetailed"><code>request.socket.getPeerCertificate()</code></a> to obtain the client's
authentication details.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="http2.html#class-http2stream" class="type"><Http2Stream></a></li>
</ul>
<p>The <a href="#class-http2stream"><code>Http2Stream</code></a> object backing the request.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
</ul>
<p>The request/response trailers object. Only populated at the <code>'end'</code> event.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Request URL string. This contains only the URL that is present in the actual
HTTP request. If the request is:</p>
<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>
<span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code> <button class="copy-button">copy</button></pre>
<p>Then <code>request.url</code> will be:</p>
<!-- eslint-disable @stylistic/js/semi -->
<pre><code class="language-js"><span class="hljs-string">'/status?name=ryan'</span></code> <button class="copy-button">copy</button></pre>
<p>To parse the url into its parts, <code>new URL()</code> can be used:</p>
<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node</span>
<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>
URL {
href: 'http://example.com/status?name=ryan',
origin: 'http://example.com',
protocol: 'http:',
username: '',
password: '',
host: 'example.com',
hostname: 'example.com',
port: '',
pathname: '/status',
search: '?name=ryan',
searchParams: URLSearchParams { 'name' => 'ryan' },
hash: ''
}</code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends: <a href="stream.html#stream" class="type"><Stream></a></li>
</ul>
<p>This object is created internally by an HTTP server, not by the user. It is
passed as the second parameter to the <a href="#event-request"><code>'request'</code></a> event.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>Indicates that the underlying <a href="#class-http2stream"><code>Http2Stream</code></a> was terminated before
<a href="#responseenddata-encoding-callback"><code>response.end()</code></a> was called or able to flush.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>Emitted when the response has been sent. More specifically, this event is
emitted when the last segment of the response headers and body have been
handed off to the HTTP/2 multiplexing for transmission over the network. It
does not imply that the client has received anything yet.</p>
<p>After this event, no more events will be emitted on the response object.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
</ul>
<p>This method adds HTTP trailing headers (a header but at the end of the
message) to the response.</p>
<p>Attempting to set a header field name or value that contains invalid characters
will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p>
<h5><code>response.appendHeader(name, value)</code><span><a class="mark" href="#responseappendheadername-value" id="responseappendheadername-value">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_appendheader_name_value"></a></h5>
<div class="api_metadata">
<span>Added in: v21.7.0, v20.12.0</span>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<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>
</ul>
<p>Append a single header value to the header object.</p>
<p>If the value is an array, this is equivalent to calling this method multiple
times.</p>
<p>If there were no previous values for the header, this is equivalent to calling
<a href="#responsesetheadername-value"><code>response.setHeader()</code></a>.</p>
<p>Attempting to set a header field name or value that contains invalid characters
will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p>
<pre><code class="language-js"><span class="hljs-comment">// Returns headers including "set-cookie: a" and "set-cookie: b"</span>
<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> {
res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'set-cookie'</span>, <span class="hljs-string">'a'</span>);
res.<span class="hljs-title function_">appendHeader</span>(<span class="hljs-string">'set-cookie'</span>, <span class="hljs-string">'b'</span>);
res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});</code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span><span>Deprecated since: v13.0.0</span>
</div>
<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>
<ul>
<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type"><tls.TLSSocket></a></li>
</ul>
<p>See <a href="#responsesocket"><code>response.socket</code></a>.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<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>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>headers</code> <a href="http2.html#headers-object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li>
<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,
or either when the attempt to create the pushed <code>Http2Stream</code> has failed or
has been rejected, or the state of <code>Http2ServerRequest</code> is closed prior to
calling the <code>http2stream.pushStream()</code> method
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>res</code> <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a> The newly-created <code>Http2ServerResponse</code>
object</li>
</ul>
</li>
</ul>
<p>Call <a href="#http2streampushstreamheaders-options-callback"><code>http2stream.pushStream()</code></a> with the given headers, and wrap the
given <a href="#class-http2stream"><code>Http2Stream</code></a> on a newly created <code>Http2ServerResponse</code> as the callback
parameter if successful. When <code>Http2ServerRequest</code> is closed, the callback is
called with an error <code>ERR_HTTP2_INVALID_STREAM</code>.</p>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>This method now returns a reference to <code>ServerResponse</code>.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li>
</ul>
<p>This method signals to the server that all of the response headers and body
have been sent; that server should consider this message complete.
The method, <code>response.end()</code>, MUST be called on each response.</p>
<p>If <code>data</code> is specified, it is equivalent to calling
<a href="http.html#responsewritechunk-encoding-callback"><code>response.write(data, encoding)</code></a> followed by <code>response.end(callback)</code>.</p>
<p>If <code>callback</code> is specified, it will be called when the response stream
is finished.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span><span>Deprecated since: v13.4.0, v12.16.0</span>
</div>
<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>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Boolean value that indicates whether the response has completed. Starts
as <code>false</code>. After <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> executes, the value will be <code>true</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Reads out a header that has already been queued but not sent to the client.
The name is case-insensitive.</p>
<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>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li>
</ul>
<p>Returns an array containing the unique names of the current outgoing headers.
All header names are lowercase.</p>
<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>);
response.<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>]);
<span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">getHeaderNames</span>();
<span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
</ul>
<p>Returns a shallow copy of the current outgoing headers. Since a shallow copy
is used, array values may be mutated without additional calls to various
header-related http module methods. The keys of the returned object are the
header names and the values are the respective header values. All header names
are lowercase.</p>
<p>The object returned by the <code>response.getHeaders()</code> method <em>does not</em>
prototypically inherit from the JavaScript <code>Object</code>. This means that typical
<code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, and others
are not defined and <em>will not work</em>.</p>
<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>);
response.<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>]);
<span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHeaders</span>();
<span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</span></code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the
outgoing headers. The header name matching is case-insensitive.</p>
<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>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>True if headers were sent, false otherwise (read-only).</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Removes a header that has been queued for implicit sending.</p>
<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>
<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>
<div class="api_metadata">
<span>Added in: v15.7.0</span>
</div>
<ul>
<li><a href="http2.html#class-http2http2serverrequest" class="type"><http2.Http2ServerRequest></a></li>
</ul>
<p>A reference to the original HTTP2 <code>request</code> object.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>When true, the Date header will be automatically generated and sent in
the response if it is not already present in the headers. Defaults to true.</p>
<p>This should only be disabled for testing; HTTP requires the Date header
in responses.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<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>
</ul>
<p>Sets a single header value for implicit headers. If this header already exists
in the to-be-sent headers, its value will be replaced. Use an array of strings
here to send multiple headers with the same name.</p>
<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>
<p>or</p>
<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>
<p>Attempting to set a header field name or value that contains invalid characters
will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p>
<p>When headers have been set with <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>, they will be merged
with any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, with the headers passed
to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p>
<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span>
<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> {
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>);
res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
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> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});</code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li>
</ul>
<p>Sets the <a href="#class-http2stream"><code>Http2Stream</code></a>'s timeout value to <code>msecs</code>. If a callback is
provided, then it is added as a listener on the <code>'timeout'</code> event on
the response object.</p>
<p>If no <code>'timeout'</code> listener is added to the request, the response, or
the server, then <a href="#class-http2stream"><code>Http2Stream</code></a>s are destroyed when they time out. If a
handler is assigned to the request, the response, or the server's <code>'timeout'</code>
events, timed out sockets must be handled explicitly.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type"><tls.TLSSocket></a></li>
</ul>
<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but
applies getters, setters, and methods based on HTTP/2 logic.</p>
<p><code>destroyed</code>, <code>readable</code>, and <code>writable</code> properties will be retrieved from and
set on <code>response.stream</code>.</p>
<p><code>destroy</code>, <code>emit</code>, <code>end</code>, <code>on</code> and <code>once</code> methods will be called on
<code>response.stream</code>.</p>
<p><code>setTimeout</code> method will be called on <code>response.stream.session</code>.</p>
<p><code>pause</code>, <code>read</code>, <code>resume</code>, and <code>write</code> will throw an error with code
<code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See <a href="#http2session-and-sockets"><code>Http2Session</code> and Sockets</a> for
more information.</p>
<p>All other interactions will be routed directly to the socket.</p>
<pre class="with-49-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http2'</span>;
<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
<span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>;
<span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">remotePort</span>;
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>);
}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
<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> {
<span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>;
<span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">remotePort</span>;
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>);
}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>When using implicit headers (not calling <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> explicitly),
this property controls the status code that will be sent to the client when
the headers get flushed.</p>
<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>
<p>After response header was sent to the client, this property indicates the
status code which was sent out.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Status message is not supported by HTTP/2 (RFC 7540 8.1.2.4). It returns
an empty string.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="http2.html#class-http2stream" class="type"><Http2Stream></a></li>
</ul>
<p>The <a href="#class-http2stream"><code>Http2Stream</code></a> object backing the response.</p>
<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>
<div class="api_metadata">
<span>Added in: v12.9.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Is <code>true</code> after <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> has been called. This property
does not indicate whether the data has been flushed, for this use
<a href="stream.html#writablewritablefinished"><code>writable.writableFinished</code></a> instead.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<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>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>If this method is called and <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> has not been called,
it will switch to implicit header mode and flush the implicit headers.</p>
<p>This sends a chunk of the response body. This method may
be called multiple times to provide successive parts of the body.</p>
<p>In the <code>node:http</code> module, the response body is omitted when the
request is a HEAD request. Similarly, the <code>204</code> and <code>304</code> responses
<em>must not</em> include a message body.</p>
<p><code>chunk</code> can be a string or a buffer. If <code>chunk</code> is a string,
the second parameter specifies how to encode it into a byte stream.
By default the <code>encoding</code> is <code>'utf8'</code>. <code>callback</code> will be called when this chunk
of data is flushed.</p>
<p>This is the raw HTTP body and has nothing to do with higher-level multi-part
body encodings that may be used.</p>
<p>The first time <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> is called, it will send the buffered
header information and the first chunk of the body to the client. The second
time <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> is called, Node.js assumes data will be streamed,
and sends the new data separately. That is, the response is buffered up to the
first chunk of the body.</p>
<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel
buffer. Returns <code>false</code> if all or part of the data was queued in user memory.
<code>'drain'</code> will be emitted when the buffer is free again.</p>
<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>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>Sends a status <code>100 Continue</code> to the client, indicating that the request body
should be sent. See the <a href="#event-checkcontinue"><code>'checkContinue'</code></a> event on <code>Http2Server</code> and
<code>Http2SecureServer</code>.</p>
<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>
<div class="api_metadata">
<span>Added in: v18.11.0</span>
</div>
<ul>
<li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
</ul>
<p>Sends a status <code>103 Early Hints</code> to the client with a Link header,
indicating that the user agent can preload/preconnect the linked resources.
The <code>hints</code> is an object containing the values of headers to be sent with
early hints message.</p>
<p><strong>Example</strong></p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> earlyHintsLink = <span class="hljs-string">'</styles.css>; rel=preload; as=style'</span>;
response.<span class="hljs-title function_">writeEarlyHints</span>({
<span class="hljs-string">'link'</span>: earlyHintsLink,
});
<span class="hljs-keyword">const</span> earlyHintsLinks = [
<span class="hljs-string">'</styles.css>; rel=preload; as=style'</span>,
<span class="hljs-string">'</scripts.js>; rel=preload; as=script'</span>,
];
response.<span class="hljs-title function_">writeEarlyHints</span>({
<span class="hljs-string">'link'</span>: earlyHintsLinks,
});</code> <button class="copy-button">copy</button></pre>
<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>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v11.10.0, v10.17.0</td>
<td><p>Return <code>this</code> from <code>writeHead()</code> to allow chaining with <code>end()</code>.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
<li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<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>
<li>Returns: <a href="http2.html#class-http2http2serverresponse" class="type"><http2.Http2ServerResponse></a></li>
</ul>
<p>Sends a response header to the request. The status code is a 3-digit HTTP
status code, like <code>404</code>. The last argument, <code>headers</code>, are the response headers.</p>
<p>Returns a reference to the <code>Http2ServerResponse</code>, so that calls can be chained.</p>
<p>For compatibility with <a href="http.html">HTTP/1</a>, a human-readable <code>statusMessage</code> may be
passed as the second argument. However, because the <code>statusMessage</code> has no
meaning within HTTP/2, the argument will have no effect and a process warning
will be emitted.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-string">'hello world'</span>;
response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, {
<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(body),
<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
});</code> <button class="copy-button">copy</button></pre>
<p><code>Content-Length</code> is given in bytes not characters. The
<code>Buffer.byteLength()</code> API may be used to determine the number of bytes in a
given encoding. On outbound messages, Node.js does not check if Content-Length
and the length of the body being transmitted are equal or not. However, when
receiving messages, Node.js will automatically reject messages when the
<code>Content-Length</code> does not match the actual payload size.</p>
<p>This method may be called at most one time on a message before
<a href="#responseenddata-encoding-callback"><code>response.end()</code></a> is called.</p>
<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
this, the implicit/mutable headers will be calculated and call this function.</p>
<p>When headers have been set with <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>, they will be merged
with any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, with the headers passed
to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p>
<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span>
<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> {
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>);
res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
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> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>Attempting to set a header field name or value that contains invalid characters
will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p>
</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>
<p>The <a href="perf_hooks.html">Performance Observer</a> API can be used to collect basic performance
metrics for each <code>Http2Session</code> and <code>Http2Stream</code> instance.</p>
<pre class="with-59-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">PerformanceObserver</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:perf_hooks'</span>;
<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> {
<span class="hljs-keyword">const</span> entry = items.<span class="hljs-title function_">getEntries</span>()[<span class="hljs-number">0</span>];
<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>
<span class="hljs-keyword">if</span> (entry.<span class="hljs-property">name</span> === <span class="hljs-string">'Http2Session'</span>) {
<span class="hljs-comment">// Entry contains statistics about the Http2Session</span>
} <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>) {
<span class="hljs-comment">// Entry contains statistics about the Http2Stream</span>
}
});
obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'http2'</span>] });</code><code class="language-js cjs"><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>);
<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> {
<span class="hljs-keyword">const</span> entry = items.<span class="hljs-title function_">getEntries</span>()[<span class="hljs-number">0</span>];
<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>
<span class="hljs-keyword">if</span> (entry.<span class="hljs-property">name</span> === <span class="hljs-string">'Http2Session'</span>) {
<span class="hljs-comment">// Entry contains statistics about the Http2Session</span>
} <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>) {
<span class="hljs-comment">// Entry contains statistics about the Http2Stream</span>
}
});
obs.<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>
<p>The <code>entryType</code> property of the <code>PerformanceEntry</code> will be equal to <code>'http2'</code>.</p>
<p>The <code>name</code> property of the <code>PerformanceEntry</code> will be equal to either
<code>'Http2Stream'</code> or <code>'Http2Session'</code>.</p>
<p>If <code>name</code> is equal to <code>Http2Stream</code>, the <code>PerformanceEntry</code> will contain the
following additional properties:</p>
<ul>
<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
<code>Http2Stream</code>.</li>
<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
<code>Http2Stream</code>.</li>
<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>
<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
<code>PerformanceEntry</code> <code>startTime</code> and the reception of the first <code>DATA</code> frame.</li>
<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
the <code>PerformanceEntry</code> <code>startTime</code> and sending of the first <code>DATA</code> frame.</li>
<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
<code>PerformanceEntry</code> <code>startTime</code> and the reception of the first header.</li>
</ul>
<p>If <code>name</code> is equal to <code>Http2Session</code>, the <code>PerformanceEntry</code> will contain the
following additional properties:</p>
<ul>
<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>
<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>
<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
<code>Http2Session</code>.</li>
<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>
<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
open during the lifetime of the <code>Http2Session</code>.</li>
<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
of a <code>PING</code> frame and the reception of its acknowledgment. Only present if
a <code>PING</code> frame has been sent on the <code>Http2Session</code>.</li>
<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
all <code>Http2Stream</code> instances.</li>
<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
the <code>Http2Session</code>.</li>
<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
<code>Http2Session</code>.</li>
</ul>
</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>
<p>HTTP/2 requires requests to have either the <code>:authority</code> pseudo-header
or the <code>host</code> header. Prefer <code>:authority</code> when constructing an HTTP/2
request directly, and <code>host</code> when converting from HTTP/1 (in proxies,
for instance).</p>
<p>The compatibility API falls back to <code>host</code> if <code>:authority</code> is not
present. See <a href="#requestauthority"><code>request.authority</code></a> for more information. However,
if you don't use the compatibility API (or use <code>req.headers</code> directly),
you need to implement any fall-back behavior yourself.</p></section>
<!-- API END -->
</div>
</div>
</div>
</body>
</html>
|