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
|
<!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 | 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/http.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:694px){.with-59-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:510px){.with-36-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:502px){.with-35-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:494px){.with-34-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:606px){.with-48-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:638px){.with-52-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;}}</style>
</head>
<body class="alt apidoc" id="api-section-http">
<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 active">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">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="http" 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="#http">HTTP</a></span>
<ul>
<li><a href="#class-httpagent">Class: <code>http.Agent</code></a>
<ul>
<li><a href="#new-agentoptions"><code>new Agent([options])</code></a></li>
<li><a href="#agentcreateconnectionoptions-callback"><code>agent.createConnection(options[, callback])</code></a></li>
<li><a href="#agentkeepsocketalivesocket"><code>agent.keepSocketAlive(socket)</code></a></li>
<li><a href="#agentreusesocketsocket-request"><code>agent.reuseSocket(socket, request)</code></a></li>
<li><a href="#agentdestroy"><code>agent.destroy()</code></a></li>
<li><a href="#agentfreesockets"><code>agent.freeSockets</code></a></li>
<li><a href="#agentgetnameoptions"><code>agent.getName([options])</code></a></li>
<li><a href="#agentmaxfreesockets"><code>agent.maxFreeSockets</code></a></li>
<li><a href="#agentmaxsockets"><code>agent.maxSockets</code></a></li>
<li><a href="#agentmaxtotalsockets"><code>agent.maxTotalSockets</code></a></li>
<li><a href="#agentrequests"><code>agent.requests</code></a></li>
<li><a href="#agentsockets"><code>agent.sockets</code></a></li>
</ul>
</li>
<li><a href="#class-httpclientrequest">Class: <code>http.ClientRequest</code></a>
<ul>
<li><span class="stability_0"><a href="#event-abort">Event: <code>'abort'</code></a></span></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-continue">Event: <code>'continue'</code></a></li>
<li><a href="#event-finish">Event: <code>'finish'</code></a></li>
<li><a href="#event-information">Event: <code>'information'</code></a></li>
<li><a href="#event-response">Event: <code>'response'</code></a></li>
<li><a href="#event-socket">Event: <code>'socket'</code></a></li>
<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li>
<li><a href="#event-upgrade">Event: <code>'upgrade'</code></a></li>
<li><span class="stability_0"><a href="#requestabort"><code>request.abort()</code></a></span></li>
<li><span class="stability_0"><a href="#requestaborted"><code>request.aborted</code></a></span></li>
<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li>
<li><a href="#requestcork"><code>request.cork()</code></a></li>
<li><a href="#requestenddata-encoding-callback"><code>request.end([data[, encoding]][, callback])</code></a></li>
<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a>
<ul>
<li><a href="#requestdestroyed"><code>request.destroyed</code></a></li>
</ul>
</li>
<li><span class="stability_0"><a href="#requestfinished"><code>request.finished</code></a></span></li>
<li><a href="#requestflushheaders"><code>request.flushHeaders()</code></a></li>
<li><a href="#requestgetheadername"><code>request.getHeader(name)</code></a></li>
<li><a href="#requestgetheadernames"><code>request.getHeaderNames()</code></a></li>
<li><a href="#requestgetheaders"><code>request.getHeaders()</code></a></li>
<li><a href="#requestgetrawheadernames"><code>request.getRawHeaderNames()</code></a></li>
<li><a href="#requesthasheadername"><code>request.hasHeader(name)</code></a></li>
<li><a href="#requestmaxheaderscount"><code>request.maxHeadersCount</code></a></li>
<li><a href="#requestpath"><code>request.path</code></a></li>
<li><a href="#requestmethod"><code>request.method</code></a></li>
<li><a href="#requesthost"><code>request.host</code></a></li>
<li><a href="#requestprotocol"><code>request.protocol</code></a></li>
<li><a href="#requestremoveheadername"><code>request.removeHeader(name)</code></a></li>
<li><a href="#requestreusedsocket"><code>request.reusedSocket</code></a></li>
<li><a href="#requestsetheadername-value"><code>request.setHeader(name, value)</code></a></li>
<li><a href="#requestsetnodelaynodelay"><code>request.setNoDelay([noDelay])</code></a></li>
<li><a href="#requestsetsocketkeepaliveenable-initialdelay"><code>request.setSocketKeepAlive([enable][, initialDelay])</code></a></li>
<li><a href="#requestsettimeouttimeout-callback"><code>request.setTimeout(timeout[, callback])</code></a></li>
<li><a href="#requestsocket"><code>request.socket</code></a></li>
<li><a href="#requestuncork"><code>request.uncork()</code></a></li>
<li><a href="#requestwritableended"><code>request.writableEnded</code></a></li>
<li><a href="#requestwritablefinished"><code>request.writableFinished</code></a></li>
<li><a href="#requestwritechunk-encoding-callback"><code>request.write(chunk[, encoding][, callback])</code></a></li>
</ul>
</li>
<li><a href="#class-httpserver">Class: <code>http.Server</code></a>
<ul>
<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li>
<li><a href="#event-checkexpectation">Event: <code>'checkExpectation'</code></a></li>
<li><a href="#event-clienterror">Event: <code>'clientError'</code></a></li>
<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
<li><a href="#event-connect_1">Event: <code>'connect'</code></a></li>
<li><a href="#event-connection">Event: <code>'connection'</code></a></li>
<li><a href="#event-droprequest">Event: <code>'dropRequest'</code></a></li>
<li><a href="#event-request">Event: <code>'request'</code></a></li>
<li><a href="#event-upgrade_1">Event: <code>'upgrade'</code></a></li>
<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li>
<li><a href="#servercloseallconnections"><code>server.closeAllConnections()</code></a></li>
<li><a href="#servercloseidleconnections"><code>server.closeIdleConnections()</code></a></li>
<li><a href="#serverheaderstimeout"><code>server.headersTimeout</code></a></li>
<li><a href="#serverlisten"><code>server.listen()</code></a></li>
<li><a href="#serverlistening"><code>server.listening</code></a></li>
<li><a href="#servermaxheaderscount"><code>server.maxHeadersCount</code></a></li>
<li><a href="#serverrequesttimeout"><code>server.requestTimeout</code></a></li>
<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li>
<li><a href="#servermaxrequestspersocket"><code>server.maxRequestsPerSocket</code></a></li>
<li><a href="#servertimeout"><code>server.timeout</code></a></li>
<li><a href="#serverkeepalivetimeout"><code>server.keepAliveTimeout</code></a></li>
<li><span class="stability_1"><a href="#serversymbolasyncdispose"><code>server[Symbol.asyncDispose]()</code></a></span></li>
</ul>
</li>
<li><a href="#class-httpserverresponse">Class: <code>http.ServerResponse</code></a>
<ul>
<li><a href="#event-close_2">Event: <code>'close'</code></a></li>
<li><a href="#event-finish_1">Event: <code>'finish'</code></a></li>
<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li>
<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li>
<li><a href="#responsecork"><code>response.cork()</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="#responseflushheaders"><code>response.flushHeaders()</code></a></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="#responsestrictcontentlength"><code>response.strictContentLength</code></a></li>
<li><a href="#responseuncork"><code>response.uncork()</code></a></li>
<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li>
<li><a href="#responsewritablefinished"><code>response.writableFinished</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-callback"><code>response.writeEarlyHints(hints[, callback])</code></a></li>
<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li>
<li><a href="#responsewriteprocessing"><code>response.writeProcessing()</code></a></li>
</ul>
</li>
<li><a href="#class-httpincomingmessage">Class: <code>http.IncomingMessage</code></a>
<ul>
<li><span class="stability_0"><a href="#event-aborted">Event: <code>'aborted'</code></a></span></li>
<li><a href="#event-close_3">Event: <code>'close'</code></a></li>
<li><span class="stability_0"><a href="#messageaborted"><code>message.aborted</code></a></span></li>
<li><a href="#messagecomplete"><code>message.complete</code></a></li>
<li><span class="stability_0"><a href="#messageconnection"><code>message.connection</code></a></span></li>
<li><a href="#messagedestroyerror"><code>message.destroy([error])</code></a></li>
<li><a href="#messageheaders"><code>message.headers</code></a></li>
<li><a href="#messageheadersdistinct"><code>message.headersDistinct</code></a></li>
<li><a href="#messagehttpversion"><code>message.httpVersion</code></a></li>
<li><a href="#messagemethod"><code>message.method</code></a></li>
<li><a href="#messagerawheaders"><code>message.rawHeaders</code></a></li>
<li><a href="#messagerawtrailers"><code>message.rawTrailers</code></a></li>
<li><a href="#messagesettimeoutmsecs-callback"><code>message.setTimeout(msecs[, callback])</code></a></li>
<li><a href="#messagesocket"><code>message.socket</code></a></li>
<li><a href="#messagestatuscode"><code>message.statusCode</code></a></li>
<li><a href="#messagestatusmessage"><code>message.statusMessage</code></a></li>
<li><a href="#messagetrailers"><code>message.trailers</code></a></li>
<li><a href="#messagetrailersdistinct"><code>message.trailersDistinct</code></a></li>
<li><a href="#messageurl"><code>message.url</code></a></li>
</ul>
</li>
<li><a href="#class-httpoutgoingmessage">Class: <code>http.OutgoingMessage</code></a>
<ul>
<li><a href="#event-drain">Event: <code>'drain'</code></a></li>
<li><a href="#event-finish_2">Event: <code>'finish'</code></a></li>
<li><a href="#event-prefinish">Event: <code>'prefinish'</code></a></li>
<li><a href="#outgoingmessageaddtrailersheaders"><code>outgoingMessage.addTrailers(headers)</code></a></li>
<li><a href="#outgoingmessageappendheadername-value"><code>outgoingMessage.appendHeader(name, value)</code></a></li>
<li><span class="stability_0"><a href="#outgoingmessageconnection"><code>outgoingMessage.connection</code></a></span></li>
<li><a href="#outgoingmessagecork"><code>outgoingMessage.cork()</code></a></li>
<li><a href="#outgoingmessagedestroyerror"><code>outgoingMessage.destroy([error])</code></a></li>
<li><a href="#outgoingmessageendchunk-encoding-callback"><code>outgoingMessage.end(chunk[, encoding][, callback])</code></a></li>
<li><a href="#outgoingmessageflushheaders"><code>outgoingMessage.flushHeaders()</code></a></li>
<li><a href="#outgoingmessagegetheadername"><code>outgoingMessage.getHeader(name)</code></a></li>
<li><a href="#outgoingmessagegetheadernames"><code>outgoingMessage.getHeaderNames()</code></a></li>
<li><a href="#outgoingmessagegetheaders"><code>outgoingMessage.getHeaders()</code></a></li>
<li><a href="#outgoingmessagehasheadername"><code>outgoingMessage.hasHeader(name)</code></a></li>
<li><a href="#outgoingmessageheaderssent"><code>outgoingMessage.headersSent</code></a></li>
<li><a href="#outgoingmessagepipe"><code>outgoingMessage.pipe()</code></a></li>
<li><a href="#outgoingmessageremoveheadername"><code>outgoingMessage.removeHeader(name)</code></a></li>
<li><a href="#outgoingmessagesetheadername-value"><code>outgoingMessage.setHeader(name, value)</code></a></li>
<li><a href="#outgoingmessagesetheadersheaders"><code>outgoingMessage.setHeaders(headers)</code></a></li>
<li><a href="#outgoingmessagesettimeoutmsesc-callback"><code>outgoingMessage.setTimeout(msesc[, callback])</code></a></li>
<li><a href="#outgoingmessagesocket"><code>outgoingMessage.socket</code></a></li>
<li><a href="#outgoingmessageuncork"><code>outgoingMessage.uncork()</code></a></li>
<li><a href="#outgoingmessagewritablecorked"><code>outgoingMessage.writableCorked</code></a></li>
<li><a href="#outgoingmessagewritableended"><code>outgoingMessage.writableEnded</code></a></li>
<li><a href="#outgoingmessagewritablefinished"><code>outgoingMessage.writableFinished</code></a></li>
<li><a href="#outgoingmessagewritablehighwatermark"><code>outgoingMessage.writableHighWaterMark</code></a></li>
<li><a href="#outgoingmessagewritablelength"><code>outgoingMessage.writableLength</code></a></li>
<li><a href="#outgoingmessagewritableobjectmode"><code>outgoingMessage.writableObjectMode</code></a></li>
<li><a href="#outgoingmessagewritechunk-encoding-callback"><code>outgoingMessage.write(chunk[, encoding][, callback])</code></a></li>
</ul>
</li>
<li><a href="#httpmethods"><code>http.METHODS</code></a></li>
<li><a href="#httpstatus_codes"><code>http.STATUS_CODES</code></a></li>
<li><a href="#httpcreateserveroptions-requestlistener"><code>http.createServer([options][, requestListener])</code></a></li>
<li><a href="#httpgetoptions-callback"><code>http.get(options[, callback])</code></a></li>
<li><a href="#httpgeturl-options-callback"><code>http.get(url[, options][, callback])</code></a></li>
<li><a href="#httpglobalagent"><code>http.globalAgent</code></a></li>
<li><a href="#httpmaxheadersize"><code>http.maxHeaderSize</code></a></li>
<li><a href="#httprequestoptions-callback"><code>http.request(options[, callback])</code></a></li>
<li><a href="#httprequesturl-options-callback"><code>http.request(url[, options][, callback])</code></a></li>
<li><a href="#httpvalidateheadernamename-label"><code>http.validateHeaderName(name[, label])</code></a></li>
<li><a href="#httpvalidateheadervaluename-value"><code>http.validateHeaderValue(name, value)</code></a></li>
<li><a href="#httpsetmaxidlehttpparsersmax"><code>http.setMaxIdleHTTPParsers(max)</code></a></li>
<li><a href="#websocket"><code>WebSocket</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 active">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">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/http.html">23.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v22.x/api/http.html">22.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v21.x/api/http.html">21.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v20.x/api/http.html">20.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v19.x/api/http.html">19.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v18.x/api/http.html">18.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v17.x/api/http.html">17.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v16.x/api/http.html">16.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v15.x/api/http.html">15.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v14.x/api/http.html">14.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v13.x/api/http.html">13.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v12.x/api/http.html">12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v11.x/api/http.html">11.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v10.x/api/http.html">10.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v9.x/api/http.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/http.html">8.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v7.x/api/http.html">7.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v6.x/api/http.html">6.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v5.x/api/http.html">5.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v4.x/api/http.html">4.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/http.html">0.12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/http.html">0.10.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="http.json">View as JSON</a>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/http.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="#http">HTTP</a></span>
<ul>
<li><a href="#class-httpagent">Class: <code>http.Agent</code></a>
<ul>
<li><a href="#new-agentoptions"><code>new Agent([options])</code></a></li>
<li><a href="#agentcreateconnectionoptions-callback"><code>agent.createConnection(options[, callback])</code></a></li>
<li><a href="#agentkeepsocketalivesocket"><code>agent.keepSocketAlive(socket)</code></a></li>
<li><a href="#agentreusesocketsocket-request"><code>agent.reuseSocket(socket, request)</code></a></li>
<li><a href="#agentdestroy"><code>agent.destroy()</code></a></li>
<li><a href="#agentfreesockets"><code>agent.freeSockets</code></a></li>
<li><a href="#agentgetnameoptions"><code>agent.getName([options])</code></a></li>
<li><a href="#agentmaxfreesockets"><code>agent.maxFreeSockets</code></a></li>
<li><a href="#agentmaxsockets"><code>agent.maxSockets</code></a></li>
<li><a href="#agentmaxtotalsockets"><code>agent.maxTotalSockets</code></a></li>
<li><a href="#agentrequests"><code>agent.requests</code></a></li>
<li><a href="#agentsockets"><code>agent.sockets</code></a></li>
</ul>
</li>
<li><a href="#class-httpclientrequest">Class: <code>http.ClientRequest</code></a>
<ul>
<li><span class="stability_0"><a href="#event-abort">Event: <code>'abort'</code></a></span></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-continue">Event: <code>'continue'</code></a></li>
<li><a href="#event-finish">Event: <code>'finish'</code></a></li>
<li><a href="#event-information">Event: <code>'information'</code></a></li>
<li><a href="#event-response">Event: <code>'response'</code></a></li>
<li><a href="#event-socket">Event: <code>'socket'</code></a></li>
<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li>
<li><a href="#event-upgrade">Event: <code>'upgrade'</code></a></li>
<li><span class="stability_0"><a href="#requestabort"><code>request.abort()</code></a></span></li>
<li><span class="stability_0"><a href="#requestaborted"><code>request.aborted</code></a></span></li>
<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li>
<li><a href="#requestcork"><code>request.cork()</code></a></li>
<li><a href="#requestenddata-encoding-callback"><code>request.end([data[, encoding]][, callback])</code></a></li>
<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a>
<ul>
<li><a href="#requestdestroyed"><code>request.destroyed</code></a></li>
</ul>
</li>
<li><span class="stability_0"><a href="#requestfinished"><code>request.finished</code></a></span></li>
<li><a href="#requestflushheaders"><code>request.flushHeaders()</code></a></li>
<li><a href="#requestgetheadername"><code>request.getHeader(name)</code></a></li>
<li><a href="#requestgetheadernames"><code>request.getHeaderNames()</code></a></li>
<li><a href="#requestgetheaders"><code>request.getHeaders()</code></a></li>
<li><a href="#requestgetrawheadernames"><code>request.getRawHeaderNames()</code></a></li>
<li><a href="#requesthasheadername"><code>request.hasHeader(name)</code></a></li>
<li><a href="#requestmaxheaderscount"><code>request.maxHeadersCount</code></a></li>
<li><a href="#requestpath"><code>request.path</code></a></li>
<li><a href="#requestmethod"><code>request.method</code></a></li>
<li><a href="#requesthost"><code>request.host</code></a></li>
<li><a href="#requestprotocol"><code>request.protocol</code></a></li>
<li><a href="#requestremoveheadername"><code>request.removeHeader(name)</code></a></li>
<li><a href="#requestreusedsocket"><code>request.reusedSocket</code></a></li>
<li><a href="#requestsetheadername-value"><code>request.setHeader(name, value)</code></a></li>
<li><a href="#requestsetnodelaynodelay"><code>request.setNoDelay([noDelay])</code></a></li>
<li><a href="#requestsetsocketkeepaliveenable-initialdelay"><code>request.setSocketKeepAlive([enable][, initialDelay])</code></a></li>
<li><a href="#requestsettimeouttimeout-callback"><code>request.setTimeout(timeout[, callback])</code></a></li>
<li><a href="#requestsocket"><code>request.socket</code></a></li>
<li><a href="#requestuncork"><code>request.uncork()</code></a></li>
<li><a href="#requestwritableended"><code>request.writableEnded</code></a></li>
<li><a href="#requestwritablefinished"><code>request.writableFinished</code></a></li>
<li><a href="#requestwritechunk-encoding-callback"><code>request.write(chunk[, encoding][, callback])</code></a></li>
</ul>
</li>
<li><a href="#class-httpserver">Class: <code>http.Server</code></a>
<ul>
<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li>
<li><a href="#event-checkexpectation">Event: <code>'checkExpectation'</code></a></li>
<li><a href="#event-clienterror">Event: <code>'clientError'</code></a></li>
<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
<li><a href="#event-connect_1">Event: <code>'connect'</code></a></li>
<li><a href="#event-connection">Event: <code>'connection'</code></a></li>
<li><a href="#event-droprequest">Event: <code>'dropRequest'</code></a></li>
<li><a href="#event-request">Event: <code>'request'</code></a></li>
<li><a href="#event-upgrade_1">Event: <code>'upgrade'</code></a></li>
<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li>
<li><a href="#servercloseallconnections"><code>server.closeAllConnections()</code></a></li>
<li><a href="#servercloseidleconnections"><code>server.closeIdleConnections()</code></a></li>
<li><a href="#serverheaderstimeout"><code>server.headersTimeout</code></a></li>
<li><a href="#serverlisten"><code>server.listen()</code></a></li>
<li><a href="#serverlistening"><code>server.listening</code></a></li>
<li><a href="#servermaxheaderscount"><code>server.maxHeadersCount</code></a></li>
<li><a href="#serverrequesttimeout"><code>server.requestTimeout</code></a></li>
<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li>
<li><a href="#servermaxrequestspersocket"><code>server.maxRequestsPerSocket</code></a></li>
<li><a href="#servertimeout"><code>server.timeout</code></a></li>
<li><a href="#serverkeepalivetimeout"><code>server.keepAliveTimeout</code></a></li>
<li><span class="stability_1"><a href="#serversymbolasyncdispose"><code>server[Symbol.asyncDispose]()</code></a></span></li>
</ul>
</li>
<li><a href="#class-httpserverresponse">Class: <code>http.ServerResponse</code></a>
<ul>
<li><a href="#event-close_2">Event: <code>'close'</code></a></li>
<li><a href="#event-finish_1">Event: <code>'finish'</code></a></li>
<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li>
<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li>
<li><a href="#responsecork"><code>response.cork()</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="#responseflushheaders"><code>response.flushHeaders()</code></a></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="#responsestrictcontentlength"><code>response.strictContentLength</code></a></li>
<li><a href="#responseuncork"><code>response.uncork()</code></a></li>
<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li>
<li><a href="#responsewritablefinished"><code>response.writableFinished</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-callback"><code>response.writeEarlyHints(hints[, callback])</code></a></li>
<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li>
<li><a href="#responsewriteprocessing"><code>response.writeProcessing()</code></a></li>
</ul>
</li>
<li><a href="#class-httpincomingmessage">Class: <code>http.IncomingMessage</code></a>
<ul>
<li><span class="stability_0"><a href="#event-aborted">Event: <code>'aborted'</code></a></span></li>
<li><a href="#event-close_3">Event: <code>'close'</code></a></li>
<li><span class="stability_0"><a href="#messageaborted"><code>message.aborted</code></a></span></li>
<li><a href="#messagecomplete"><code>message.complete</code></a></li>
<li><span class="stability_0"><a href="#messageconnection"><code>message.connection</code></a></span></li>
<li><a href="#messagedestroyerror"><code>message.destroy([error])</code></a></li>
<li><a href="#messageheaders"><code>message.headers</code></a></li>
<li><a href="#messageheadersdistinct"><code>message.headersDistinct</code></a></li>
<li><a href="#messagehttpversion"><code>message.httpVersion</code></a></li>
<li><a href="#messagemethod"><code>message.method</code></a></li>
<li><a href="#messagerawheaders"><code>message.rawHeaders</code></a></li>
<li><a href="#messagerawtrailers"><code>message.rawTrailers</code></a></li>
<li><a href="#messagesettimeoutmsecs-callback"><code>message.setTimeout(msecs[, callback])</code></a></li>
<li><a href="#messagesocket"><code>message.socket</code></a></li>
<li><a href="#messagestatuscode"><code>message.statusCode</code></a></li>
<li><a href="#messagestatusmessage"><code>message.statusMessage</code></a></li>
<li><a href="#messagetrailers"><code>message.trailers</code></a></li>
<li><a href="#messagetrailersdistinct"><code>message.trailersDistinct</code></a></li>
<li><a href="#messageurl"><code>message.url</code></a></li>
</ul>
</li>
<li><a href="#class-httpoutgoingmessage">Class: <code>http.OutgoingMessage</code></a>
<ul>
<li><a href="#event-drain">Event: <code>'drain'</code></a></li>
<li><a href="#event-finish_2">Event: <code>'finish'</code></a></li>
<li><a href="#event-prefinish">Event: <code>'prefinish'</code></a></li>
<li><a href="#outgoingmessageaddtrailersheaders"><code>outgoingMessage.addTrailers(headers)</code></a></li>
<li><a href="#outgoingmessageappendheadername-value"><code>outgoingMessage.appendHeader(name, value)</code></a></li>
<li><span class="stability_0"><a href="#outgoingmessageconnection"><code>outgoingMessage.connection</code></a></span></li>
<li><a href="#outgoingmessagecork"><code>outgoingMessage.cork()</code></a></li>
<li><a href="#outgoingmessagedestroyerror"><code>outgoingMessage.destroy([error])</code></a></li>
<li><a href="#outgoingmessageendchunk-encoding-callback"><code>outgoingMessage.end(chunk[, encoding][, callback])</code></a></li>
<li><a href="#outgoingmessageflushheaders"><code>outgoingMessage.flushHeaders()</code></a></li>
<li><a href="#outgoingmessagegetheadername"><code>outgoingMessage.getHeader(name)</code></a></li>
<li><a href="#outgoingmessagegetheadernames"><code>outgoingMessage.getHeaderNames()</code></a></li>
<li><a href="#outgoingmessagegetheaders"><code>outgoingMessage.getHeaders()</code></a></li>
<li><a href="#outgoingmessagehasheadername"><code>outgoingMessage.hasHeader(name)</code></a></li>
<li><a href="#outgoingmessageheaderssent"><code>outgoingMessage.headersSent</code></a></li>
<li><a href="#outgoingmessagepipe"><code>outgoingMessage.pipe()</code></a></li>
<li><a href="#outgoingmessageremoveheadername"><code>outgoingMessage.removeHeader(name)</code></a></li>
<li><a href="#outgoingmessagesetheadername-value"><code>outgoingMessage.setHeader(name, value)</code></a></li>
<li><a href="#outgoingmessagesetheadersheaders"><code>outgoingMessage.setHeaders(headers)</code></a></li>
<li><a href="#outgoingmessagesettimeoutmsesc-callback"><code>outgoingMessage.setTimeout(msesc[, callback])</code></a></li>
<li><a href="#outgoingmessagesocket"><code>outgoingMessage.socket</code></a></li>
<li><a href="#outgoingmessageuncork"><code>outgoingMessage.uncork()</code></a></li>
<li><a href="#outgoingmessagewritablecorked"><code>outgoingMessage.writableCorked</code></a></li>
<li><a href="#outgoingmessagewritableended"><code>outgoingMessage.writableEnded</code></a></li>
<li><a href="#outgoingmessagewritablefinished"><code>outgoingMessage.writableFinished</code></a></li>
<li><a href="#outgoingmessagewritablehighwatermark"><code>outgoingMessage.writableHighWaterMark</code></a></li>
<li><a href="#outgoingmessagewritablelength"><code>outgoingMessage.writableLength</code></a></li>
<li><a href="#outgoingmessagewritableobjectmode"><code>outgoingMessage.writableObjectMode</code></a></li>
<li><a href="#outgoingmessagewritechunk-encoding-callback"><code>outgoingMessage.write(chunk[, encoding][, callback])</code></a></li>
</ul>
</li>
<li><a href="#httpmethods"><code>http.METHODS</code></a></li>
<li><a href="#httpstatus_codes"><code>http.STATUS_CODES</code></a></li>
<li><a href="#httpcreateserveroptions-requestlistener"><code>http.createServer([options][, requestListener])</code></a></li>
<li><a href="#httpgetoptions-callback"><code>http.get(options[, callback])</code></a></li>
<li><a href="#httpgeturl-options-callback"><code>http.get(url[, options][, callback])</code></a></li>
<li><a href="#httpglobalagent"><code>http.globalAgent</code></a></li>
<li><a href="#httpmaxheadersize"><code>http.maxHeaderSize</code></a></li>
<li><a href="#httprequestoptions-callback"><code>http.request(options[, callback])</code></a></li>
<li><a href="#httprequesturl-options-callback"><code>http.request(url[, options][, callback])</code></a></li>
<li><a href="#httpvalidateheadernamename-label"><code>http.validateHeaderName(name[, label])</code></a></li>
<li><a href="#httpvalidateheadervaluename-value"><code>http.validateHeaderValue(name, value)</code></a></li>
<li><a href="#httpsetmaxidlehttpparsersmax"><code>http.setMaxIdleHTTPParsers(max)</code></a></li>
<li><a href="#websocket"><code>WebSocket</code></a></li>
</ul>
</li>
</ul></details>
<div role="main" id="apicontent">
<h2>HTTP<span><a class="mark" href="#http" id="http">#</a></span><a aria-hidden="true" class="legacy" id="http_http"></a></h2>
<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/http.js">lib/http.js</a></p>
<p>This module, containing both a client and server, can be imported via
<code>require('node:http')</code> (CommonJS) or <code>import * as http from 'node:http'</code> (ES module).</p>
<p>The HTTP interfaces in Node.js are designed to support many features
of the protocol which have been traditionally difficult to use.
In particular, large, possibly chunk-encoded, messages. The interface is
careful to never buffer entire requests or responses, so the
user is able to stream data.</p>
<p>HTTP message headers are represented by an object like this:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span> <span class="hljs-attr">"content-length"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"123"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"content-type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"text/plain"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"connection"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"keep-alive"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"host"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"example.com"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"accept"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"*/*"</span> <span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
<p>Keys are lowercased. Values are not modified.</p>
<p>In order to support the full spectrum of possible HTTP applications, the Node.js
HTTP API is very low-level. It deals with stream handling and message
parsing only. It parses a message into headers and body but it does not
parse the actual headers or the body.</p>
<p>See <a href="#messageheaders"><code>message.headers</code></a> for details on how duplicate headers are handled.</p>
<p>The raw headers as they were received are retained in the <code>rawHeaders</code>
property, which is an array of <code>[key, value, key2, value2, ...]</code>. For
example, the previous message header object might have a <code>rawHeaders</code>
list like the following:</p>
<!-- eslint-disable @stylistic/js/semi -->
<pre><code class="language-js">[ <span class="hljs-string">'ConTent-Length'</span>, <span class="hljs-string">'123456'</span>,
<span class="hljs-string">'content-LENGTH'</span>, <span class="hljs-string">'123'</span>,
<span class="hljs-string">'content-type'</span>, <span class="hljs-string">'text/plain'</span>,
<span class="hljs-string">'CONNECTION'</span>, <span class="hljs-string">'keep-alive'</span>,
<span class="hljs-string">'Host'</span>, <span class="hljs-string">'example.com'</span>,
<span class="hljs-string">'accepT'</span>, <span class="hljs-string">'*/*'</span> ]</code> <button class="copy-button">copy</button></pre>
<section><h3>Class: <code>http.Agent</code><span><a class="mark" href="#class-httpagent" id="class-httpagent">#</a></span><a aria-hidden="true" class="legacy" id="http_class_http_agent"></a></h3>
<div class="api_metadata">
<span>Added in: v0.3.4</span>
</div>
<p>An <code>Agent</code> is responsible for managing connection persistence
and reuse for HTTP clients. It maintains a queue of pending requests
for a given host and port, reusing a single socket connection for each
until the queue is empty, at which time the socket is either destroyed
or put into a pool where it is kept to be used again for requests to the
same host and port. Whether it is destroyed or pooled depends on the
<code>keepAlive</code> <a href="#new-agentoptions">option</a>.</p>
<p>Pooled connections have TCP Keep-Alive enabled for them, but servers may
still close idle connections, in which case they will be removed from the
pool and a new connection will be made when a new HTTP request is made for
that host and port. Servers may also refuse to allow multiple requests
over the same connection, in which case the connection will have to be
remade for every request and cannot be pooled. The <code>Agent</code> will still make
the requests to that server, but each one will occur over a new connection.</p>
<p>When a connection is closed by the client or the server, it is removed
from the pool. Any unused sockets in the pool will be unrefed so as not
to keep the Node.js process running when there are no outstanding requests.
(see <a href="net.html#socketunref"><code>socket.unref()</code></a>).</p>
<p>It is good practice, to <a href="#agentdestroy"><code>destroy()</code></a> an <code>Agent</code> instance when it is no
longer in use, because unused sockets consume OS resources.</p>
<p>Sockets are removed from an agent when the socket emits either
a <code>'close'</code> event or an <code>'agentRemove'</code> event. When intending to keep one
HTTP request open for a long time without keeping it in the agent, something
like the following may be done:</p>
<pre><code class="language-js">http.<span class="hljs-title function_">get</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
<span class="hljs-comment">// Do stuff</span>
}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'socket'</span>, <span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
socket.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'agentRemove'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>An agent may also be used for an individual request. By providing
<code>{agent: false}</code> as an option to the <code>http.get()</code> or <code>http.request()</code>
functions, a one-time use <code>Agent</code> with default options will be used
for the client connection.</p>
<p><code>agent:false</code>:</p>
<pre><code class="language-js">http.<span class="hljs-title function_">get</span>({
<span class="hljs-attr">hostname</span>: <span class="hljs-string">'localhost'</span>,
<span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
<span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
<span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span>, <span class="hljs-comment">// Create a new agent just for this one request</span>
}, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
<span class="hljs-comment">// Do stuff with response</span>
});</code> <button class="copy-button">copy</button></pre>
<h4><code>new Agent([options])</code><span><a class="mark" href="#new-agentoptions" id="new-agentoptions">#</a></span><a aria-hidden="true" class="legacy" id="http_new_agent_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.6.0, v14.17.0</td>
<td><p>Change the default scheduling from 'fifo' to 'lifo'.</p></td></tr>
<tr><td>v14.5.0, v12.20.0</td>
<td><p>Add <code>scheduling</code> option to specify the free socket scheduling strategy.</p></td></tr>
<tr><td>v14.5.0, v12.19.0</td>
<td><p>Add <code>maxTotalSockets</code> option to agent constructor.</p></td></tr>
<tr><td>v0.3.4</td>
<td><p><span>Added in: v0.3.4</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> Set of configurable options to set on the agent.
Can have the following fields:
<ul>
<li><code>keepAlive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Keep sockets around even when there are no
outstanding requests, so they can be used for future requests without
having to reestablish a TCP connection. Not to be confused with the
<code>keep-alive</code> value of the <code>Connection</code> header. The <code>Connection: keep-alive</code>
header is always sent when using an agent except when the <code>Connection</code>
header is explicitly specified or when the <code>keepAlive</code> and <code>maxSockets</code>
options are respectively set to <code>false</code> and <code>Infinity</code>, in which case
<code>Connection: close</code> will be used. <strong>Default:</strong> <code>false</code>.</li>
<li><code>keepAliveMsecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> When using the <code>keepAlive</code> option, specifies
the <a href="net.html#socketsetkeepaliveenable-initialdelay">initial delay</a>
for TCP Keep-Alive packets. Ignored when the
<code>keepAlive</code> option is <code>false</code> or <code>undefined</code>. <strong>Default:</strong> <code>1000</code>.</li>
<li><code>maxSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Maximum number of sockets to allow per host.
If the same host opens multiple concurrent connections, each request
will use new socket until the <code>maxSockets</code> value is reached.
If the host attempts to open more connections than <code>maxSockets</code>,
the additional requests will enter into a pending request queue, and
will enter active connection state when an existing connection terminates.
This makes sure there are at most <code>maxSockets</code> active connections at
any point in time, from a given host.
<strong>Default:</strong> <code>Infinity</code>.</li>
<li><code>maxTotalSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Maximum number of sockets allowed for
all hosts in total. Each request will use a new socket
until the maximum is reached.
<strong>Default:</strong> <code>Infinity</code>.</li>
<li><code>maxFreeSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Maximum number of sockets per host to leave open
in a free state. Only relevant if <code>keepAlive</code> is set to <code>true</code>.
<strong>Default:</strong> <code>256</code>.</li>
<li><code>scheduling</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Scheduling strategy to apply when picking
the next free socket to use. It can be <code>'fifo'</code> or <code>'lifo'</code>.
The main difference between the two scheduling strategies is that <code>'lifo'</code>
selects the most recently used socket, while <code>'fifo'</code> selects
the least recently used socket.
In case of a low rate of request per second, the <code>'lifo'</code> scheduling
will lower the risk of picking a socket that might have been closed
by the server due to inactivity.
In case of a high rate of request per second,
the <code>'fifo'</code> scheduling will maximize the number of open sockets,
while the <code>'lifo'</code> scheduling will keep it as low as possible.
<strong>Default:</strong> <code>'lifo'</code>.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Socket timeout in milliseconds.
This will set the timeout when the socket is created.</li>
</ul>
</li>
</ul>
<p><code>options</code> in <a href="net.html#socketconnectoptions-connectlistener"><code>socket.connect()</code></a> are also supported.</p>
<p>To configure any of them, a custom <a href="#class-httpagent"><code>http.Agent</code></a> instance must be created.</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_">Agent</span>, request } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">const</span> keepAliveAgent = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Agent</span>({ <span class="hljs-attr">keepAlive</span>: <span class="hljs-literal">true</span> });
options.<span class="hljs-property">agent</span> = keepAliveAgent;
<span class="hljs-title function_">request</span>(options, onResponseCallback);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">const</span> keepAliveAgent = <span class="hljs-keyword">new</span> http.<span class="hljs-title class_">Agent</span>({ <span class="hljs-attr">keepAlive</span>: <span class="hljs-literal">true</span> });
options.<span class="hljs-property">agent</span> = keepAliveAgent;
http.<span class="hljs-title function_">request</span>(options, onResponseCallback);</code><button class="copy-button">copy</button></pre>
<h4><code>agent.createConnection(options[, callback])</code><span><a class="mark" href="#agentcreateconnectionoptions-callback" id="agentcreateconnectionoptions-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_createconnection_options_callback"></a></h4>
<div class="api_metadata">
<span>Added in: v0.11.4</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> Options containing connection details. Check
<a href="net.html#netcreateconnectionoptions-connectlistener"><code>net.createConnection()</code></a> for the format of the options</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 function that receives the created socket</li>
<li>Returns: <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>Produces a socket/stream to be used for HTTP requests.</p>
<p>By default, this function is the same as <a href="net.html#netcreateconnectionoptions-connectlistener"><code>net.createConnection()</code></a>. However,
custom agents may override this method in case greater flexibility is desired.</p>
<p>A socket/stream can be supplied in one of two ways: by returning the
socket/stream from this function, or by passing the socket/stream to <code>callback</code>.</p>
<p>This method is guaranteed to return an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<p><code>callback</code> has a signature of <code>(err, stream)</code>.</p>
<h4><code>agent.keepSocketAlive(socket)</code><span><a class="mark" href="#agentkeepsocketalivesocket" id="agentkeepsocketalivesocket">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_keepsocketalive_socket"></a></h4>
<div class="api_metadata">
<span>Added in: v8.1.0</span>
</div>
<ul>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>Called when <code>socket</code> is detached from a request and could be persisted by the
<code>Agent</code>. Default behavior is to:</p>
<pre><code class="language-js">socket.<span class="hljs-title function_">setKeepAlive</span>(<span class="hljs-literal">true</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">keepAliveMsecs</span>);
socket.<span class="hljs-title function_">unref</span>();
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;</code> <button class="copy-button">copy</button></pre>
<p>This method can be overridden by a particular <code>Agent</code> subclass. If this
method returns a falsy value, the socket will be destroyed instead of persisting
it for use with the next request.</p>
<p>The <code>socket</code> argument can be an instance of <a href="net.html#class-netsocket" class="type"><net.Socket></a>, a subclass of
<a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>.</p>
<h4><code>agent.reuseSocket(socket, request)</code><span><a class="mark" href="#agentreusesocketsocket-request" id="agentreusesocketsocket-request">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_reusesocket_socket_request"></a></h4>
<div class="api_metadata">
<span>Added in: v8.1.0</span>
</div>
<ul>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
<li><code>request</code> <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li>
</ul>
<p>Called when <code>socket</code> is attached to <code>request</code> after being persisted because of
the keep-alive options. Default behavior is to:</p>
<pre><code class="language-js">socket.<span class="hljs-title function_">ref</span>();</code> <button class="copy-button">copy</button></pre>
<p>This method can be overridden by a particular <code>Agent</code> subclass.</p>
<p>The <code>socket</code> argument can be an instance of <a href="net.html#class-netsocket" class="type"><net.Socket></a>, a subclass of
<a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>.</p>
<h4><code>agent.destroy()</code><span><a class="mark" href="#agentdestroy" id="agentdestroy">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_destroy"></a></h4>
<div class="api_metadata">
<span>Added in: v0.11.4</span>
</div>
<p>Destroy any sockets that are currently in use by the agent.</p>
<p>It is usually not necessary to do this. However, if using an
agent with <code>keepAlive</code> enabled, then it is best to explicitly shut down
the agent when it is no longer needed. Otherwise,
sockets might stay open for quite a long time before the server
terminates them.</p>
<h4><code>agent.freeSockets</code><span><a class="mark" href="#agentfreesockets" id="agentfreesockets">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_freesockets"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v16.0.0</td>
<td><p>The property now has a <code>null</code> prototype.</p></td></tr>
<tr><td>v0.11.4</td>
<td><p><span>Added in: v0.11.4</span></p></td></tr>
</tbody></table>
</details>
</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>An object which contains arrays of sockets currently awaiting use by
the agent when <code>keepAlive</code> is enabled. Do not modify.</p>
<p>Sockets in the <code>freeSockets</code> list will be automatically destroyed and
removed from the array on <code>'timeout'</code>.</p>
<h4><code>agent.getName([options])</code><span><a class="mark" href="#agentgetnameoptions" id="agentgetnameoptions">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_getname_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v17.7.0, v16.15.0</td>
<td><p>The <code>options</code> parameter is now optional.</p></td></tr>
<tr><td>v0.11.4</td>
<td><p><span>Added in: v0.11.4</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> A set of options providing information for name generation
<ul>
<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A domain name or IP address of the server to issue the
request to</li>
<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Port of remote server</li>
<li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Local interface to bind for network connections
when issuing the request</li>
<li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Must be 4 or 6 if this doesn't equal <code>undefined</code>.</li>
</ul>
</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>Get a unique name for a set of request options, to determine whether a
connection can be reused. For an HTTP agent, this returns
<code>host:port:localAddress</code> or <code>host:port:localAddress:family</code>. For an HTTPS agent,
the name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options
that determine socket reusability.</p>
<h4><code>agent.maxFreeSockets</code><span><a class="mark" href="#agentmaxfreesockets" id="agentmaxfreesockets">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_maxfreesockets"></a></h4>
<div class="api_metadata">
<span>Added in: v0.11.7</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>By default set to 256. For agents with <code>keepAlive</code> enabled, this
sets the maximum number of sockets that will be left open in the free
state.</p>
<h4><code>agent.maxSockets</code><span><a class="mark" href="#agentmaxsockets" id="agentmaxsockets">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_maxsockets"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.6</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>By default set to <code>Infinity</code>. Determines how many concurrent sockets the agent
can have open per origin. Origin is the returned value of <a href="#agentgetnameoptions"><code>agent.getName()</code></a>.</p>
<h4><code>agent.maxTotalSockets</code><span><a class="mark" href="#agentmaxtotalsockets" id="agentmaxtotalsockets">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_maxtotalsockets"></a></h4>
<div class="api_metadata">
<span>Added in: v14.5.0, v12.19.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>By default set to <code>Infinity</code>. Determines how many concurrent sockets the agent
can have open. Unlike <code>maxSockets</code>, this parameter applies across all origins.</p>
<h4><code>agent.requests</code><span><a class="mark" href="#agentrequests" id="agentrequests">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_requests"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v16.0.0</td>
<td><p>The property now has a <code>null</code> prototype.</p></td></tr>
<tr><td>v0.5.9</td>
<td><p><span>Added in: v0.5.9</span></p></td></tr>
</tbody></table>
</details>
</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>An object which contains queues of requests that have not yet been assigned to
sockets. Do not modify.</p>
<h4><code>agent.sockets</code><span><a class="mark" href="#agentsockets" id="agentsockets">#</a></span><a aria-hidden="true" class="legacy" id="http_agent_sockets"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v16.0.0</td>
<td><p>The property now has a <code>null</code> prototype.</p></td></tr>
<tr><td>v0.3.6</td>
<td><p><span>Added in: v0.3.6</span></p></td></tr>
</tbody></table>
</details>
</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>An object which contains arrays of sockets currently in use by the
agent. Do not modify.</p>
</section><section><h3>Class: <code>http.ClientRequest</code><span><a class="mark" href="#class-httpclientrequest" id="class-httpclientrequest">#</a></span><a aria-hidden="true" class="legacy" id="http_class_http_clientrequest"></a></h3>
<div class="api_metadata">
<span>Added in: v0.1.17</span>
</div>
<ul>
<li>Extends: <a href="http.html#class-httpoutgoingmessage" class="type"><http.OutgoingMessage></a></li>
</ul>
<p>This object is created internally and returned from <a href="#httprequestoptions-callback"><code>http.request()</code></a>. It
represents an <em>in-progress</em> request whose header has already been queued. The
header is still mutable using the <a href="#requestsetheadername-value"><code>setHeader(name, value)</code></a>,
<a href="#requestgetheadername"><code>getHeader(name)</code></a>, <a href="#requestremoveheadername"><code>removeHeader(name)</code></a> API. The actual header will
be sent along with the first data chunk or when calling <a href="#requestenddata-encoding-callback"><code>request.end()</code></a>.</p>
<p>To get the response, add a listener for <a href="#event-response"><code>'response'</code></a> to the request object.
<a href="#event-response"><code>'response'</code></a> will be emitted from the request object when the response
headers have been received. The <a href="#event-response"><code>'response'</code></a> event is executed with one
argument which is an instance of <a href="#class-httpincomingmessage"><code>http.IncomingMessage</code></a>.</p>
<p>During the <a href="#event-response"><code>'response'</code></a> event, one can add listeners to the
response object; particularly to listen for the <code>'data'</code> event.</p>
<p>If no <a href="#event-response"><code>'response'</code></a> handler is added, then the response will be
entirely discarded. However, if a <a href="#event-response"><code>'response'</code></a> event handler is added,
then the data from the response object <strong>must</strong> be consumed, either by
calling <code>response.read()</code> whenever there is a <code>'readable'</code> event, or
by adding a <code>'data'</code> handler, or by calling the <code>.resume()</code> method.
Until the data is consumed, the <code>'end'</code> event will not fire. Also, until
the data is read it will consume memory that can eventually lead to a
'process out of memory' error.</p>
<p>For backward compatibility, <code>res</code> will only emit <code>'error'</code> if there is an
<code>'error'</code> listener registered.</p>
<p>Set <code>Content-Length</code> header to limit the response body size.
If <a href="#responsestrictcontentlength"><code>response.strictContentLength</code></a> is set to <code>true</code>, mismatching the
<code>Content-Length</code> header value will result in an <code>Error</code> being thrown,
identified by <code>code:</code> <a href="errors.html#err_http_content_length_mismatch"><code>'ERR_HTTP_CONTENT_LENGTH_MISMATCH'</code></a>.</p>
<p><code>Content-Length</code> value should be in bytes, not characters. Use
<a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength()</code></a> to determine the length of the body in bytes.</p>
<h4>Event: <code>'abort'</code><span><a class="mark" href="#event-abort" id="event-abort">#</a></span><a aria-hidden="true" class="legacy" id="http_event_abort"></a></h4>
<div class="api_metadata">
<span>Added in: v1.4.1</span><span>Deprecated since: v17.0.0, v16.12.0</span>
</div>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated. Listen for the <code>'close'</code> event instead.</div><p></p>
<p>Emitted when the request has been aborted by the client. This event is only
emitted on the first call to <code>abort()</code>.</p>
<h4>Event: <code>'close'</code><span><a class="mark" href="#event-close" id="event-close">#</a></span><a aria-hidden="true" class="legacy" id="http_event_close"></a></h4>
<div class="api_metadata">
<span>Added in: v0.5.4</span>
</div>
<p>Indicates that the request is completed, or its underlying connection was
terminated prematurely (before the response completion).</p>
<h4>Event: <code>'connect'</code><span><a class="mark" href="#event-connect" id="event-connect">#</a></span><a aria-hidden="true" class="legacy" id="http_event_connect"></a></h4>
<div class="api_metadata">
<span>Added in: v0.7.0</span>
</div>
<ul>
<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
<li><code>head</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
<p>Emitted each time a server responds to a request with a <code>CONNECT</code> method. If
this event is not being listened for, clients receiving a <code>CONNECT</code> method will
have their connections closed.</p>
<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<p>A client and server pair demonstrating how to listen for the <code>'connect'</code> event:</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> { createServer, request } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:net'</span>;
<span class="hljs-keyword">import</span> { <span class="hljs-variable constant_">URL</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
<span class="hljs-comment">// Create an HTTP tunneling proxy</span>
<span class="hljs-keyword">const</span> proxy = <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_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
});
proxy.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span class="hljs-function">(<span class="hljs-params">req, clientSocket, head</span>) =></span> {
<span class="hljs-comment">// Connect to an origin server</span>
<span class="hljs-keyword">const</span> { port, hostname } = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">`http://<span class="hljs-subst">${req.url}</span>`</span>);
<span class="hljs-keyword">const</span> serverSocket = <span class="hljs-title function_">connect</span>(port || <span class="hljs-number">80</span>, hostname, <span class="hljs-function">() =></span> {
clientSocket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 200 Connection Established\r\n'</span> +
<span class="hljs-string">'Proxy-agent: Node.js-Proxy\r\n'</span> +
<span class="hljs-string">'\r\n'</span>);
serverSocket.<span class="hljs-title function_">write</span>(head);
serverSocket.<span class="hljs-title function_">pipe</span>(clientSocket);
clientSocket.<span class="hljs-title function_">pipe</span>(serverSocket);
});
});
<span class="hljs-comment">// Now that proxy is running</span>
proxy.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-string">'127.0.0.1'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// Make a request to a tunneling proxy</span>
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
<span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">method</span>: <span class="hljs-string">'CONNECT'</span>,
<span class="hljs-attr">path</span>: <span class="hljs-string">'www.google.com:80'</span>,
};
<span class="hljs-keyword">const</span> req = <span class="hljs-title function_">request</span>(options);
req.<span class="hljs-title function_">end</span>();
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span class="hljs-function">(<span class="hljs-params">res, socket, head</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got connected!'</span>);
<span class="hljs-comment">// Make a request over an HTTP tunnel</span>
socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'GET / HTTP/1.1\r\n'</span> +
<span class="hljs-string">'Host: www.google.com:80\r\n'</span> +
<span class="hljs-string">'Connection: close\r\n'</span> +
<span class="hljs-string">'\r\n'</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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk.<span class="hljs-title function_">toString</span>());
});
socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
proxy.<span class="hljs-title function_">close</span>();
});
});
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</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> { <span class="hljs-variable constant_">URL</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
<span class="hljs-comment">// Create an HTTP tunneling proxy</span>
<span class="hljs-keyword">const</span> proxy = http.<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_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
});
proxy.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span class="hljs-function">(<span class="hljs-params">req, clientSocket, head</span>) =></span> {
<span class="hljs-comment">// Connect to an origin server</span>
<span class="hljs-keyword">const</span> { port, hostname } = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">`http://<span class="hljs-subst">${req.url}</span>`</span>);
<span class="hljs-keyword">const</span> serverSocket = net.<span class="hljs-title function_">connect</span>(port || <span class="hljs-number">80</span>, hostname, <span class="hljs-function">() =></span> {
clientSocket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 200 Connection Established\r\n'</span> +
<span class="hljs-string">'Proxy-agent: Node.js-Proxy\r\n'</span> +
<span class="hljs-string">'\r\n'</span>);
serverSocket.<span class="hljs-title function_">write</span>(head);
serverSocket.<span class="hljs-title function_">pipe</span>(clientSocket);
clientSocket.<span class="hljs-title function_">pipe</span>(serverSocket);
});
});
<span class="hljs-comment">// Now that proxy is running</span>
proxy.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-string">'127.0.0.1'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// Make a request to a tunneling proxy</span>
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
<span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">method</span>: <span class="hljs-string">'CONNECT'</span>,
<span class="hljs-attr">path</span>: <span class="hljs-string">'www.google.com:80'</span>,
};
<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options);
req.<span class="hljs-title function_">end</span>();
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span class="hljs-function">(<span class="hljs-params">res, socket, head</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got connected!'</span>);
<span class="hljs-comment">// Make a request over an HTTP tunnel</span>
socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'GET / HTTP/1.1\r\n'</span> +
<span class="hljs-string">'Host: www.google.com:80\r\n'</span> +
<span class="hljs-string">'Connection: close\r\n'</span> +
<span class="hljs-string">'\r\n'</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> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk.<span class="hljs-title function_">toString</span>());
});
socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
proxy.<span class="hljs-title function_">close</span>();
});
});
});</code><button class="copy-button">copy</button></pre>
<h4>Event: <code>'continue'</code><span><a class="mark" href="#event-continue" id="event-continue">#</a></span><a aria-hidden="true" class="legacy" id="http_event_continue"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.2</span>
</div>
<p>Emitted when the server sends a '100 Continue' HTTP response, usually because
the request contained 'Expect: 100-continue'. This is an instruction that
the client should send the request body.</p>
<h4>Event: <code>'finish'</code><span><a class="mark" href="#event-finish" id="event-finish">#</a></span><a aria-hidden="true" class="legacy" id="http_event_finish"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.6</span>
</div>
<p>Emitted when the request 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 operating system for transmission over the network. It does not imply that
the server has received anything yet.</p>
<h4>Event: <code>'information'</code><span><a class="mark" href="#event-information" id="event-information">#</a></span><a aria-hidden="true" class="legacy" id="http_event_information"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>httpVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>httpVersionMajor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>httpVersionMinor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></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></li>
<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li>
</ul>
</li>
</ul>
<p>Emitted when the server sends a 1xx intermediate response (excluding 101
Upgrade). The listeners of this event will receive an object containing the
HTTP version, status code, status message, key-value headers object,
and array with the raw header names followed by their respective values.</p>
<pre class="with-36-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> { request } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
<span class="hljs-attr">path</span>: <span class="hljs-string">'/length_request'</span>,
};
<span class="hljs-comment">// Make a request</span>
<span class="hljs-keyword">const</span> req = <span class="hljs-title function_">request</span>(options);
req.<span class="hljs-title function_">end</span>();
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'information'</span>, <span class="hljs-function">(<span class="hljs-params">info</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Got information prior to main response: <span class="hljs-subst">${info.statusCode}</span>`</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
<span class="hljs-attr">path</span>: <span class="hljs-string">'/length_request'</span>,
};
<span class="hljs-comment">// Make a request</span>
<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options);
req.<span class="hljs-title function_">end</span>();
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'information'</span>, <span class="hljs-function">(<span class="hljs-params">info</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Got information prior to main response: <span class="hljs-subst">${info.statusCode}</span>`</span>);
});</code><button class="copy-button">copy</button></pre>
<p>101 Upgrade statuses do not fire this event due to their break from the
traditional HTTP request/response chain, such as web sockets, in-place TLS
upgrades, or HTTP 2.0. To be notified of 101 Upgrade notices, listen for the
<a href="#event-upgrade"><code>'upgrade'</code></a> event instead.</p>
<h4>Event: <code>'response'</code><span><a class="mark" href="#event-response" id="event-response">#</a></span><a aria-hidden="true" class="legacy" id="http_event_response"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.0</span>
</div>
<ul>
<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li>
</ul>
<p>Emitted when a response is received to this request. This event is emitted only
once.</p>
<h4>Event: <code>'socket'</code><span><a class="mark" href="#event-socket" id="event-socket">#</a></span><a aria-hidden="true" class="legacy" id="http_event_socket"></a></h4>
<div class="api_metadata">
<span>Added in: v0.5.3</span>
</div>
<ul>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<h4>Event: <code>'timeout'</code><span><a class="mark" href="#event-timeout" id="event-timeout">#</a></span><a aria-hidden="true" class="legacy" id="http_event_timeout"></a></h4>
<div class="api_metadata">
<span>Added in: v0.7.8</span>
</div>
<p>Emitted when the underlying socket times out from inactivity. This only notifies
that the socket has been idle. The request must be destroyed manually.</p>
<p>See also: <a href="#requestsettimeouttimeout-callback"><code>request.setTimeout()</code></a>.</p>
<h4>Event: <code>'upgrade'</code><span><a class="mark" href="#event-upgrade" id="event-upgrade">#</a></span><a aria-hidden="true" class="legacy" id="http_event_upgrade"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.94</span>
</div>
<ul>
<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
<li><code>head</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
<p>Emitted each time a server responds to a request with an upgrade. If this
event is not being listened for and the response status code is 101 Switching
Protocols, clients receiving an upgrade header will have their connections
closed.</p>
<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<p>A client server pair demonstrating how to listen for the <code>'upgrade'</code> event.</p>
<pre class="with-35-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> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>;
<span class="hljs-comment">// Create an HTTP server</span>
<span class="hljs-keyword">const</span> server = http.<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_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
});
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'upgrade'</span>, <span class="hljs-function">(<span class="hljs-params">req, socket, head</span>) =></span> {
socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'</span> +
<span class="hljs-string">'Upgrade: WebSocket\r\n'</span> +
<span class="hljs-string">'Connection: Upgrade\r\n'</span> +
<span class="hljs-string">'\r\n'</span>);
socket.<span class="hljs-title function_">pipe</span>(socket); <span class="hljs-comment">// echo back</span>
});
<span class="hljs-comment">// Now that server is running</span>
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-string">'127.0.0.1'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// make a request</span>
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
<span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">headers</span>: {
<span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Upgrade'</span>,
<span class="hljs-string">'Upgrade'</span>: <span class="hljs-string">'websocket'</span>,
},
};
<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options);
req.<span class="hljs-title function_">end</span>();
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'upgrade'</span>, <span class="hljs-function">(<span class="hljs-params">res, socket, upgradeHead</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got upgraded!'</span>);
socket.<span class="hljs-title function_">end</span>();
process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
});
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-comment">// Create an HTTP server</span>
<span class="hljs-keyword">const</span> server = http.<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_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
});
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'upgrade'</span>, <span class="hljs-function">(<span class="hljs-params">req, socket, head</span>) =></span> {
socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'</span> +
<span class="hljs-string">'Upgrade: WebSocket\r\n'</span> +
<span class="hljs-string">'Connection: Upgrade\r\n'</span> +
<span class="hljs-string">'\r\n'</span>);
socket.<span class="hljs-title function_">pipe</span>(socket); <span class="hljs-comment">// echo back</span>
});
<span class="hljs-comment">// Now that server is running</span>
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-string">'127.0.0.1'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-comment">// make a request</span>
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
<span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">headers</span>: {
<span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Upgrade'</span>,
<span class="hljs-string">'Upgrade'</span>: <span class="hljs-string">'websocket'</span>,
},
};
<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options);
req.<span class="hljs-title function_">end</span>();
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'upgrade'</span>, <span class="hljs-function">(<span class="hljs-params">res, socket, upgradeHead</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got upgraded!'</span>);
socket.<span class="hljs-title function_">end</span>();
process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
});
});</code><button class="copy-button">copy</button></pre>
<h4><code>request.abort()</code><span><a class="mark" href="#requestabort" id="requestabort">#</a></span><a aria-hidden="true" class="legacy" id="http_request_abort"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.8</span><span>Deprecated since: v14.1.0, v13.14.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="#requestdestroyerror"><code>request.destroy()</code></a> instead.</div><p></p>
<p>Marks the request as aborting. Calling this will cause remaining data
in the response to be dropped and the socket to be destroyed.</p>
<h4><code>request.aborted</code><span><a class="mark" href="#requestaborted" id="requestaborted">#</a></span><a aria-hidden="true" class="legacy" id="http_request_aborted"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v17.0.0, v16.12.0</td>
<td><p><span>Deprecated since: v17.0.0, v16.12.0</span></p></td></tr>
<tr><td>v11.0.0</td>
<td><p>The <code>aborted</code> property is no longer a timestamp number.</p></td></tr>
<tr><td>v0.11.14</td>
<td><p><span>Added in: v0.11.14</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated. Check <a href="#requestdestroyed"><code>request.destroyed</code></a> instead.</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>The <code>request.aborted</code> property will be <code>true</code> if the request has
been aborted.</p>
<h4><code>request.connection</code><span><a class="mark" href="#requestconnection" id="requestconnection">#</a></span><a aria-hidden="true" class="legacy" id="http_request_connection"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.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="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>See <a href="#requestsocket"><code>request.socket</code></a>.</p>
<h4><code>request.cork()</code><span><a class="mark" href="#requestcork" id="requestcork">#</a></span><a aria-hidden="true" class="legacy" id="http_request_cork"></a></h4>
<div class="api_metadata">
<span>Added in: v13.2.0, v12.16.0</span>
</div>
<p>See <a href="stream.html#writablecork"><code>writable.cork()</code></a>.</p>
<h4><code>request.end([data[, encoding]][, callback])</code><span><a class="mark" href="#requestenddata-encoding-callback" id="requestenddata-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_request_end_data_encoding_callback"></a></h4>
<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>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>This method now returns a reference to <code>ClientRequest</code>.</p></td></tr>
<tr><td>v0.1.90</td>
<td><p><span>Added in: v0.1.90</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>Finishes sending the request. If any parts of the body are
unsent, it will flush them to the stream. If the request is
chunked, this will send the terminating <code>'0\r\n\r\n'</code>.</p>
<p>If <code>data</code> is specified, it is equivalent to calling
<a href="#requestwritechunk-encoding-callback"><code>request.write(data, encoding)</code></a> followed by <code>request.end(callback)</code>.</p>
<p>If <code>callback</code> is specified, it will be called when the request stream
is finished.</p>
<h4><code>request.destroy([error])</code><span><a class="mark" href="#requestdestroyerror" id="requestdestroyerror">#</a></span><a aria-hidden="true" class="legacy" id="http_request_destroy_error"></a></h4>
<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</td>
<td><p>The function returns <code>this</code> for consistency with other Readable streams.</p></td></tr>
<tr><td>v0.3.0</td>
<td><p><span>Added in: v0.3.0</span></p></td></tr>
</tbody></table>
</details>
</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> Optional, an error to emit with <code>'error'</code> event.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li>
</ul>
<p>Destroy the request. Optionally emit an <code>'error'</code> event,
and emit a <code>'close'</code> event. Calling this will cause remaining data
in the response to be dropped and the socket to be destroyed.</p>
<p>See <a href="stream.html#writabledestroyerror"><code>writable.destroy()</code></a> for further details.</p>
<h5><code>request.destroyed</code><span><a class="mark" href="#requestdestroyed" id="requestdestroyed">#</a></span><a aria-hidden="true" class="legacy" id="http_request_destroyed"></a></h5>
<div class="api_metadata">
<span>Added in: v14.1.0, v13.14.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="#requestdestroyerror"><code>request.destroy()</code></a> has been called.</p>
<p>See <a href="stream.html#writabledestroyed"><code>writable.destroyed</code></a> for further details.</p>
<h4><code>request.finished</code><span><a class="mark" href="#requestfinished" id="requestfinished">#</a></span><a aria-hidden="true" class="legacy" id="http_request_finished"></a></h4>
<div class="api_metadata">
<span>Added in: v0.0.1</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="#requestwritableended"><code>request.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>The <code>request.finished</code> property will be <code>true</code> if <a href="#requestenddata-encoding-callback"><code>request.end()</code></a>
has been called. <code>request.end()</code> will automatically be called if the
request was initiated via <a href="#httpgetoptions-callback"><code>http.get()</code></a>.</p>
<h4><code>request.flushHeaders()</code><span><a class="mark" href="#requestflushheaders" id="requestflushheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_request_flushheaders"></a></h4>
<div class="api_metadata">
<span>Added in: v1.6.0</span>
</div>
<p>Flushes the request headers.</p>
<p>For efficiency reasons, Node.js normally buffers the request headers until
<code>request.end()</code> is called or the first chunk of request data is written. It
then tries to pack the request headers and data into a single TCP packet.</p>
<p>That's usually desired (it saves a TCP round-trip), but not when the first
data is not sent until possibly much later. <code>request.flushHeaders()</code> bypasses
the optimization and kickstarts the request.</p>
<h4><code>request.getHeader(name)</code><span><a class="mark" href="#requestgetheadername" id="requestgetheadername">#</a></span><a aria-hidden="true" class="legacy" id="http_request_getheader_name"></a></h4>
<div class="api_metadata">
<span>Added in: v1.6.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#Data_types" class="type"><any></a></li>
</ul>
<p>Reads out a header on the request. The name is case-insensitive.
The type of the return value depends on the arguments provided to
<a href="#requestsetheadername-value"><code>request.setHeader()</code></a>.</p>
<pre><code class="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'content-type'</span>, <span class="hljs-string">'text/html'</span>);
request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Length'</span>, <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(body));
request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Cookie'</span>, [<span class="hljs-string">'type=ninja'</span>, <span class="hljs-string">'language=javascript'</span>]);
<span class="hljs-keyword">const</span> contentType = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Type'</span>);
<span class="hljs-comment">// 'contentType' is 'text/html'</span>
<span class="hljs-keyword">const</span> contentLength = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Length'</span>);
<span class="hljs-comment">// 'contentLength' is of type number</span>
<span class="hljs-keyword">const</span> cookie = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Cookie'</span>);
<span class="hljs-comment">// 'cookie' is of type string[]</span></code> <button class="copy-button">copy</button></pre>
<h4><code>request.getHeaderNames()</code><span><a class="mark" href="#requestgetheadernames" id="requestgetheadernames">#</a></span><a aria-hidden="true" class="legacy" id="http_request_getheadernames"></a></h4>
<div class="api_metadata">
<span>Added in: v7.7.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">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Foo'</span>, <span class="hljs-string">'bar'</span>);
request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Cookie'</span>, [<span class="hljs-string">'foo=bar'</span>, <span class="hljs-string">'bar=baz'</span>]);
<span class="hljs-keyword">const</span> headerNames = request.<span class="hljs-title function_">getHeaderNames</span>();
<span class="hljs-comment">// headerNames === ['foo', 'cookie']</span></code> <button class="copy-button">copy</button></pre>
<h4><code>request.getHeaders()</code><span><a class="mark" href="#requestgetheaders" id="requestgetheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_request_getheaders"></a></h4>
<div class="api_metadata">
<span>Added in: v7.7.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>request.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">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Foo'</span>, <span class="hljs-string">'bar'</span>);
request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Cookie'</span>, [<span class="hljs-string">'foo=bar'</span>, <span class="hljs-string">'bar=baz'</span>]);
<span class="hljs-keyword">const</span> headers = request.<span class="hljs-title function_">getHeaders</span>();
<span class="hljs-comment">// headers === { foo: 'bar', 'cookie': ['foo=bar', 'bar=baz'] }</span></code> <button class="copy-button">copy</button></pre>
<h4><code>request.getRawHeaderNames()</code><span><a class="mark" href="#requestgetrawheadernames" id="requestgetrawheadernames">#</a></span><a aria-hidden="true" class="legacy" id="http_request_getrawheadernames"></a></h4>
<div class="api_metadata">
<span>Added in: v15.13.0, v14.17.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 raw
headers. Header names are returned with their exact casing being set.</p>
<pre><code class="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Foo'</span>, <span class="hljs-string">'bar'</span>);
request.<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 = request.<span class="hljs-title function_">getRawHeaderNames</span>();
<span class="hljs-comment">// headerNames === ['Foo', 'Set-Cookie']</span></code> <button class="copy-button">copy</button></pre>
<h4><code>request.hasHeader(name)</code><span><a class="mark" href="#requesthasheadername" id="requesthasheadername">#</a></span><a aria-hidden="true" class="legacy" id="http_request_hasheader_name"></a></h4>
<div class="api_metadata">
<span>Added in: v7.7.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 = request.<span class="hljs-title function_">hasHeader</span>(<span class="hljs-string">'content-type'</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>request.maxHeadersCount</code><span><a class="mark" href="#requestmaxheaderscount" id="requestmaxheaderscount">#</a></span><a aria-hidden="true" class="legacy" id="http_request_maxheaderscount"></a></h4>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>2000</code></li>
</ul>
<p>Limits maximum response headers count. If set to 0, no limit will be applied.</p>
<h4><code>request.path</code><span><a class="mark" href="#requestpath" id="requestpath">#</a></span><a aria-hidden="true" class="legacy" id="http_request_path"></a></h4>
<div class="api_metadata">
<span>Added in: v0.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> The request path.</li>
</ul>
<h4><code>request.method</code><span><a class="mark" href="#requestmethod" id="requestmethod">#</a></span><a aria-hidden="true" class="legacy" id="http_request_method"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.97</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request method.</li>
</ul>
<h4><code>request.host</code><span><a class="mark" href="#requesthost" id="requesthost">#</a></span><a aria-hidden="true" class="legacy" id="http_request_host"></a></h4>
<div class="api_metadata">
<span>Added in: v14.5.0, v12.19.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request host.</li>
</ul>
<h4><code>request.protocol</code><span><a class="mark" href="#requestprotocol" id="requestprotocol">#</a></span><a aria-hidden="true" class="legacy" id="http_request_protocol"></a></h4>
<div class="api_metadata">
<span>Added in: v14.5.0, v12.19.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request protocol.</li>
</ul>
<h4><code>request.removeHeader(name)</code><span><a class="mark" href="#requestremoveheadername" id="requestremoveheadername">#</a></span><a aria-hidden="true" class="legacy" id="http_request_removeheader_name"></a></h4>
<div class="api_metadata">
<span>Added in: v1.6.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's already defined into headers object.</p>
<pre><code class="language-js">request.<span class="hljs-title function_">removeHeader</span>(<span class="hljs-string">'Content-Type'</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>request.reusedSocket</code><span><a class="mark" href="#requestreusedsocket" id="requestreusedsocket">#</a></span><a aria-hidden="true" class="legacy" id="http_request_reusedsocket"></a></h4>
<div class="api_metadata">
<span>Added in: v13.0.0, v12.16.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the request is send through a reused socket.</li>
</ul>
<p>When sending request through a keep-alive enabled agent, the underlying socket
might be reused. But if server closes connection at unfortunate time, client
may run into a 'ECONNRESET' error.</p>
<pre class="with-34-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> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-comment">// Server has a 5 seconds keep-alive timeout by default</span>
http
.<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_">write</span>(<span class="hljs-string">'hello\n'</span>);
res.<span class="hljs-title function_">end</span>();
})
.<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);
<span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
<span class="hljs-comment">// Adapting a keep-alive agent</span>
http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:3000'</span>, { agent }, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
<span class="hljs-comment">// Do nothing</span>
});
});
}, <span class="hljs-number">5000</span>); <span class="hljs-comment">// Sending request on 5s interval so it's easy to hit idle timeout</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-comment">// Server has a 5 seconds keep-alive timeout by default</span>
http
.<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_">write</span>(<span class="hljs-string">'hello\n'</span>);
res.<span class="hljs-title function_">end</span>();
})
.<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);
<span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
<span class="hljs-comment">// Adapting a keep-alive agent</span>
http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:3000'</span>, { agent }, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
<span class="hljs-comment">// Do nothing</span>
});
});
}, <span class="hljs-number">5000</span>); <span class="hljs-comment">// Sending request on 5s interval so it's easy to hit idle timeout</span></code><button class="copy-button">copy</button></pre>
<p>By marking a request whether it reused socket or not, we can do
automatic error retry base on it.</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> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">const</span> agent = <span class="hljs-keyword">new</span> http.<span class="hljs-title class_">Agent</span>({ <span class="hljs-attr">keepAlive</span>: <span class="hljs-literal">true</span> });
<span class="hljs-keyword">function</span> <span class="hljs-title function_">retriableRequest</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">const</span> req = http
.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:3000'</span>, { agent }, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
<span class="hljs-comment">// ...</span>
})
.<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-comment">// Check if retry is needed</span>
<span class="hljs-keyword">if</span> (req.<span class="hljs-property">reusedSocket</span> && err.<span class="hljs-property">code</span> === <span class="hljs-string">'ECONNRESET'</span>) {
<span class="hljs-title function_">retriableRequest</span>();
}
});
}
<span class="hljs-title function_">retriableRequest</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">const</span> agent = <span class="hljs-keyword">new</span> http.<span class="hljs-title class_">Agent</span>({ <span class="hljs-attr">keepAlive</span>: <span class="hljs-literal">true</span> });
<span class="hljs-keyword">function</span> <span class="hljs-title function_">retriableRequest</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">const</span> req = http
.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:3000'</span>, { agent }, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
<span class="hljs-comment">// ...</span>
})
.<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-comment">// Check if retry is needed</span>
<span class="hljs-keyword">if</span> (req.<span class="hljs-property">reusedSocket</span> && err.<span class="hljs-property">code</span> === <span class="hljs-string">'ECONNRESET'</span>) {
<span class="hljs-title function_">retriableRequest</span>();
}
});
}
<span class="hljs-title function_">retriableRequest</span>();</code><button class="copy-button">copy</button></pre>
<h4><code>request.setHeader(name, value)</code><span><a class="mark" href="#requestsetheadername-value" id="requestsetheadername-value">#</a></span><a aria-hidden="true" class="legacy" id="http_request_setheader_name_value"></a></h4>
<div class="api_metadata">
<span>Added in: v1.6.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#Data_types" class="type"><any></a></li>
</ul>
<p>Sets a single header value for headers object. 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. Non-string values will be
stored without modification. Therefore, <a href="#requestgetheadername"><code>request.getHeader()</code></a> may return
non-string values. However, the non-string values will be converted to strings
for network transmission.</p>
<pre><code class="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'application/json'</span>);</code> <button class="copy-button">copy</button></pre>
<p>or</p>
<pre><code class="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'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>When the value is a string an exception will be thrown if it contains
characters outside the <code>latin1</code> encoding.</p>
<p>If you need to pass UTF-8 characters in the value please encode the value
using the <a href="https://www.rfc-editor.org/rfc/rfc8187.txt">RFC 8187</a> standard.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> filename = <span class="hljs-string">'Rock 🎵.txt'</span>;
request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Disposition'</span>, <span class="hljs-string">`attachment; filename*=utf-8''<span class="hljs-subst">${<span class="hljs-built_in">encodeURIComponent</span>(filename)}</span>`</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>request.setNoDelay([noDelay])</code><span><a class="mark" href="#requestsetnodelaynodelay" id="requestsetnodelaynodelay">#</a></span><a aria-hidden="true" class="legacy" id="http_request_setnodelay_nodelay"></a></h4>
<div class="api_metadata">
<span>Added in: v0.5.9</span>
</div>
<ul>
<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Once a socket is assigned to this request and is connected
<a href="net.html#socketsetnodelaynodelay"><code>socket.setNoDelay()</code></a> will be called.</p>
<h4><code>request.setSocketKeepAlive([enable][, initialDelay])</code><span><a class="mark" href="#requestsetsocketkeepaliveenable-initialdelay" id="requestsetsocketkeepaliveenable-initialdelay">#</a></span><a aria-hidden="true" class="legacy" id="http_request_setsocketkeepalive_enable_initialdelay"></a></h4>
<div class="api_metadata">
<span>Added in: v0.5.9</span>
</div>
<ul>
<li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
<li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>Once a socket is assigned to this request and is connected
<a href="net.html#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive()</code></a> will be called.</p>
<h4><code>request.setTimeout(timeout[, callback])</code><span><a class="mark" href="#requestsettimeouttimeout-callback" id="requestsettimeouttimeout-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_request_settimeout_timeout_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v9.0.0</td>
<td><p>Consistently set socket timeout only when the socket connects.</p></td></tr>
<tr><td>v0.5.9</td>
<td><p><span>Added in: v0.5.9</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Milliseconds before a request times out.</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Optional function to be called when a timeout occurs.
Same as binding to the <code>'timeout'</code> event.</li>
<li>Returns: <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li>
</ul>
<p>Once a socket is assigned to this request and is connected
<a href="net.html#socketsettimeouttimeout-callback"><code>socket.setTimeout()</code></a> will be called.</p>
<h4><code>request.socket</code><span><a class="mark" href="#requestsocket" id="requestsocket">#</a></span><a aria-hidden="true" class="legacy" id="http_request_socket"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div>
<ul>
<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>Reference to the underlying socket. Usually users will not want to access
this property. In particular, the socket will not emit <code>'readable'</code> events
because of how the protocol parser attaches to the socket.</p>
<pre class="with-34-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> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">host</span>: <span class="hljs-string">'www.google.com'</span>,
};
<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">get</span>(options);
req.<span class="hljs-title function_">end</span>();
req.<span class="hljs-title function_">once</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
<span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localAddress</span>;
<span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localPort</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</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-comment">// Consume response object</span>
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">host</span>: <span class="hljs-string">'www.google.com'</span>,
};
<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">get</span>(options);
req.<span class="hljs-title function_">end</span>();
req.<span class="hljs-title function_">once</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
<span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localAddress</span>;
<span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localPort</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</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-comment">// Consume response object</span>
});</code><button class="copy-button">copy</button></pre>
<p>This property is guaranteed to be an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specified a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<h4><code>request.uncork()</code><span><a class="mark" href="#requestuncork" id="requestuncork">#</a></span><a aria-hidden="true" class="legacy" id="http_request_uncork"></a></h4>
<div class="api_metadata">
<span>Added in: v13.2.0, v12.16.0</span>
</div>
<p>See <a href="stream.html#writableuncork"><code>writable.uncork()</code></a>.</p>
<h4><code>request.writableEnded</code><span><a class="mark" href="#requestwritableended" id="requestwritableended">#</a></span><a aria-hidden="true" class="legacy" id="http_request_writableended"></a></h4>
<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="#requestenddata-encoding-callback"><code>request.end()</code></a> has been called. This property
does not indicate whether the data has been flushed, for this use
<a href="#requestwritablefinished"><code>request.writableFinished</code></a> instead.</p>
<h4><code>request.writableFinished</code><span><a class="mark" href="#requestwritablefinished" id="requestwritablefinished">#</a></span><a aria-hidden="true" class="legacy" id="http_request_writablefinished"></a></h4>
<div class="api_metadata">
<span>Added in: v12.7.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> if all data has been flushed to the underlying system, immediately
before the <a href="#event-finish"><code>'finish'</code></a> event is emitted.</p>
<h4><code>request.write(chunk[, encoding][, callback])</code><span><a class="mark" href="#requestwritechunk-encoding-callback" id="requestwritechunk-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_request_write_chunk_encoding_callback"></a></h4>
<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>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v0.1.29</td>
<td><p><span>Added in: v0.1.29</span></p></td></tr>
</tbody></table>
</details>
</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>Sends a chunk of the body. This method can be called multiple times. If no
<code>Content-Length</code> is set, data will automatically be encoded in HTTP Chunked
transfer encoding, so that server knows when the data ends. The
<code>Transfer-Encoding: chunked</code> header is added. Calling <a href="#requestenddata-encoding-callback"><code>request.end()</code></a>
is necessary to finish sending the request.</p>
<p>The <code>encoding</code> argument is optional and only applies when <code>chunk</code> is a string.
Defaults to <code>'utf8'</code>.</p>
<p>The <code>callback</code> argument is optional and will be called when this chunk of data
is flushed, but only if the chunk is non-empty.</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>
<p>When <code>write</code> function is called with empty string or buffer, it does
nothing and waits for more input.</p>
</section><section><h3>Class: <code>http.Server</code><span><a class="mark" href="#class-httpserver" id="class-httpserver">#</a></span><a aria-hidden="true" class="legacy" id="http_class_http_server"></a></h3>
<div class="api_metadata">
<span>Added in: v0.1.17</span>
</div>
<ul>
<li>Extends: <a href="net.html#class-netserver" class="type"><net.Server></a></li>
</ul>
<h4>Event: <code>'checkContinue'</code><span><a class="mark" href="#event-checkcontinue" id="event-checkcontinue">#</a></span><a aria-hidden="true" class="legacy" id="http_event_checkcontinue"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div>
<ul>
<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li>
<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li>
</ul>
<p>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 <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>
<h4>Event: <code>'checkExpectation'</code><span><a class="mark" href="#event-checkexpectation" id="event-checkexpectation">#</a></span><a aria-hidden="true" class="legacy" id="http_event_checkexpectation"></a></h4>
<div class="api_metadata">
<span>Added in: v5.5.0</span>
</div>
<ul>
<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li>
<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li>
</ul>
<p>Emitted each time a request with an HTTP <code>Expect</code> header is received, where the
value is not <code>100-continue</code>. If this event is not listened for, the server will
automatically respond with a <code>417 Expectation Failed</code> as appropriate.</p>
<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will
not be emitted.</p>
<h4>Event: <code>'clientError'</code><span><a class="mark" href="#event-clienterror" id="event-clienterror">#</a></span><a aria-hidden="true" class="legacy" id="http_event_clienterror"></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.0.0</td>
<td><p>The default behavior will return a 431 Request Header Fields Too Large if a HPE_HEADER_OVERFLOW error occurs.</p></td></tr>
<tr><td>v9.4.0</td>
<td><p>The <code>rawPacket</code> is the current buffer that just parsed. Adding this buffer to the error object of <code>'clientError'</code> event is to make it possible that developers can log the broken packet.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The default action of calling <code>.destroy()</code> on the <code>socket</code> will no longer take place if there are listeners attached for <code>'clientError'</code>.</p></td></tr>
<tr><td>v0.1.94</td>
<td><p><span>Added in: v0.1.94</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>If a client connection emits an <code>'error'</code> event, it will be forwarded here.
Listener of this event is responsible for closing/destroying the underlying
socket. For example, one may wish to more gracefully close the socket with a
custom HTTP response instead of abruptly severing the connection. The socket
<strong>must be closed or destroyed</strong> before the listener ends.</p>
<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<p>Default behavior is to try close the socket with a HTTP '400 Bad Request',
or a HTTP '431 Request Header Fields Too Large' in the case of a
<a href="errors.html#hpe_header_overflow"><code>HPE_HEADER_OVERFLOW</code></a> error. If the socket is not writable or headers
of the current attached <a href="#class-httpserverresponse"><code>http.ServerResponse</code></a> has been sent, it is
immediately destroyed.</p>
<p><code>socket</code> is the <a href="net.html#class-netsocket"><code>net.Socket</code></a> object that the error originated from.</p>
<pre class="with-34-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> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">const</span> server = http.<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_">end</span>();
});
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'clientError'</span>, <span class="hljs-function">(<span class="hljs-params">err, socket</span>) =></span> {
socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</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> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">const</span> server = http.<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_">end</span>();
});
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'clientError'</span>, <span class="hljs-function">(<span class="hljs-params">err, socket</span>) =></span> {
socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</span>);
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><button class="copy-button">copy</button></pre>
<p>When the <code>'clientError'</code> event occurs, there is no <code>request</code> or <code>response</code>
object, so any HTTP response sent, including response headers and payload,
<em>must</em> be written directly to the <code>socket</code> object. Care must be taken to
ensure the response is a properly formatted HTTP response message.</p>
<p><code>err</code> is an instance of <code>Error</code> with two extra columns:</p>
<ul>
<li><code>bytesParsed</code>: the bytes count of request packet that Node.js may have parsed
correctly;</li>
<li><code>rawPacket</code>: the raw packet of current request.</li>
</ul>
<p>In some cases, the client has already received the response and/or the socket
has already been destroyed, like in case of <code>ECONNRESET</code> errors. Before
trying to send data to the socket, it is better to check that it is still
writable.</p>
<pre><code class="language-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'clientError'</span>, <span class="hljs-function">(<span class="hljs-params">err, socket</span>) =></span> {
<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ECONNRESET'</span> || !socket.<span class="hljs-property">writable</span>) {
<span class="hljs-keyword">return</span>;
}
socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4>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="http_event_close_1"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.4</span>
</div>
<p>Emitted when the server closes.</p>
<h4>Event: <code>'connect'</code><span><a class="mark" href="#event-connect_1" id="event-connect_1">#</a></span><a aria-hidden="true" class="legacy" id="http_event_connect_1"></a></h4>
<div class="api_metadata">
<span>Added in: v0.7.0</span>
</div>
<ul>
<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Arguments for the HTTP request, as it is in
the <a href="#event-request"><code>'request'</code></a> event</li>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a> Network socket between the server and client</li>
<li><code>head</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> The first packet of the tunneling stream (may be empty)</li>
</ul>
<p>Emitted each time a client requests an HTTP <code>CONNECT</code> method. If this event is
not listened for, then clients requesting a <code>CONNECT</code> method will have their
connections closed.</p>
<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<p>After this event is emitted, the request's socket will not have a <code>'data'</code>
event listener, meaning it will need to be bound in order to handle data
sent to the server on that socket.</p>
<h4>Event: <code>'connection'</code><span><a class="mark" href="#event-connection" id="event-connection">#</a></span><a aria-hidden="true" class="legacy" id="http_event_connection"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.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. In particular, the socket will not emit <code>'readable'</code> events
because of how the protocol parser attaches to the socket. The <code>socket</code> can
also be accessed at <code>request.socket</code>.</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>
<p>If <code>socket.setTimeout()</code> is called here, the timeout will be replaced with
<code>server.keepAliveTimeout</code> when the socket has served a request (if
<code>server.keepAliveTimeout</code> is non-zero).</p>
<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<h4>Event: <code>'dropRequest'</code><span><a class="mark" href="#event-droprequest" id="event-droprequest">#</a></span><a aria-hidden="true" class="legacy" id="http_event_droprequest"></a></h4>
<div class="api_metadata">
<span>Added in: v18.7.0, v16.17.0</span>
</div>
<ul>
<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Arguments for the HTTP request, as it is in
the <a href="#event-request"><code>'request'</code></a> event</li>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a> Network socket between the server and client</li>
</ul>
<p>When the number of requests on a socket reaches the threshold of
<code>server.maxRequestsPerSocket</code>, the server will drop new requests
and emit <code>'dropRequest'</code> event instead, then send <code>503</code> to client.</p>
<h4>Event: <code>'request'</code><span><a class="mark" href="#event-request" id="event-request">#</a></span><a aria-hidden="true" class="legacy" id="http_event_request"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.0</span>
</div>
<ul>
<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li>
<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li>
</ul>
<p>Emitted each time there is a request. There may be multiple requests
per connection (in the case of HTTP Keep-Alive connections).</p>
<h4>Event: <code>'upgrade'</code><span><a class="mark" href="#event-upgrade_1" id="event-upgrade_1">#</a></span><a aria-hidden="true" class="legacy" id="http_event_upgrade_1"></a></h4>
<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>Not listening to this event no longer causes the socket to be destroyed if a client sends an Upgrade header.</p></td></tr>
<tr><td>v0.1.94</td>
<td><p><span>Added in: v0.1.94</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Arguments for the HTTP request, as it is in
the <a href="#event-request"><code>'request'</code></a> event</li>
<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a> Network socket between the server and client</li>
<li><code>head</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> The first packet of the upgraded stream (may be empty)</li>
</ul>
<p>Emitted each time a client requests an HTTP upgrade. Listening to this event
is optional and clients cannot insist on a protocol change.</p>
<p>After this event is emitted, the request's socket will not have a <code>'data'</code>
event listener, meaning it will need to be bound in order to handle data
sent to the server on that socket.</p>
<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<h4><code>server.close([callback])</code><span><a class="mark" href="#serverclosecallback" id="serverclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="http_server_close_callback"></a></h4>
<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>The method closes idle connections before returning.</p></td></tr>
<tr><td>v0.1.90</td>
<td><p><span>Added in: v0.1.90</span></p></td></tr>
</tbody></table>
</details>
</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 accepting new connections and closes all connections
connected to this server which are not sending a request or waiting for
a response.
See <a href="net.html#serverclosecallback"><code>net.Server.close()</code></a>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>({ <span class="hljs-attr">keepAliveTimeout</span>: <span class="hljs-number">60000</span> }, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></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">'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>({
<span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>,
}));
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
<span class="hljs-comment">// Close the server after 10 seconds</span>
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
server.<span class="hljs-title function_">close</span>(<span class="hljs-function">() =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server on port 8000 closed successfully'</span>);
});
}, <span class="hljs-number">10000</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>server.closeAllConnections()</code><span><a class="mark" href="#servercloseallconnections" id="servercloseallconnections">#</a></span><a aria-hidden="true" class="legacy" id="http_server_closeallconnections"></a></h4>
<div class="api_metadata">
<span>Added in: v18.2.0</span>
</div>
<p>Closes all established HTTP(S) connections connected to this server, including
active connections connected to this server which are sending a request or
waiting for a response. This does <em>not</em> destroy sockets upgraded to a different
protocol, such as WebSocket or HTTP/2.</p>
<blockquote>
<p>This is a forceful way of closing all connections and should be used with
caution. Whenever using this in conjunction with <code>server.close</code>, calling this
<em>after</em> <code>server.close</code> is recommended as to avoid race conditions where new
connections are created between a call to this and a call to <code>server.close</code>.</p>
</blockquote>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>({ <span class="hljs-attr">keepAliveTimeout</span>: <span class="hljs-number">60000</span> }, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></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">'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>({
<span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>,
}));
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
<span class="hljs-comment">// Close the server after 10 seconds</span>
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
server.<span class="hljs-title function_">close</span>(<span class="hljs-function">() =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server on port 8000 closed successfully'</span>);
});
<span class="hljs-comment">// Closes all connections, ensuring the server closes successfully</span>
server.<span class="hljs-title function_">closeAllConnections</span>();
}, <span class="hljs-number">10000</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>server.closeIdleConnections()</code><span><a class="mark" href="#servercloseidleconnections" id="servercloseidleconnections">#</a></span><a aria-hidden="true" class="legacy" id="http_server_closeidleconnections"></a></h4>
<div class="api_metadata">
<span>Added in: v18.2.0</span>
</div>
<p>Closes all connections connected to this server which are not sending a request
or waiting for a response.</p>
<blockquote>
<p>Starting with Node.js 19.0.0, there's no need for calling this method in
conjunction with <code>server.close</code> to reap <code>keep-alive</code> connections. Using it
won't cause any harm though, and it can be useful to ensure backwards
compatibility for libraries and applications that need to support versions
older than 19.0.0. Whenever using this in conjunction with <code>server.close</code>,
calling this <em>after</em> <code>server.close</code> is recommended as to avoid race
conditions where new connections are created between a call to this and a
call to <code>server.close</code>.</p>
</blockquote>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>({ <span class="hljs-attr">keepAliveTimeout</span>: <span class="hljs-number">60000</span> }, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></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">'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>({
<span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>,
}));
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
<span class="hljs-comment">// Close the server after 10 seconds</span>
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
server.<span class="hljs-title function_">close</span>(<span class="hljs-function">() =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server on port 8000 closed successfully'</span>);
});
<span class="hljs-comment">// Closes idle connections, such as keep-alive connections. Server will close</span>
<span class="hljs-comment">// once remaining active connections are terminated</span>
server.<span class="hljs-title function_">closeIdleConnections</span>();
}, <span class="hljs-number">10000</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>server.headersTimeout</code><span><a class="mark" href="#serverheaderstimeout" id="serverheaderstimeout">#</a></span><a aria-hidden="true" class="legacy" id="http_server_headerstimeout"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.4.0, v18.14.0</td>
<td><p>The default is now set to the minimum between 60000 (60 seconds) or <code>requestTimeout</code>.</p></td></tr>
<tr><td>v11.3.0, v10.14.0</td>
<td><p><span>Added in: v11.3.0, v10.14.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> <strong>Default:</strong> The minimum between <a href="#serverrequesttimeout"><code>server.requestTimeout</code></a> or <code>60000</code>.</li>
</ul>
<p>Limit the amount of time the parser will wait to receive the complete HTTP
headers.</p>
<p>If the timeout expires, the server responds with status 408 without
forwarding the request to the request listener and then closes the connection.</p>
<p>It must be set to a non-zero value (e.g. 120 seconds) to protect against
potential Denial-of-Service attacks in case the server is deployed without a
reverse proxy in front.</p>
<h4><code>server.listen()</code><span><a class="mark" href="#serverlisten" id="serverlisten">#</a></span><a aria-hidden="true" class="legacy" id="http_server_listen"></a></h4>
<p>Starts the HTTP server listening for connections.
This method is identical to <a href="net.html#serverlisten"><code>server.listen()</code></a> from <a href="net.html#class-netserver"><code>net.Server</code></a>.</p>
<h4><code>server.listening</code><span><a class="mark" href="#serverlistening" id="serverlistening">#</a></span><a aria-hidden="true" class="legacy" id="http_server_listening"></a></h4>
<div class="api_metadata">
<span>Added in: v5.7.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether or not the server is listening for connections.</li>
</ul>
<h4><code>server.maxHeadersCount</code><span><a class="mark" href="#servermaxheaderscount" id="servermaxheaderscount">#</a></span><a aria-hidden="true" class="legacy" id="http_server_maxheaderscount"></a></h4>
<div class="api_metadata">
<span>Added in: v0.7.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>2000</code></li>
</ul>
<p>Limits maximum incoming headers count. If set to 0, no limit will be applied.</p>
<h4><code>server.requestTimeout</code><span><a class="mark" href="#serverrequesttimeout" id="serverrequesttimeout">#</a></span><a aria-hidden="true" class="legacy" id="http_server_requesttimeout"></a></h4>
<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>The default request timeout changed from no timeout to 300s (5 minutes).</p></td></tr>
<tr><td>v14.11.0</td>
<td><p><span>Added in: v14.11.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> <strong>Default:</strong> <code>300000</code></li>
</ul>
<p>Sets the timeout value in milliseconds for receiving the entire request from
the client.</p>
<p>If the timeout expires, the server responds with status 408 without
forwarding the request to the request listener and then closes the connection.</p>
<p>It must be set to a non-zero value (e.g. 120 seconds) to protect against
potential Denial-of-Service attacks in case the server is deployed without a
reverse proxy in front.</p>
<h4><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="http_server_settimeout_msecs_callback"></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 default timeout changed from 120s to 0 (no timeout).</p></td></tr>
<tr><td>v0.9.12</td>
<td><p><span>Added in: v0.9.12</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="http.html#class-httpserver" class="type"><http.Server></a></li>
</ul>
<p>Sets the timeout value for sockets, and emits a <code>'timeout'</code> event on
the Server object, passing the socket as an argument, if a timeout
occurs.</p>
<p>If there is a <code>'timeout'</code> event listener on the Server object, then it
will be called with the timed-out socket as an argument.</p>
<p>By default, the Server does not timeout sockets. However, if a callback
is assigned to the Server's <code>'timeout'</code> event, timeouts must be handled
explicitly.</p>
<h4><code>server.maxRequestsPerSocket</code><span><a class="mark" href="#servermaxrequestspersocket" id="servermaxrequestspersocket">#</a></span><a aria-hidden="true" class="legacy" id="http_server_maxrequestspersocket"></a></h4>
<div class="api_metadata">
<span>Added in: v16.10.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Requests per socket. <strong>Default:</strong> 0 (no limit)</li>
</ul>
<p>The maximum number of requests socket can handle
before closing keep alive connection.</p>
<p>A value of <code>0</code> will disable the limit.</p>
<p>When the limit is reached it will set the <code>Connection</code> header value to <code>close</code>,
but will not actually close the connection, subsequent requests sent
after the limit is reached will get <code>503 Service Unavailable</code> as a response.</p>
<h4><code>server.timeout</code><span><a class="mark" href="#servertimeout" id="servertimeout">#</a></span><a aria-hidden="true" class="legacy" id="http_server_timeout"></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 default timeout changed from 120s to 0 (no timeout).</p></td></tr>
<tr><td>v0.9.12</td>
<td><p><span>Added in: v0.9.12</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>
<h4><code>server.keepAliveTimeout</code><span><a class="mark" href="#serverkeepalivetimeout" id="serverkeepalivetimeout">#</a></span><a aria-hidden="true" class="legacy" id="http_server_keepalivetimeout"></a></h4>
<div class="api_metadata">
<span>Added in: v8.0.0</span>
</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> <code>5000</code> (5 seconds).</li>
</ul>
<p>The number of milliseconds of inactivity a server needs to wait for additional
incoming data, after it has finished writing the last response, before a socket
will be destroyed. If the server receives new data before the keep-alive
timeout has fired, it will reset the regular inactivity timeout, i.e.,
<a href="#servertimeout"><code>server.timeout</code></a>.</p>
<p>A value of <code>0</code> will disable the keep-alive timeout behavior on incoming
connections.
A value of <code>0</code> makes the http server behave similarly to Node.js versions prior
to 8.0.0, which did not have a keep-alive timeout.</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>
<h4><code>server[Symbol.asyncDispose]()</code><span><a class="mark" href="#serversymbolasyncdispose" id="serversymbolasyncdispose">#</a></span><a aria-hidden="true" class="legacy" id="http_server_symbol_asyncdispose"></a></h4>
<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>
</section><section><h3>Class: <code>http.ServerResponse</code><span><a class="mark" href="#class-httpserverresponse" id="class-httpserverresponse">#</a></span><a aria-hidden="true" class="legacy" id="http_class_http_serverresponse"></a></h3>
<div class="api_metadata">
<span>Added in: v0.1.17</span>
</div>
<ul>
<li>Extends: <a href="http.html#class-httpoutgoingmessage" class="type"><http.OutgoingMessage></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>
<h4>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="http_event_close_2"></a></h4>
<div class="api_metadata">
<span>Added in: v0.6.7</span>
</div>
<p>Indicates that the response is completed, or its underlying connection was
terminated prematurely (before the response completion).</p>
<h4>Event: <code>'finish'</code><span><a class="mark" href="#event-finish_1" id="event-finish_1">#</a></span><a aria-hidden="true" class="legacy" id="http_event_finish_1"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.6</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 operating system for transmission over the network. It
does not imply that the client has received anything yet.</p>
<h4><code>response.addTrailers(headers)</code><span><a class="mark" href="#responseaddtrailersheaders" id="responseaddtrailersheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_response_addtrailers_headers"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.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>Trailers will <strong>only</strong> be emitted if chunked encoding is used for the
response; if it is not (e.g. if the request was HTTP/1.0), they will
be silently discarded.</p>
<p>HTTP requires the <code>Trailer</code> header to be sent in order to
emit trailers, with a list of the header fields in its value. E.g.,</p>
<pre><code class="language-js">response.<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'</span>,
<span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> });
response.<span class="hljs-title function_">write</span>(fileData);
response.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-MD5'</span>: <span class="hljs-string">'7895bf4b8828b55ceaf47747b4bca667'</span> });
response.<span class="hljs-title function_">end</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>
<h4><code>response.connection</code><span><a class="mark" href="#responseconnection" id="responseconnection">#</a></span><a aria-hidden="true" class="legacy" id="http_response_connection"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.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="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>See <a href="#responsesocket"><code>response.socket</code></a>.</p>
<h4><code>response.cork()</code><span><a class="mark" href="#responsecork" id="responsecork">#</a></span><a aria-hidden="true" class="legacy" id="http_response_cork"></a></h4>
<div class="api_metadata">
<span>Added in: v13.2.0, v12.16.0</span>
</div>
<p>See <a href="stream.html#writablecork"><code>writable.cork()</code></a>.</p>
<h4><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="http_response_end_data_encoding_callback"></a></h4>
<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>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>This method now returns a reference to <code>ServerResponse</code>.</p></td></tr>
<tr><td>v0.1.90</td>
<td><p><span>Added in: v0.1.90</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 similar in effect to calling
<a href="#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>
<h4><code>response.finished</code><span><a class="mark" href="#responsefinished" id="responsefinished">#</a></span><a aria-hidden="true" class="legacy" id="http_response_finished"></a></h4>
<div class="api_metadata">
<span>Added in: v0.0.2</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>The <code>response.finished</code> property will be <code>true</code> if <a href="#responseenddata-encoding-callback"><code>response.end()</code></a>
has been called.</p>
<h4><code>response.flushHeaders()</code><span><a class="mark" href="#responseflushheaders" id="responseflushheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_response_flushheaders"></a></h4>
<div class="api_metadata">
<span>Added in: v1.6.0</span>
</div>
<p>Flushes the response headers. See also: <a href="#requestflushheaders"><code>request.flushHeaders()</code></a>.</p>
<h4><code>response.getHeader(name)</code><span><a class="mark" href="#responsegetheadername" id="responsegetheadername">#</a></span><a aria-hidden="true" class="legacy" id="http_response_getheader_name"></a></h4>
<div class="api_metadata">
<span>Added in: v0.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#Data_types" class="type"><any></a></li>
</ul>
<p>Reads out a header that's already been queued but not sent to the client.
The name is case-insensitive. The type of the return value depends
on the arguments provided to <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>.</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'</span>);
response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Length'</span>, <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(body));
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>]);
<span class="hljs-keyword">const</span> contentType = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'content-type'</span>);
<span class="hljs-comment">// contentType is 'text/html'</span>
<span class="hljs-keyword">const</span> contentLength = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Length'</span>);
<span class="hljs-comment">// contentLength is of type number</span>
<span class="hljs-keyword">const</span> setCookie = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'set-cookie'</span>);
<span class="hljs-comment">// setCookie is of type string[]</span></code> <button class="copy-button">copy</button></pre>
<h4><code>response.getHeaderNames()</code><span><a class="mark" href="#responsegetheadernames" id="responsegetheadernames">#</a></span><a aria-hidden="true" class="legacy" id="http_response_getheadernames"></a></h4>
<div class="api_metadata">
<span>Added in: v7.7.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>
<h4><code>response.getHeaders()</code><span><a class="mark" href="#responsegetheaders" id="responsegetheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_response_getheaders"></a></h4>
<div class="api_metadata">
<span>Added in: v7.7.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>
<h4><code>response.hasHeader(name)</code><span><a class="mark" href="#responsehasheadername" id="responsehasheadername">#</a></span><a aria-hidden="true" class="legacy" id="http_response_hasheader_name"></a></h4>
<div class="api_metadata">
<span>Added in: v7.7.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>
<h4><code>response.headersSent</code><span><a class="mark" href="#responseheaderssent" id="responseheaderssent">#</a></span><a aria-hidden="true" class="legacy" id="http_response_headerssent"></a></h4>
<div class="api_metadata">
<span>Added in: v0.9.3</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>Boolean (read-only). True if headers were sent, false otherwise.</p>
<h4><code>response.removeHeader(name)</code><span><a class="mark" href="#responseremoveheadername" id="responseremoveheadername">#</a></span><a aria-hidden="true" class="legacy" id="http_response_removeheader_name"></a></h4>
<div class="api_metadata">
<span>Added in: v0.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's 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>
<h4><code>response.req</code><span><a class="mark" href="#responsereq" id="responsereq">#</a></span><a aria-hidden="true" class="legacy" id="http_response_req"></a></h4>
<div class="api_metadata">
<span>Added in: v15.7.0</span>
</div>
<ul>
<li><a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li>
</ul>
<p>A reference to the original HTTP <code>request</code> object.</p>
<h4><code>response.sendDate</code><span><a class="mark" href="#responsesenddate" id="responsesenddate">#</a></span><a aria-hidden="true" class="legacy" id="http_response_senddate"></a></h4>
<div class="api_metadata">
<span>Added in: v0.7.5</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>
<h4><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="http_response_setheader_name_value"></a></h4>
<div class="api_metadata">
<span>Added in: v0.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#Data_types" class="type"><any></a></li>
<li>Returns: <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li>
</ul>
<p>Returns the response object.</p>
<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. Non-string values will be
stored without modification. Therefore, <a href="#responsegetheadername"><code>response.getHeader()</code></a> may return
non-string values. However, the non-string values will be converted to strings
for network transmission. The same response object is returned to the caller,
to enable call chaining.</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'</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 = http.<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'</span> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>If <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> method is called and this method has not been
called, it will directly write the supplied header values onto the network
channel without caching internally, and the <a href="#responsegetheadername"><code>response.getHeader()</code></a> on the
header will not yield the expected result. If progressive population of headers
is desired with potential future retrieval and modification, use
<a href="#responsesetheadername-value"><code>response.setHeader()</code></a> instead of <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>.</p>
<h4><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="http_response_settimeout_msecs_callback"></a></h4>
<div class="api_metadata">
<span>Added in: v0.9.12</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="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li>
</ul>
<p>Sets the Socket'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 sockets 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>
<h4><code>response.socket</code><span><a class="mark" href="#responsesocket" id="responsesocket">#</a></span><a aria-hidden="true" class="legacy" id="http_response_socket"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div>
<ul>
<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>Reference to the underlying socket. Usually users will not want to access
this property. In particular, the socket will not emit <code>'readable'</code> events
because of how the protocol parser attaches to the socket. After
<code>response.end()</code>, the property is nulled.</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> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">const</span> server = http.<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 = res.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>;
<span class="hljs-keyword">const</span> port = res.<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> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">const</span> server = http.<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 = res.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>;
<span class="hljs-keyword">const</span> port = res.<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>
<p>This property is guaranteed to be an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specified a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<h4><code>response.statusCode</code><span><a class="mark" href="#responsestatuscode" id="responsestatuscode">#</a></span><a aria-hidden="true" class="legacy" id="http_response_statuscode"></a></h4>
<div class="api_metadata">
<span>Added in: v0.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> <strong>Default:</strong> <code>200</code></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>
<h4><code>response.statusMessage</code><span><a class="mark" href="#responsestatusmessage" id="responsestatusmessage">#</a></span><a aria-hidden="true" class="legacy" id="http_response_statusmessage"></a></h4>
<div class="api_metadata">
<span>Added in: v0.11.8</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>When using implicit headers (not calling <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> explicitly),
this property controls the status message that will be sent to the client when
the headers get flushed. If this is left as <code>undefined</code> then the standard
message for the status code will be used.</p>
<pre><code class="language-js">response.<span class="hljs-property">statusMessage</span> = <span class="hljs-string">'Not found'</span>;</code> <button class="copy-button">copy</button></pre>
<p>After response header was sent to the client, this property indicates the
status message which was sent out.</p>
<h4><code>response.strictContentLength</code><span><a class="mark" href="#responsestrictcontentlength" id="responsestrictcontentlength">#</a></span><a aria-hidden="true" class="legacy" id="http_response_strictcontentlength"></a></h4>
<div class="api_metadata">
<span>Added in: v18.10.0, v16.18.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li>
</ul>
<p>If set to <code>true</code>, Node.js will check whether the <code>Content-Length</code>
header value and the size of the body, in bytes, are equal.
Mismatching the <code>Content-Length</code> header value will result
in an <code>Error</code> being thrown, identified by <code>code:</code> <a href="errors.html#err_http_content_length_mismatch"><code>'ERR_HTTP_CONTENT_LENGTH_MISMATCH'</code></a>.</p>
<h4><code>response.uncork()</code><span><a class="mark" href="#responseuncork" id="responseuncork">#</a></span><a aria-hidden="true" class="legacy" id="http_response_uncork"></a></h4>
<div class="api_metadata">
<span>Added in: v13.2.0, v12.16.0</span>
</div>
<p>See <a href="stream.html#writableuncork"><code>writable.uncork()</code></a>.</p>
<h4><code>response.writableEnded</code><span><a class="mark" href="#responsewritableended" id="responsewritableended">#</a></span><a aria-hidden="true" class="legacy" id="http_response_writableended"></a></h4>
<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="#responsewritablefinished"><code>response.writableFinished</code></a> instead.</p>
<h4><code>response.writableFinished</code><span><a class="mark" href="#responsewritablefinished" id="responsewritablefinished">#</a></span><a aria-hidden="true" class="legacy" id="http_response_writablefinished"></a></h4>
<div class="api_metadata">
<span>Added in: v12.7.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> if all data has been flushed to the underlying system, immediately
before the <a href="#event-finish"><code>'finish'</code></a> event is emitted.</p>
<h4><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="http_response_write_chunk_encoding_callback"></a></h4>
<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>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v0.1.29</td>
<td><p><span>Added in: v0.1.29</span></p></td></tr>
</tbody></table>
</details>
</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> <strong>Default:</strong> <code>'utf8'</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></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>If <code>rejectNonStandardBodyWrites</code> is set to true in <code>createServer</code>
then writing to the body is not allowed when the request method or response
status do not support content. If an attempt is made to write to the body for a
HEAD request or as part of a <code>204</code> or <code>304</code>response, a synchronous <code>Error</code>
with the code <code>ERR_HTTP_BODY_NOT_ALLOWED</code> is thrown.</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.
<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>
<h4><code>response.writeContinue()</code><span><a class="mark" href="#responsewritecontinue" id="responsewritecontinue">#</a></span><a aria-hidden="true" class="legacy" id="http_response_writecontinue"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div>
<p>Sends an HTTP/1.1 100 Continue message to the client, indicating that
the request body should be sent. See the <a href="#event-checkcontinue"><code>'checkContinue'</code></a> event on
<code>Server</code>.</p>
<h4><code>response.writeEarlyHints(hints[, callback])</code><span><a class="mark" href="#responsewriteearlyhintshints-callback" id="responsewriteearlyhintshints-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_response_writeearlyhints_hints_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.11.0</td>
<td><p>Allow passing hints as an object.</p></td></tr>
<tr><td>v18.11.0</td>
<td><p><span>Added in: v18.11.0</span></p></td></tr>
</tbody></table>
</details>
</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>
<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>Sends an HTTP/1.1 103 Early Hints message 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. The optional <code>callback</code> argument will be called when
the response message has been written.</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,
<span class="hljs-string">'x-trace-id'</span>: <span class="hljs-string">'id for diagnostics'</span>,
});
<span class="hljs-keyword">const</span> <span class="hljs-title function_">earlyHintsCallback</span> = (<span class="hljs-params"></span>) => <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'early hints message sent'</span>);
response.<span class="hljs-title function_">writeEarlyHints</span>({
<span class="hljs-string">'link'</span>: earlyHintsLinks,
}, earlyHintsCallback);</code> <button class="copy-button">copy</button></pre>
<h4><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="http_response_writehead_statuscode_statusmessage_headers"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.14.0</td>
<td><p>Allow passing headers as an array.</p></td></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>v5.11.0, v4.4.5</td>
<td><p>A <code>RangeError</code> is thrown if <code>statusCode</code> is not a number in the range <code>[100, 999]</code>.</p></td></tr>
<tr><td>v0.1.30</td>
<td><p><span>Added in: v0.1.30</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="http.html#class-httpserverresponse" class="type"><http.ServerResponse></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.
Optionally one can give a human-readable <code>statusMessage</code> as the second
argument.</p>
<p><code>headers</code> may be an <code>Array</code> where 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. The array is in the same
format as <code>request.rawHeaders</code>.</p>
<p>Returns a reference to the <code>ServerResponse</code>, so that calls can be chained.</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'</span>,
})
.<span class="hljs-title function_">end</span>(body);</code> <button class="copy-button">copy</button></pre>
<p>This method must only be called once on a message and it must
be called 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>
<p>If this method is called and <a href="#responsesetheadername-value"><code>response.setHeader()</code></a> has not been called,
it will directly write the supplied header values onto the network channel
without caching internally, and the <a href="#responsegetheadername"><code>response.getHeader()</code></a> on the header
will not yield the expected result. If progressive population of headers is
desired with potential future retrieval and modification, use
<a href="#responsesetheadername-value"><code>response.setHeader()</code></a> instead.</p>
<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span>
<span class="hljs-keyword">const</span> server = http.<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'</span> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p><code>Content-Length</code> is read in bytes, not characters. Use
<a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength()</code></a> to determine the length of the body in bytes. Node.js
will check whether <code>Content-Length</code> and the length of the body which has
been transmitted are equal or not.</p>
<p>Attempting to set a header field name or value that contains invalid characters
will result in a [<code>Error</code>][] being thrown.</p>
<h4><code>response.writeProcessing()</code><span><a class="mark" href="#responsewriteprocessing" id="responsewriteprocessing">#</a></span><a aria-hidden="true" class="legacy" id="http_response_writeprocessing"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<p>Sends a HTTP/1.1 102 Processing message to the client, indicating that
the request body should be sent.</p>
</section><section><h3>Class: <code>http.IncomingMessage</code><span><a class="mark" href="#class-httpincomingmessage" id="class-httpincomingmessage">#</a></span><a aria-hidden="true" class="legacy" id="http_class_http_incomingmessage"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.5.0</td>
<td><p>The <code>destroyed</code> value returns <code>true</code> after the incoming data is consumed.</p></td></tr>
<tr><td>v13.1.0, v12.16.0</td>
<td><p>The <code>readableHighWaterMark</code> value mirrors that of the socket.</p></td></tr>
<tr><td>v0.1.17</td>
<td><p><span>Added in: v0.1.17</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li>Extends: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li>
</ul>
<p>An <code>IncomingMessage</code> object is created by <a href="#class-httpserver"><code>http.Server</code></a> or
<a href="#class-httpclientrequest"><code>http.ClientRequest</code></a> and passed as the first argument to the <a href="#event-request"><code>'request'</code></a>
and <a href="#event-response"><code>'response'</code></a> event respectively. It may be used to access response
status, headers, and data.</p>
<p>Different from its <code>socket</code> value which is a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, the
<code>IncomingMessage</code> itself extends <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> and is created separately to
parse and emit the incoming HTTP headers and payload, as the underlying socket
may be reused multiple times in case of keep-alive.</p>
<h4>Event: <code>'aborted'</code><span><a class="mark" href="#event-aborted" id="event-aborted">#</a></span><a aria-hidden="true" class="legacy" id="http_event_aborted"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.8</span><span>Deprecated since: v17.0.0, v16.12.0</span>
</div>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated. Listen for <code>'close'</code> event instead.</div><p></p>
<p>Emitted when the request has been aborted.</p>
<h4>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="http_event_close_3"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v16.0.0</td>
<td><p>The close event is now emitted when the request has been completed and not when the underlying socket is closed.</p></td></tr>
<tr><td>v0.4.2</td>
<td><p><span>Added in: v0.4.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>Emitted when the request has been completed.</p>
<h4><code>message.aborted</code><span><a class="mark" href="#messageaborted" id="messageaborted">#</a></span><a aria-hidden="true" class="legacy" id="http_message_aborted"></a></h4>
<div class="api_metadata">
<span>Added in: v10.1.0</span><span>Deprecated since: v17.0.0, v16.12.0</span>
</div>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated. Check <code>message.destroyed</code> from <a href="stream.html#class-streamreadable" class="type"><stream.Readable></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>The <code>message.aborted</code> property will be <code>true</code> if the request has
been aborted.</p>
<h4><code>message.complete</code><span><a class="mark" href="#messagecomplete" id="messagecomplete">#</a></span><a aria-hidden="true" class="legacy" id="http_message_complete"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.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>message.complete</code> property will be <code>true</code> if a complete HTTP message has
been received and successfully parsed.</p>
<p>This property is particularly useful as a means of determining if a client or
server fully transmitted a message before a connection was terminated:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>({
<span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
<span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>,
}, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
res.<span class="hljs-title function_">resume</span>();
res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-keyword">if</span> (!res.<span class="hljs-property">complete</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(
<span class="hljs-string">'The connection was terminated while the message was still being sent'</span>);
});
});</code> <button class="copy-button">copy</button></pre>
<h4><code>message.connection</code><span><a class="mark" href="#messageconnection" id="messageconnection">#</a></span><a aria-hidden="true" class="legacy" id="http_message_connection"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.90</span><span>Deprecated since: v16.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="#messagesocket"><code>message.socket</code></a>.</div><p></p>
<p>Alias for <a href="#messagesocket"><code>message.socket</code></a>.</p>
<h4><code>message.destroy([error])</code><span><a class="mark" href="#messagedestroyerror" id="messagedestroyerror">#</a></span><a aria-hidden="true" class="legacy" id="http_message_destroy_error"></a></h4>
<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>The function returns <code>this</code> for consistency with other Readable streams.</p></td></tr>
<tr><td>v0.3.0</td>
<td><p><span>Added in: v0.3.0</span></p></td></tr>
</tbody></table>
</details>
</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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li>
</ul>
<p>Calls <code>destroy()</code> on the socket that received the <code>IncomingMessage</code>. If <code>error</code>
is provided, an <code>'error'</code> event is emitted on the socket and <code>error</code> is passed
as an argument to any listeners on the event.</p>
<h4><code>message.headers</code><span><a class="mark" href="#messageheaders" id="messageheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_message_headers"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.5.0, v18.14.0</td>
<td><p>The <code>joinDuplicateHeaders</code> option in the <code>http.request()</code> and <code>http.createServer()</code> functions ensures that duplicate headers are not discarded, but rather combined using a comma separator, in accordance with RFC 9110 Section 5.3.</p></td></tr>
<tr><td>v15.1.0</td>
<td><p><code>message.headers</code> is now lazily computed using an accessor property on the prototype and is no longer enumerable.</p></td></tr>
<tr><td>v0.1.5</td>
<td><p><span>Added in: v0.1.5</span></p></td></tr>
</tbody></table>
</details>
</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>Duplicates in raw headers are handled in the following ways, depending on the
header name:</p>
<ul>
<li>Duplicates of <code>age</code>, <code>authorization</code>, <code>content-length</code>, <code>content-type</code>,
<code>etag</code>, <code>expires</code>, <code>from</code>, <code>host</code>, <code>if-modified-since</code>, <code>if-unmodified-since</code>,
<code>last-modified</code>, <code>location</code>, <code>max-forwards</code>, <code>proxy-authorization</code>, <code>referer</code>,
<code>retry-after</code>, <code>server</code>, or <code>user-agent</code> are discarded.
To allow duplicate values of the headers listed above to be joined,
use the option <code>joinDuplicateHeaders</code> in <a href="#httprequestoptions-callback"><code>http.request()</code></a>
and <a href="#httpcreateserveroptions-requestlistener"><code>http.createServer()</code></a>. See RFC 9110 Section 5.3 for more
information.</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 <code>; </code>.</li>
<li>For all other headers, the values are joined together with <code>, </code>.</li>
</ul>
<h4><code>message.headersDistinct</code><span><a class="mark" href="#messageheadersdistinct" id="messageheadersdistinct">#</a></span><a aria-hidden="true" class="legacy" id="http_message_headersdistinct"></a></h4>
<div class="api_metadata">
<span>Added in: v18.3.0, v16.17.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>Similar to <a href="#messageheaders"><code>message.headers</code></a>, but there is no join logic and the values are
always arrays of strings, even for headers received just once.</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">headersDistinct</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>message.httpVersion</code><span><a class="mark" href="#messagehttpversion" id="messagehttpversion">#</a></span><a aria-hidden="true" class="legacy" id="http_message_httpversion"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.1</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.
Probably either <code>'1.1'</code> or <code>'1.0'</code>.</p>
<p>Also <code>message.httpVersionMajor</code> is the first integer and
<code>message.httpVersionMinor</code> is the second.</p>
<h4><code>message.method</code><span><a class="mark" href="#messagemethod" id="messagemethod">#</a></span><a aria-hidden="true" class="legacy" id="http_message_method"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.1</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><strong>Only valid for request obtained from <a href="#class-httpserver"><code>http.Server</code></a>.</strong></p>
<p>The request method as a string. Read only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.</p>
<h4><code>message.rawHeaders</code><span><a class="mark" href="#messagerawheaders" id="messagerawheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_message_rawheaders"></a></h4>
<div class="api_metadata">
<span>Added in: v0.11.6</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>
<h4><code>message.rawTrailers</code><span><a class="mark" href="#messagerawtrailers" id="messagerawtrailers">#</a></span><a aria-hidden="true" class="legacy" id="http_message_rawtrailers"></a></h4>
<div class="api_metadata">
<span>Added in: v0.11.6</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>
<h4><code>message.setTimeout(msecs[, callback])</code><span><a class="mark" href="#messagesettimeoutmsecs-callback" id="messagesettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_message_settimeout_msecs_callback"></a></h4>
<div class="api_metadata">
<span>Added in: v0.5.9</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="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li>
</ul>
<p>Calls <code>message.socket.setTimeout(msecs, callback)</code>.</p>
<h4><code>message.socket</code><span><a class="mark" href="#messagesocket" id="messagesocket">#</a></span><a aria-hidden="true" class="legacy" id="http_message_socket"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div>
<ul>
<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>The <a href="net.html#class-netsocket"><code>net.Socket</code></a> object associated with the connection.</p>
<p>With HTTPS support, use <a href="tls.html#tlssocketgetpeercertificatedetailed"><code>request.socket.getPeerCertificate()</code></a> to obtain the
client's authentication details.</p>
<p>This property is guaranteed to be an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class,
a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specified a socket
type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a> or internally nulled.</p>
<h4><code>message.statusCode</code><span><a class="mark" href="#messagestatuscode" id="messagestatuscode">#</a></span><a aria-hidden="true" class="legacy" id="http_message_statuscode"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.1</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><strong>Only valid for response obtained from <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a>.</strong></p>
<p>The 3-digit HTTP response status code. E.G. <code>404</code>.</p>
<h4><code>message.statusMessage</code><span><a class="mark" href="#messagestatusmessage" id="messagestatusmessage">#</a></span><a aria-hidden="true" class="legacy" id="http_message_statusmessage"></a></h4>
<div class="api_metadata">
<span>Added in: v0.11.10</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><strong>Only valid for response obtained from <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a>.</strong></p>
<p>The HTTP response status message (reason phrase). E.G. <code>OK</code> or <code>Internal Server Error</code>.</p>
<h4><code>message.trailers</code><span><a class="mark" href="#messagetrailers" id="messagetrailers">#</a></span><a aria-hidden="true" class="legacy" id="http_message_trailers"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.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>
<h4><code>message.trailersDistinct</code><span><a class="mark" href="#messagetrailersdistinct" id="messagetrailersdistinct">#</a></span><a aria-hidden="true" class="legacy" id="http_message_trailersdistinct"></a></h4>
<div class="api_metadata">
<span>Added in: v18.3.0, v16.17.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>Similar to <a href="#messagetrailers"><code>message.trailers</code></a>, but there is no join logic and the values are
always arrays of strings, even for headers received just once.
Only populated at the <code>'end'</code> event.</p>
<h4><code>message.url</code><span><a class="mark" href="#messageurl" id="messageurl">#</a></span><a aria-hidden="true" class="legacy" id="http_message_url"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.90</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><strong>Only valid for request obtained from <a href="#class-httpserver"><code>http.Server</code></a>.</strong></p>
<p>Request URL string. This contains only the URL that is present in the actual
HTTP request. Take the following request:</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>To parse the URL into its parts:</p>
<pre><code class="language-js"><span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">`http://<span class="hljs-subst">${process.env.HOST ?? <span class="hljs-string">'localhost'</span>}</span><span class="hljs-subst">${request.url}</span>`</span>);</code> <button class="copy-button">copy</button></pre>
<p>When <code>request.url</code> is <code>'/status?name=ryan'</code> and <code>process.env.HOST</code> is undefined:</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(`http://<span class="hljs-variable">${process.env.HOST ?? 'localhost'}</span><span class="hljs-variable">${request.url}</span>`);</span>
URL {
href: 'http://localhost/status?name=ryan',
origin: 'http://localhost',
protocol: 'http:',
username: '',
password: '',
host: 'localhost',
hostname: 'localhost',
port: '',
pathname: '/status',
search: '?name=ryan',
searchParams: URLSearchParams { 'name' => 'ryan' },
hash: ''
}</code> <button class="copy-button">copy</button></pre>
<p>Ensure that you set <code>process.env.HOST</code> to the server's host name, or consider
replacing this part entirely. If using <code>req.headers.host</code>, ensure proper
validation is used, as clients may specify a custom <code>Host</code> header.</p>
</section><section><h3>Class: <code>http.OutgoingMessage</code><span><a class="mark" href="#class-httpoutgoingmessage" id="class-httpoutgoingmessage">#</a></span><a aria-hidden="true" class="legacy" id="http_class_http_outgoingmessage"></a></h3>
<div class="api_metadata">
<span>Added in: v0.1.17</span>
</div>
<ul>
<li>Extends: <a href="stream.html#stream" class="type"><Stream></a></li>
</ul>
<p>This class serves as the parent class of <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a>
and <a href="#class-httpserverresponse"><code>http.ServerResponse</code></a>. It is an abstract outgoing message from
the perspective of the participants of an HTTP transaction.</p>
<h4>Event: <code>'drain'</code><span><a class="mark" href="#event-drain" id="event-drain">#</a></span><a aria-hidden="true" class="legacy" id="http_event_drain"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.6</span>
</div>
<p>Emitted when the buffer of the message is free again.</p>
<h4>Event: <code>'finish'</code><span><a class="mark" href="#event-finish_2" id="event-finish_2">#</a></span><a aria-hidden="true" class="legacy" id="http_event_finish_2"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.17</span>
</div>
<p>Emitted when the transmission is finished successfully.</p>
<h4>Event: <code>'prefinish'</code><span><a class="mark" href="#event-prefinish" id="event-prefinish">#</a></span><a aria-hidden="true" class="legacy" id="http_event_prefinish"></a></h4>
<div class="api_metadata">
<span>Added in: v0.11.6</span>
</div>
<p>Emitted after <code>outgoingMessage.end()</code> is called.
When the event is emitted, all data has been processed but not necessarily
completely flushed.</p>
<h4><code>outgoingMessage.addTrailers(headers)</code><span><a class="mark" href="#outgoingmessageaddtrailersheaders" id="outgoingmessageaddtrailersheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_addtrailers_headers"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.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>Adds HTTP trailers (headers but at the end of the message) to the message.</p>
<p>Trailers will <strong>only</strong> be emitted if the message is chunked encoded. If not,
the trailers will be silently discarded.</p>
<p>HTTP requires the <code>Trailer</code> header to be sent to emit trailers,
with a list of header field names in its value, e.g.</p>
<pre><code class="language-js">message.<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'</span>,
<span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> });
message.<span class="hljs-title function_">write</span>(fileData);
message.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-MD5'</span>: <span class="hljs-string">'7895bf4b8828b55ceaf47747b4bca667'</span> });
message.<span class="hljs-title function_">end</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 <code>TypeError</code> being thrown.</p>
<h4><code>outgoingMessage.appendHeader(name, value)</code><span><a class="mark" href="#outgoingmessageappendheadername-value" id="outgoingmessageappendheadername-value">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_appendheader_name_value"></a></h4>
<div class="api_metadata">
<span>Added in: v18.3.0, v16.17.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> Header name</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> Header value</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></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="#outgoingmessagesetheadername-value"><code>outgoingMessage.setHeader(name, value)</code></a>.</p>
<p>Depending of the value of <code>options.uniqueHeaders</code> when the client request or the
server were created, this will end up in the header being sent multiple times or
a single time with values joined using <code>; </code>.</p>
<h4><code>outgoingMessage.connection</code><span><a class="mark" href="#outgoingmessageconnection" id="outgoingmessageconnection">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_connection"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.0</span><span>Deprecated since: v15.12.0, v14.17.1</span>
</div>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#outgoingmessagesocket"><code>outgoingMessage.socket</code></a> instead.</div><p></p>
<p>Alias of <a href="#outgoingmessagesocket"><code>outgoingMessage.socket</code></a>.</p>
<h4><code>outgoingMessage.cork()</code><span><a class="mark" href="#outgoingmessagecork" id="outgoingmessagecork">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_cork"></a></h4>
<div class="api_metadata">
<span>Added in: v13.2.0, v12.16.0</span>
</div>
<p>See <a href="stream.html#writablecork"><code>writable.cork()</code></a>.</p>
<h4><code>outgoingMessage.destroy([error])</code><span><a class="mark" href="#outgoingmessagedestroyerror" id="outgoingmessagedestroyerror">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_destroy_error"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.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> Optional, an error to emit with <code>error</code> event</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li>
</ul>
<p>Destroys the message. Once a socket is associated with the message
and is connected, that socket will be destroyed as well.</p>
<h4><code>outgoingMessage.end(chunk[, encoding][, callback])</code><span><a class="mark" href="#outgoingmessageendchunk-encoding-callback" id="outgoingmessageendchunk-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_end_chunk_encoding_callback"></a></h4>
<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>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v0.11.6</td>
<td><p>add <code>callback</code> argument.</p></td></tr>
<tr><td>v0.1.90</td>
<td><p><span>Added in: v0.1.90</span></p></td></tr>
</tbody></table>
</details>
</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> Optional, <strong>Default</strong>: <code>utf8</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> Optional</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li>
</ul>
<p>Finishes the outgoing message. If any parts of the body are unsent, it will
flush them to the underlying system. If the message is chunked, it will
send the terminating chunk <code>0\r\n\r\n</code>, and send the trailers (if any).</p>
<p>If <code>chunk</code> is specified, it is equivalent to calling
<code>outgoingMessage.write(chunk, encoding)</code>, followed by
<code>outgoingMessage.end(callback)</code>.</p>
<p>If <code>callback</code> is provided, it will be called when the message is finished
(equivalent to a listener of the <code>'finish'</code> event).</p>
<h4><code>outgoingMessage.flushHeaders()</code><span><a class="mark" href="#outgoingmessageflushheaders" id="outgoingmessageflushheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_flushheaders"></a></h4>
<div class="api_metadata">
<span>Added in: v1.6.0</span>
</div>
<p>Flushes the message headers.</p>
<p>For efficiency reason, Node.js normally buffers the message headers
until <code>outgoingMessage.end()</code> is called or the first chunk of message data
is written. It then tries to pack the headers and data into a single TCP
packet.</p>
<p>It is usually desired (it saves a TCP round-trip), but not when the first
data is not sent until possibly much later. <code>outgoingMessage.flushHeaders()</code>
bypasses the optimization and kickstarts the message.</p>
<h4><code>outgoingMessage.getHeader(name)</code><span><a class="mark" href="#outgoingmessagegetheadername" id="outgoingmessagegetheadername">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_getheader_name"></a></h4>
<div class="api_metadata">
<span>Added in: v0.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> Name of header</li>
<li>Returns: <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>Gets the value of the HTTP header with the given name. If that header is not
set, the returned value will be <code>undefined</code>.</p>
<h4><code>outgoingMessage.getHeaderNames()</code><span><a class="mark" href="#outgoingmessagegetheadernames" id="outgoingmessagegetheadernames">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_getheadernames"></a></h4>
<div class="api_metadata">
<span>Added in: v7.7.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 names are lowercase.</p>
<h4><code>outgoingMessage.getHeaders()</code><span><a class="mark" href="#outgoingmessagegetheaders" id="outgoingmessagegetheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_getheaders"></a></h4>
<div class="api_metadata">
<span>Added in: v7.7.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>outgoingMessage.getHeaders()</code> method does
not 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 will not work.</p>
<pre><code class="language-js">outgoingMessage.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Foo'</span>, <span class="hljs-string">'bar'</span>);
outgoingMessage.<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 = outgoingMessage.<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>
<h4><code>outgoingMessage.hasHeader(name)</code><span><a class="mark" href="#outgoingmessagehasheadername" id="outgoingmessagehasheadername">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_hasheader_name"></a></h4>
<div class="api_metadata">
<span>Added in: v7.7.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 is case-insensitive.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> hasContentType = outgoingMessage.<span class="hljs-title function_">hasHeader</span>(<span class="hljs-string">'content-type'</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>outgoingMessage.headersSent</code><span><a class="mark" href="#outgoingmessageheaderssent" id="outgoingmessageheaderssent">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_headerssent"></a></h4>
<div class="api_metadata">
<span>Added in: v0.9.3</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. <code>true</code> if the headers were sent, otherwise <code>false</code>.</p>
<h4><code>outgoingMessage.pipe()</code><span><a class="mark" href="#outgoingmessagepipe" id="outgoingmessagepipe">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_pipe"></a></h4>
<div class="api_metadata">
<span>Added in: v9.0.0</span>
</div>
<p>Overrides the <code>stream.pipe()</code> method inherited from the legacy <code>Stream</code> class
which is the parent class of <code>http.OutgoingMessage</code>.</p>
<p>Calling this method will throw an <code>Error</code> because <code>outgoingMessage</code> is a
write-only stream.</p>
<h4><code>outgoingMessage.removeHeader(name)</code><span><a class="mark" href="#outgoingmessageremoveheadername" id="outgoingmessageremoveheadername">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_removeheader_name"></a></h4>
<div class="api_metadata">
<span>Added in: v0.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> Header name</li>
</ul>
<p>Removes a header that is queued for implicit sending.</p>
<pre><code class="language-js">outgoingMessage.<span class="hljs-title function_">removeHeader</span>(<span class="hljs-string">'Content-Encoding'</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>outgoingMessage.setHeader(name, value)</code><span><a class="mark" href="#outgoingmessagesetheadername-value" id="outgoingmessagesetheadername-value">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_setheader_name_value"></a></h4>
<div class="api_metadata">
<span>Added in: v0.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> Header name</li>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Header value</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li>
</ul>
<p>Sets a single header value. If the header already exists in the to-be-sent
headers, its value will be replaced. Use an array of strings to send multiple
headers with the same name.</p>
<h4><code>outgoingMessage.setHeaders(headers)</code><span><a class="mark" href="#outgoingmessagesetheadersheaders" id="outgoingmessagesetheadersheaders">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_setheaders_headers"></a></h4>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/Headers" class="type"><Headers></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" class="type"><Map></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>Sets multiple header values for implicit headers.
<code>headers</code> must be an instance of <a href="globals.html#class-headers"><code>Headers</code></a> or <code>Map</code>,
if a header already exists in the to-be-sent headers,
its value will be replaced.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> headers = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Headers</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span> });
outgoingMessage.<span class="hljs-title function_">setHeaders</span>(headers);</code> <button class="copy-button">copy</button></pre>
<p>or</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> headers = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>([[<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>]]);
outgoingMessage.<span class="hljs-title function_">setHeaders</span>(headers);</code> <button class="copy-button">copy</button></pre>
<p>When headers have been set with <a href="#outgoingmessagesetheadersheaders"><code>outgoingMessage.setHeaders()</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 = http.<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> headers = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Headers</span>({ <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/html'</span> });
res.<span class="hljs-title function_">setHeaders</span>(headers);
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'</span> });
res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>outgoingMessage.setTimeout(msesc[, callback])</code><span><a class="mark" href="#outgoingmessagesettimeoutmsesc-callback" id="outgoingmessagesettimeoutmsesc-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_settimeout_msesc_callback"></a></h4>
<div class="api_metadata">
<span>Added in: v0.9.12</span>
</div>
<ul>
<li><code>msesc</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> Optional function to be called when a timeout
occurs. Same as binding to the <code>timeout</code> event.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li>
</ul>
<p>Once a socket is associated with the message and is connected,
<a href="net.html#socketsettimeouttimeout-callback"><code>socket.setTimeout()</code></a> will be called with <code>msecs</code> as the first parameter.</p>
<h4><code>outgoingMessage.socket</code><span><a class="mark" href="#outgoingmessagesocket" id="outgoingmessagesocket">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_socket"></a></h4>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div>
<ul>
<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li>
</ul>
<p>Reference to the underlying socket. Usually, users will not want to access
this property.</p>
<p>After calling <code>outgoingMessage.end()</code>, this property will be nulled.</p>
<h4><code>outgoingMessage.uncork()</code><span><a class="mark" href="#outgoingmessageuncork" id="outgoingmessageuncork">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_uncork"></a></h4>
<div class="api_metadata">
<span>Added in: v13.2.0, v12.16.0</span>
</div>
<p>See <a href="stream.html#writableuncork"><code>writable.uncork()</code></a></p>
<h4><code>outgoingMessage.writableCorked</code><span><a class="mark" href="#outgoingmessagewritablecorked" id="outgoingmessagewritablecorked">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_writablecorked"></a></h4>
<div class="api_metadata">
<span>Added in: v13.2.0, v12.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>The number of times <code>outgoingMessage.cork()</code> has been called.</p>
<h4><code>outgoingMessage.writableEnded</code><span><a class="mark" href="#outgoingmessagewritableended" id="outgoingmessagewritableended">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_writableended"></a></h4>
<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> if <code>outgoingMessage.end()</code> has been called. This property does
not indicate whether the data has been flushed. For that purpose, use
<code>message.writableFinished</code> instead.</p>
<h4><code>outgoingMessage.writableFinished</code><span><a class="mark" href="#outgoingmessagewritablefinished" id="outgoingmessagewritablefinished">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_writablefinished"></a></h4>
<div class="api_metadata">
<span>Added in: v12.7.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> if all data has been flushed to the underlying system.</p>
<h4><code>outgoingMessage.writableHighWaterMark</code><span><a class="mark" href="#outgoingmessagewritablehighwatermark" id="outgoingmessagewritablehighwatermark">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_writablehighwatermark"></a></h4>
<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#Number_type" class="type"><number></a></li>
</ul>
<p>The <code>highWaterMark</code> of the underlying socket if assigned. Otherwise, the default
buffer level when <a href="stream.html#writablewritechunk-encoding-callback"><code>writable.write()</code></a> starts returning false (<code>16384</code>).</p>
<h4><code>outgoingMessage.writableLength</code><span><a class="mark" href="#outgoingmessagewritablelength" id="outgoingmessagewritablelength">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_writablelength"></a></h4>
<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#Number_type" class="type"><number></a></li>
</ul>
<p>The number of buffered bytes.</p>
<h4><code>outgoingMessage.writableObjectMode</code><span><a class="mark" href="#outgoingmessagewritableobjectmode" id="outgoingmessagewritableobjectmode">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_writableobjectmode"></a></h4>
<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>Always <code>false</code>.</p>
<h4><code>outgoingMessage.write(chunk[, encoding][, callback])</code><span><a class="mark" href="#outgoingmessagewritechunk-encoding-callback" id="outgoingmessagewritechunk-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_outgoingmessage_write_chunk_encoding_callback"></a></h4>
<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>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v0.11.6</td>
<td><p>The <code>callback</code> argument was added.</p></td></tr>
<tr><td>v0.1.29</td>
<td><p><span>Added in: v0.1.29</span></p></td></tr>
</tbody></table>
</details>
</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> <strong>Default</strong>: <code>utf8</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></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 chunk of the body. This method can be called multiple times.</p>
<p>The <code>encoding</code> argument is only relevant when <code>chunk</code> is a string. Defaults to
<code>'utf8'</code>.</p>
<p>The <code>callback</code> argument is optional and will be called when this chunk of data
is flushed.</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 the user
memory. The <code>'drain'</code> event will be emitted when the buffer is free again.</p>
</section><section><h3><code>http.METHODS</code><span><a class="mark" href="#httpmethods" id="httpmethods">#</a></span><a aria-hidden="true" class="legacy" id="http_http_methods"></a></h3>
<div class="api_metadata">
<span>Added in: v0.11.8</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>A list of the HTTP methods that are supported by the parser.</p>
</section><section><h3><code>http.STATUS_CODES</code><span><a class="mark" href="#httpstatus_codes" id="httpstatus_codes">#</a></span><a aria-hidden="true" class="legacy" id="http_http_status_codes"></a></h3>
<div class="api_metadata">
<span>Added in: v0.1.22</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>A collection of all the standard HTTP response status codes, and the
short description of each. For example, <code>http.STATUS_CODES[404] === 'Not Found'</code>.</p>
</section><section><h3><code>http.createServer([options][, requestListener])</code><span><a class="mark" href="#httpcreateserveroptions-requestlistener" id="httpcreateserveroptions-requestlistener">#</a></span><a aria-hidden="true" class="legacy" id="http_http_createserver_options_requestlistener"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>The <code>highWaterMark</code> option is supported now.</p></td></tr>
<tr><td>v18.0.0</td>
<td><p>The <code>requestTimeout</code>, <code>headersTimeout</code>, <code>keepAliveTimeout</code>, and <code>connectionsCheckingInterval</code> options are supported now.</p></td></tr>
<tr><td>v18.0.0</td>
<td><p>The <code>noDelay</code> option now defaults to <code>true</code>.</p></td></tr>
<tr><td>v17.7.0, v16.15.0</td>
<td><p>The <code>noDelay</code>, <code>keepAlive</code> and <code>keepAliveInitialDelay</code> options are supported now.</p></td></tr>
<tr><td>v13.3.0</td>
<td><p>The <code>maxHeaderSize</code> option is supported now.</p></td></tr>
<tr><td>v13.8.0, v12.15.0, v10.19.0</td>
<td><p>The <code>insecureHTTPParser</code> option is supported now.</p></td></tr>
<tr><td>v9.6.0, v8.12.0</td>
<td><p>The <code>options</code> argument is supported now.</p></td></tr>
<tr><td>v0.1.13</td>
<td><p><span>Added in: v0.1.13</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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>connectionsCheckingInterval</code>: Sets the interval value in milliseconds to
check for request and headers timeout in incomplete requests.
<strong>Default:</strong> <code>30000</code>.</li>
<li><code>headersTimeout</code>: Sets the timeout value in milliseconds for receiving
the complete HTTP headers from the client.
See <a href="#serverheaderstimeout"><code>server.headersTimeout</code></a> for more information.
<strong>Default:</strong> <code>60000</code>.</li>
<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Optionally overrides all <code>socket</code>s'
<code>readableHighWaterMark</code> and <code>writableHighWaterMark</code>. This affects
<code>highWaterMark</code> property of both <code>IncomingMessage</code> and <code>ServerResponse</code>.
<strong>Default:</strong> See <a href="stream.html#streamgetdefaulthighwatermarkobjectmode"><code>stream.getDefaultHighWaterMark()</code></a>.</li>
<li><code>insecureHTTPParser</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, it will use a HTTP parser
with leniency flags enabled. Using the insecure parser should be avoided.
See <a href="cli.html#--insecure-http-parser"><code>--insecure-http-parser</code></a> for more information.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>IncomingMessage</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Specifies the <code>IncomingMessage</code>
class to be used. Useful for extending the original <code>IncomingMessage</code>.
<strong>Default:</strong> <code>IncomingMessage</code>.</li>
<li><code>joinDuplicateHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, this option allows
joining the field line values of multiple headers in a request with
a comma (<code>, </code>) instead of discarding the duplicates.
For more information, refer to <a href="#messageheaders"><code>message.headers</code></a>.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>keepAlive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, it enables keep-alive functionality
on the socket immediately after a new incoming connection is received,
similarly on what is done in [<code>socket.setKeepAlive([enable][, initialDelay])</code>][<code>socket.setKeepAlive(enable, initialDelay)</code>].
<strong>Default:</strong> <code>false</code>.</li>
<li><code>keepAliveInitialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> If set to a positive number, it sets the
initial delay before the first keepalive probe is sent on an idle socket.
<strong>Default:</strong> <code>0</code>.</li>
<li><code>keepAliveTimeout</code>: The number of milliseconds of inactivity a server
needs to wait for additional incoming data, after it has finished writing
the last response, before a socket will be destroyed.
See <a href="#serverkeepalivetimeout"><code>server.keepAliveTimeout</code></a> for more information.
<strong>Default:</strong> <code>5000</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> Optionally overrides the value of
<a href="cli.html#--max-http-header-sizesize"><code>--max-http-header-size</code></a> for requests received by this server, i.e.
the maximum length of request headers in bytes.
<strong>Default:</strong> 16384 (16 KiB).</li>
<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, it disables the use of Nagle's
algorithm immediately after a new incoming connection is received.
<strong>Default:</strong> <code>true</code>.</li>
<li><code>requestTimeout</code>: Sets the timeout value in milliseconds for receiving
the entire request from the client.
See <a href="#serverrequesttimeout"><code>server.requestTimeout</code></a> for more information.
<strong>Default:</strong> <code>300000</code>.</li>
<li><code>requireHostHeader</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, it forces the server to
respond with a 400 (Bad Request) status code to any HTTP/1.1
request message that lacks a Host header
(as mandated by the specification).
<strong>Default:</strong> <code>true</code>.</li>
<li><code>ServerResponse</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a> Specifies the <code>ServerResponse</code> class
to be used. Useful for extending the original <code>ServerResponse</code>. <strong>Default:</strong>
<code>ServerResponse</code>.</li>
<li><code>uniqueHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> A list of response headers that should be sent only
once. If the header's value is an array, the items will be joined
using <code>; </code>.</li>
<li><code>rejectNonStandardBodyWrites</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, an error is thrown
when writing to an HTTP response which does not have a body.
<strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>
<p><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p>
</li>
<li>
<p>Returns: <a href="http.html#class-httpserver" class="type"><http.Server></a></p>
</li>
</ul>
<p>Returns a new instance of <a href="#class-httpserver"><code>http.Server</code></a>.</p>
<p>The <code>requestListener</code> is a function which is automatically
added to the <a href="#event-request"><code>'request'</code></a> event.</p>
<pre class="with-34-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> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-comment">// Create a local server to receive data from</span>
<span class="hljs-keyword">const</span> server = http.<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_">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>({
<span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</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> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-comment">// Create a local server to receive data from</span>
<span class="hljs-keyword">const</span> server = http.<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_">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>({
<span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>,
}));
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><button class="copy-button">copy</button></pre>
<pre class="with-34-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> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-comment">// Create a local server to receive data from</span>
<span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>();
<span class="hljs-comment">// Listen to the request event</span>
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'request'</span>, <span class="hljs-function">(<span class="hljs-params">request, res</span>) =></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">'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>({
<span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</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> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-comment">// Create a local server to receive data from</span>
<span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>();
<span class="hljs-comment">// Listen to the request event</span>
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'request'</span>, <span class="hljs-function">(<span class="hljs-params">request, res</span>) =></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">'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>({
<span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>,
}));
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><button class="copy-button">copy</button></pre>
</section><section><h3><code>http.get(options[, callback])</code><span><a class="mark" href="#httpgetoptions-callback" id="httpgetoptions-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_http_get_options_callback"></a></h3>
</section><section><h3><code>http.get(url[, options][, callback])</code><span><a class="mark" href="#httpgeturl-options-callback" id="httpgeturl-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_http_get_url_options_callback"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.9.0</td>
<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr>
<tr><td>v7.5.0</td>
<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr>
<tr><td>v0.3.6</td>
<td><p><span>Added in: v0.3.6</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>url</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></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Accepts the same <code>options</code> as
<a href="#httprequestoptions-callback"><code>http.request()</code></a>, with the method set to GET by default.</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="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li>
</ul>
<p>Since most requests are GET requests without bodies, Node.js provides this
convenience method. The only difference between this method and
<a href="#httprequestoptions-callback"><code>http.request()</code></a> is that it sets the method to GET by default and calls <code>req.end()</code>
automatically. The callback must take care to consume the response
data for reasons stated in <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a> section.</p>
<p>The <code>callback</code> is invoked with a single argument that is an instance of
<a href="#class-httpincomingmessage"><code>http.IncomingMessage</code></a>.</p>
<p>JSON fetching example:</p>
<pre><code class="language-js">http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:8000/'</span>, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
<span class="hljs-keyword">const</span> { statusCode } = res;
<span class="hljs-keyword">const</span> contentType = res.<span class="hljs-property">headers</span>[<span class="hljs-string">'content-type'</span>];
<span class="hljs-keyword">let</span> error;
<span class="hljs-comment">// Any 2xx status code signals a successful response but</span>
<span class="hljs-comment">// here we're only checking for 200.</span>
<span class="hljs-keyword">if</span> (statusCode !== <span class="hljs-number">200</span>) {
error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Request Failed.\n'</span> +
<span class="hljs-string">`Status Code: <span class="hljs-subst">${statusCode}</span>`</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!<span class="hljs-regexp">/^application\/json/</span>.<span class="hljs-title function_">test</span>(contentType)) {
error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Invalid content-type.\n'</span> +
<span class="hljs-string">`Expected application/json but received <span class="hljs-subst">${contentType}</span>`</span>);
}
<span class="hljs-keyword">if</span> (error) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(error.<span class="hljs-property">message</span>);
<span class="hljs-comment">// Consume response data to free up memory</span>
res.<span class="hljs-title function_">resume</span>();
<span class="hljs-keyword">return</span>;
}
res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
<span class="hljs-keyword">let</span> rawData = <span class="hljs-string">''</span>;
res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> { rawData += chunk; });
res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> parsedData = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(rawData);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(parsedData);
} <span class="hljs-keyword">catch</span> (e) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e.<span class="hljs-property">message</span>);
}
});
}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`Got error: <span class="hljs-subst">${e.message}</span>`</span>);
});
<span class="hljs-comment">// Create a local server to receive data from</span>
<span class="hljs-keyword">const</span> server = http.<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_">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>({
<span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>,
}));
});
server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre>
</section><section><h3><code>http.globalAgent</code><span><a class="mark" href="#httpglobalagent" id="httpglobalagent">#</a></span><a aria-hidden="true" class="legacy" id="http_http_globalagent"></a></h3>
<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>The agent now uses HTTP Keep-Alive and a 5 second timeout by default.</p></td></tr>
<tr><td>v0.5.9</td>
<td><p><span>Added in: v0.5.9</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><a href="http.html#class-httpagent" class="type"><http.Agent></a></li>
</ul>
<p>Global instance of <code>Agent</code> which is used as the default for all HTTP client
requests. Diverges from a default <code>Agent</code> configuration by having <code>keepAlive</code>
enabled and a <code>timeout</code> of 5 seconds.</p>
</section><section><h3><code>http.maxHeaderSize</code><span><a class="mark" href="#httpmaxheadersize" id="httpmaxheadersize">#</a></span><a aria-hidden="true" class="legacy" id="http_http_maxheadersize"></a></h3>
<div class="api_metadata">
<span>Added in: v11.6.0, v10.15.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>Read-only property specifying the maximum allowed size of HTTP headers in bytes.
Defaults to 16 KiB. Configurable using the <a href="cli.html#--max-http-header-sizesize"><code>--max-http-header-size</code></a> CLI
option.</p>
<p>This can be overridden for servers and client requests by passing the
<code>maxHeaderSize</code> option.</p>
</section><section><h3><code>http.request(options[, callback])</code><span><a class="mark" href="#httprequestoptions-callback" id="httprequestoptions-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_http_request_options_callback"></a></h3>
</section><section><h3><code>http.request(url[, options][, callback])</code><span><a class="mark" href="#httprequesturl-options-callback" id="httprequesturl-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="http_http_request_url_options_callback"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v16.7.0, v14.18.0</td>
<td><p>When using a <code>URL</code> object parsed username and password will now be properly URI decoded.</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>v13.3.0</td>
<td><p>The <code>maxHeaderSize</code> option is supported now.</p></td></tr>
<tr><td>v13.8.0, v12.15.0, v10.19.0</td>
<td><p>The <code>insecureHTTPParser</code> option is supported now.</p></td></tr>
<tr><td>v10.9.0</td>
<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr>
<tr><td>v7.5.0</td>
<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr>
<tr><td>v0.3.6</td>
<td><p><span>Added in: v0.3.6</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>url</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></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>agent</code> <a href="http.html#class-httpagent" class="type"><http.Agent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Controls <a href="#class-httpagent"><code>Agent</code></a> behavior. Possible
values:
<ul>
<li><code>undefined</code> (default): use <a href="#httpglobalagent"><code>http.globalAgent</code></a> for this host and port.</li>
<li><code>Agent</code> object: explicitly use the passed in <code>Agent</code>.</li>
<li><code>false</code>: causes a new <code>Agent</code> with default values to be used.</li>
</ul>
</li>
<li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Basic authentication (<code>'user:password'</code>) to compute an
Authorization header.</li>
<li><code>createConnection</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A function that produces a socket/stream to
use for the request when the <code>agent</code> option is not used. This can be used to
avoid creating a custom <code>Agent</code> class just to override the default
<code>createConnection</code> function. See <a href="#agentcreateconnectionoptions-callback"><code>agent.createConnection()</code></a> for more
details. Any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream is a valid return value.</li>
<li><code>defaultPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Default port for the protocol. <strong>Default:</strong>
<code>agent.defaultPort</code> if an <code>Agent</code> is used, else <code>undefined</code>.</li>
<li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> IP address family to use when resolving <code>host</code> or
<code>hostname</code>. Valid values are <code>4</code> or <code>6</code>. When unspecified, both IP v4 and
v6 will be used.</li>
<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An object containing request headers.</li>
<li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Optional <a href="dns.html#supported-getaddrinfo-flags"><code>dns.lookup()</code> hints</a>.</li>
<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A domain name or IP address of the server to issue the
request to. <strong>Default:</strong> <code>'localhost'</code>.</li>
<li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Alias for <code>host</code>. To support <a href="url.html#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a>,
<code>hostname</code> will be used if both <code>host</code> and <code>hostname</code> are specified.</li>
<li><code>insecureHTTPParser</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, it will use a HTTP parser
with leniency flags enabled. Using the insecure parser should be avoided.
See <a href="cli.html#--insecure-http-parser"><code>--insecure-http-parser</code></a> for more information.
<strong>Default:</strong> <code>false</code></li>
<li><code>joinDuplicateHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> It joins the field line values of
multiple headers in a request with <code>, </code> instead of discarding
the duplicates. See <a href="#messageheaders"><code>message.headers</code></a> for more information.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Local interface to bind for network connections.</li>
<li><code>localPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Local port to connect from.</li>
<li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Custom lookup function. <strong>Default:</strong> <a href="dns.html#dnslookuphostname-options-callback"><code>dns.lookup()</code></a>.</li>
<li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Optionally overrides the value of
<a href="cli.html#--max-http-header-sizesize"><code>--max-http-header-size</code></a> (the maximum length of response headers in
bytes) for responses received from the server.
<strong>Default:</strong> 16384 (16 KiB).</li>
<li><code>method</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A string specifying the HTTP request method. <strong>Default:</strong>
<code>'GET'</code>.</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Request path. Should include query string if any.
E.G. <code>'/index.html?page=12'</code>. An exception is thrown when the request path
contains illegal characters. Currently, only spaces are rejected but that
may change in the future. <strong>Default:</strong> <code>'/'</code>.</li>
<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Port of remote server. <strong>Default:</strong> <code>defaultPort</code> if set,
else <code>80</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> Protocol to use. <strong>Default:</strong> <code>'http:'</code>.</li>
<li><code>setDefaultHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a>: Specifies whether or not to automatically add
default headers such as <code>Connection</code>, <code>Content-Length</code>, <code>Transfer-Encoding</code>,
and <code>Host</code>. If set to <code>false</code> then all necessary headers must be added
manually. Defaults to <code>true</code>.</li>
<li><code>setHost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a>: Specifies whether or not to automatically add the
<code>Host</code> header. If provided, this overrides <code>setDefaultHeaders</code>. Defaults to
<code>true</code>.</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>
<li><code>socketPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Unix domain socket. Cannot be used if one of <code>host</code>
or <code>port</code> is specified, as those specify a TCP Socket.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a>: A number specifying the socket timeout in milliseconds.
This will set the timeout before the socket is connected.</li>
<li><code>uniqueHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> A list of request headers that should be sent
only once. If the header's value is an array, the items will be joined
using <code>; </code>.</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></li>
<li>Returns: <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li>
</ul>
<p><code>options</code> in <a href="net.html#socketconnectoptions-connectlistener"><code>socket.connect()</code></a> are also supported.</p>
<p>Node.js maintains several connections per server to make HTTP requests.
This function allows one to transparently issue requests.</p>
<p><code>url</code> can be a string or a <a href="url.html#the-whatwg-url-api"><code>URL</code></a> object. If <code>url</code> is a
string, it is automatically parsed with <a href="url.html#new-urlinput-base"><code>new URL()</code></a>. If it is a <a href="url.html#the-whatwg-url-api"><code>URL</code></a>
object, it will be automatically converted to an ordinary <code>options</code> object.</p>
<p>If both <code>url</code> and <code>options</code> are specified, the objects are merged, with the
<code>options</code> properties taking precedence.</p>
<p>The optional <code>callback</code> parameter will be added as a one-time listener for
the <a href="#event-response"><code>'response'</code></a> event.</p>
<p><code>http.request()</code> returns an instance of the <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a>
class. The <code>ClientRequest</code> instance is a writable stream. If one needs to
upload a file with a POST request, then write to the <code>ClientRequest</code> object.</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> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
<span class="hljs-keyword">const</span> postData = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({
<span class="hljs-string">'msg'</span>: <span class="hljs-string">'Hello World!'</span>,
});
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">hostname</span>: <span class="hljs-string">'www.google.com'</span>,
<span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
<span class="hljs-attr">path</span>: <span class="hljs-string">'/upload'</span>,
<span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>,
<span class="hljs-attr">headers</span>: {
<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>,
<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(postData),
},
};
<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`STATUS: <span class="hljs-subst">${res.statusCode}</span>`</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`HEADERS: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(res.headers)}</span>`</span>);
res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
res.<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-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`BODY: <span class="hljs-subst">${chunk}</span>`</span>);
});
res.<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">'No more data in response.'</span>);
});
});
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`problem with request: <span class="hljs-subst">${e.message}</span>`</span>);
});
<span class="hljs-comment">// Write data to request body</span>
req.<span class="hljs-title function_">write</span>(postData);
req.<span class="hljs-title function_">end</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">const</span> postData = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({
<span class="hljs-string">'msg'</span>: <span class="hljs-string">'Hello World!'</span>,
});
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">hostname</span>: <span class="hljs-string">'www.google.com'</span>,
<span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
<span class="hljs-attr">path</span>: <span class="hljs-string">'/upload'</span>,
<span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>,
<span class="hljs-attr">headers</span>: {
<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>,
<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(postData),
},
};
<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`STATUS: <span class="hljs-subst">${res.statusCode}</span>`</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`HEADERS: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(res.headers)}</span>`</span>);
res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
res.<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-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`BODY: <span class="hljs-subst">${chunk}</span>`</span>);
});
res.<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">'No more data in response.'</span>);
});
});
req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`problem with request: <span class="hljs-subst">${e.message}</span>`</span>);
});
<span class="hljs-comment">// Write data to request body</span>
req.<span class="hljs-title function_">write</span>(postData);
req.<span class="hljs-title function_">end</span>();</code><button class="copy-button">copy</button></pre>
<p>In the example <code>req.end()</code> was called. With <code>http.request()</code> one
must always call <code>req.end()</code> to signify the end of the request -
even if there is no data being written to the request body.</p>
<p>If any error is encountered during the request (be that with DNS resolution,
TCP level errors, or actual HTTP parse errors) an <code>'error'</code> event is emitted
on the returned request object. As with all <code>'error'</code> events, if no listeners
are registered the error will be thrown.</p>
<p>There are a few special headers that should be noted.</p>
<ul>
<li>
<p>Sending a 'Connection: keep-alive' will notify Node.js that the connection to
the server should be persisted until the next request.</p>
</li>
<li>
<p>Sending a 'Content-Length' header will disable the default chunked encoding.</p>
</li>
<li>
<p>Sending an 'Expect' header will immediately send the request headers.
Usually, when sending 'Expect: 100-continue', both a timeout and a listener
for the <code>'continue'</code> event should be set. See RFC 2616 Section 8.2.3 for more
information.</p>
</li>
<li>
<p>Sending an Authorization header will override using the <code>auth</code> option
to compute basic authentication.</p>
</li>
</ul>
<p>Example using a <a href="url.html#the-whatwg-url-api"><code>URL</code></a> as <code>options</code>:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> options = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'http://abc:xyz@example.com'</span>);
<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
<span class="hljs-comment">// ...</span>
});</code> <button class="copy-button">copy</button></pre>
<p>In a successful request, the following events will be emitted in the following
order:</p>
<ul>
<li><code>'socket'</code></li>
<li><code>'response'</code>
<ul>
<li><code>'data'</code> any number of times, on the <code>res</code> object
(<code>'data'</code> will not be emitted at all if the response body is empty, for
instance, in most redirects)</li>
<li><code>'end'</code> on the <code>res</code> object</li>
</ul>
</li>
<li><code>'close'</code></li>
</ul>
<p>In the case of a connection error, the following events will be emitted:</p>
<ul>
<li><code>'socket'</code></li>
<li><code>'error'</code></li>
<li><code>'close'</code></li>
</ul>
<p>In the case of a premature connection close before the response is received,
the following events will be emitted in the following order:</p>
<ul>
<li><code>'socket'</code></li>
<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code
<code>'ECONNRESET'</code></li>
<li><code>'close'</code></li>
</ul>
<p>In the case of a premature connection close after the response is received,
the following events will be emitted in the following order:</p>
<ul>
<li><code>'socket'</code></li>
<li><code>'response'</code>
<ul>
<li><code>'data'</code> any number of times, on the <code>res</code> object</li>
</ul>
</li>
<li>(connection closed here)</li>
<li><code>'aborted'</code> on the <code>res</code> object</li>
<li><code>'close'</code></li>
<li><code>'error'</code> on the <code>res</code> object with an error with message
<code>'Error: aborted'</code> and code <code>'ECONNRESET'</code></li>
<li><code>'close'</code> on the <code>res</code> object</li>
</ul>
<p>If <code>req.destroy()</code> is called before a socket is assigned, the following
events will be emitted in the following order:</p>
<ul>
<li>(<code>req.destroy()</code> called here)</li>
<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code
<code>'ECONNRESET'</code>, or the error with which <code>req.destroy()</code> was called</li>
<li><code>'close'</code></li>
</ul>
<p>If <code>req.destroy()</code> is called before the connection succeeds, the following
events will be emitted in the following order:</p>
<ul>
<li><code>'socket'</code></li>
<li>(<code>req.destroy()</code> called here)</li>
<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code
<code>'ECONNRESET'</code>, or the error with which <code>req.destroy()</code> was called</li>
<li><code>'close'</code></li>
</ul>
<p>If <code>req.destroy()</code> is called after the response is received, the following
events will be emitted in the following order:</p>
<ul>
<li><code>'socket'</code></li>
<li><code>'response'</code>
<ul>
<li><code>'data'</code> any number of times, on the <code>res</code> object</li>
</ul>
</li>
<li>(<code>req.destroy()</code> called here)</li>
<li><code>'aborted'</code> on the <code>res</code> object</li>
<li><code>'close'</code></li>
<li><code>'error'</code> on the <code>res</code> object with an error with message <code>'Error: aborted'</code>
and code <code>'ECONNRESET'</code>, or the error with which <code>req.destroy()</code> was called</li>
<li><code>'close'</code> on the <code>res</code> object</li>
</ul>
<p>If <code>req.abort()</code> is called before a socket is assigned, the following
events will be emitted in the following order:</p>
<ul>
<li>(<code>req.abort()</code> called here)</li>
<li><code>'abort'</code></li>
<li><code>'close'</code></li>
</ul>
<p>If <code>req.abort()</code> is called before the connection succeeds, the following
events will be emitted in the following order:</p>
<ul>
<li><code>'socket'</code></li>
<li>(<code>req.abort()</code> called here)</li>
<li><code>'abort'</code></li>
<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code
<code>'ECONNRESET'</code></li>
<li><code>'close'</code></li>
</ul>
<p>If <code>req.abort()</code> is called after the response is received, the following
events will be emitted in the following order:</p>
<ul>
<li><code>'socket'</code></li>
<li><code>'response'</code>
<ul>
<li><code>'data'</code> any number of times, on the <code>res</code> object</li>
</ul>
</li>
<li>(<code>req.abort()</code> called here)</li>
<li><code>'abort'</code></li>
<li><code>'aborted'</code> on the <code>res</code> object</li>
<li><code>'error'</code> on the <code>res</code> object with an error with message
<code>'Error: aborted'</code> and code <code>'ECONNRESET'</code>.</li>
<li><code>'close'</code></li>
<li><code>'close'</code> on the <code>res</code> object</li>
</ul>
<p>Setting the <code>timeout</code> option or using the <code>setTimeout()</code> function will
not abort the request or do anything besides add a <code>'timeout'</code> event.</p>
<p>Passing an <code>AbortSignal</code> and then calling <code>abort()</code> on the corresponding
<code>AbortController</code> will behave the same way as calling <code>.destroy()</code> on the
request. Specifically, the <code>'error'</code> event will be emitted with an error with
the message <code>'AbortError: The operation was aborted'</code>, the code <code>'ABORT_ERR'</code>
and the <code>cause</code>, if one was provided.</p>
</section><section><h3><code>http.validateHeaderName(name[, label])</code><span><a class="mark" href="#httpvalidateheadernamename-label" id="httpvalidateheadernamename-label">#</a></span><a aria-hidden="true" class="legacy" id="http_http_validateheadername_name_label"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.5.0, v18.14.0</td>
<td><p>The <code>label</code> parameter is added.</p></td></tr>
<tr><td>v14.3.0</td>
<td><p><span>Added in: v14.3.0</span></p></td></tr>
</tbody></table>
</details>
</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>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Label for error message. <strong>Default:</strong> <code>'Header name'</code>.</li>
</ul>
<p>Performs the low-level validations on the provided <code>name</code> that are done when
<code>res.setHeader(name, value)</code> is called.</p>
<p>Passing illegal value as <code>name</code> will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown,
identified by <code>code: 'ERR_INVALID_HTTP_TOKEN'</code>.</p>
<p>It is not necessary to use this method before passing headers to an HTTP request
or response. The HTTP module will automatically validate such headers.</p>
<p>Example:</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> { validateHeaderName } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">validateHeaderName</span>(<span class="hljs-string">''</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>); <span class="hljs-comment">// --> true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">code</span>); <span class="hljs-comment">// --> 'ERR_INVALID_HTTP_TOKEN'</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); <span class="hljs-comment">// --> 'Header name must be a valid HTTP token [""]'</span>
}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { validateHeaderName } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">validateHeaderName</span>(<span class="hljs-string">''</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>); <span class="hljs-comment">// --> true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">code</span>); <span class="hljs-comment">// --> 'ERR_INVALID_HTTP_TOKEN'</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); <span class="hljs-comment">// --> 'Header name must be a valid HTTP token [""]'</span>
}</code><button class="copy-button">copy</button></pre>
</section><section><h3><code>http.validateHeaderValue(name, value)</code><span><a class="mark" href="#httpvalidateheadervaluename-value" id="httpvalidateheadervaluename-value">#</a></span><a aria-hidden="true" class="legacy" id="http_http_validateheadervalue_name_value"></a></h3>
<div class="api_metadata">
<span>Added in: v14.3.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#Data_types" class="type"><any></a></li>
</ul>
<p>Performs the low-level validations on the provided <code>value</code> that are done when
<code>res.setHeader(name, value)</code> is called.</p>
<p>Passing illegal value as <code>value</code> will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p>
<ul>
<li>Undefined value error is identified by <code>code: 'ERR_HTTP_INVALID_HEADER_VALUE'</code>.</li>
<li>Invalid value character error is identified by <code>code: 'ERR_INVALID_CHAR'</code>.</li>
</ul>
<p>It is not necessary to use this method before passing headers to an HTTP request
or response. The HTTP module will automatically validate such headers.</p>
<p>Examples:</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> { validateHeaderValue } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-literal">undefined</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>); <span class="hljs-comment">// --> true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">code</span> === <span class="hljs-string">'ERR_HTTP_INVALID_HEADER_VALUE'</span>); <span class="hljs-comment">// --> true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); <span class="hljs-comment">// --> 'Invalid value "undefined" for header "x-my-header"'</span>
}
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-string">'oʊmɪɡə'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>); <span class="hljs-comment">// --> true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">code</span> === <span class="hljs-string">'ERR_INVALID_CHAR'</span>); <span class="hljs-comment">// --> true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); <span class="hljs-comment">// --> 'Invalid character in header content ["x-my-header"]'</span>
}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { validateHeaderValue } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-literal">undefined</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>); <span class="hljs-comment">// --> true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">code</span> === <span class="hljs-string">'ERR_HTTP_INVALID_HEADER_VALUE'</span>); <span class="hljs-comment">// --> true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); <span class="hljs-comment">// --> 'Invalid value "undefined" for header "x-my-header"'</span>
}
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-string">'oʊmɪɡə'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>); <span class="hljs-comment">// --> true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">code</span> === <span class="hljs-string">'ERR_INVALID_CHAR'</span>); <span class="hljs-comment">// --> true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); <span class="hljs-comment">// --> 'Invalid character in header content ["x-my-header"]'</span>
}</code><button class="copy-button">copy</button></pre>
</section><section><h3><code>http.setMaxIdleHTTPParsers(max)</code><span><a class="mark" href="#httpsetmaxidlehttpparsersmax" id="httpsetmaxidlehttpparsersmax">#</a></span><a aria-hidden="true" class="legacy" id="http_http_setmaxidlehttpparsers_max"></a></h3>
<div class="api_metadata">
<span>Added in: v18.8.0, v16.18.0</span>
</div>
<ul>
<li><code>max</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>1000</code>.</li>
</ul>
<p>Set the maximum number of idle HTTP parsers.</p>
</section><section><h3><code>WebSocket</code><span><a class="mark" href="#websocket" id="websocket">#</a></span><a aria-hidden="true" class="legacy" id="http_websocket"></a></h3>
<div class="api_metadata">
<span>Added in: v22.5.0</span>
</div>
<p>A browser-compatible implementation of <a href="#websocket"><code>WebSocket</code></a>.</p></section>
<!-- API END -->
</div>
</div>
</div>
</body>
</html>
|