11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>HTTP | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/http.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci <style>@media(max-width:694px){.with-59-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:622px){.with-50-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:510px){.with-36-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:502px){.with-35-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:494px){.with-34-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:606px){.with-48-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:518px){.with-37-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:638px){.with-52-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:646px){.with-53-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-http"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http active">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="http" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <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"/> 1041cb0ef41Sopenharmony_ci <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"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <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"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#http">HTTP</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#class-httpagent">Class: <code>http.Agent</code></a> 1261cb0ef41Sopenharmony_ci<ul> 1271cb0ef41Sopenharmony_ci<li><a href="#new-agentoptions"><code>new Agent([options])</code></a></li> 1281cb0ef41Sopenharmony_ci<li><a href="#agentcreateconnectionoptions-callback"><code>agent.createConnection(options[, callback])</code></a></li> 1291cb0ef41Sopenharmony_ci<li><a href="#agentkeepsocketalivesocket"><code>agent.keepSocketAlive(socket)</code></a></li> 1301cb0ef41Sopenharmony_ci<li><a href="#agentreusesocketsocket-request"><code>agent.reuseSocket(socket, request)</code></a></li> 1311cb0ef41Sopenharmony_ci<li><a href="#agentdestroy"><code>agent.destroy()</code></a></li> 1321cb0ef41Sopenharmony_ci<li><a href="#agentfreesockets"><code>agent.freeSockets</code></a></li> 1331cb0ef41Sopenharmony_ci<li><a href="#agentgetnameoptions"><code>agent.getName([options])</code></a></li> 1341cb0ef41Sopenharmony_ci<li><a href="#agentmaxfreesockets"><code>agent.maxFreeSockets</code></a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#agentmaxsockets"><code>agent.maxSockets</code></a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#agentmaxtotalsockets"><code>agent.maxTotalSockets</code></a></li> 1371cb0ef41Sopenharmony_ci<li><a href="#agentrequests"><code>agent.requests</code></a></li> 1381cb0ef41Sopenharmony_ci<li><a href="#agentsockets"><code>agent.sockets</code></a></li> 1391cb0ef41Sopenharmony_ci</ul> 1401cb0ef41Sopenharmony_ci</li> 1411cb0ef41Sopenharmony_ci<li><a href="#class-httpclientrequest">Class: <code>http.ClientRequest</code></a> 1421cb0ef41Sopenharmony_ci<ul> 1431cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#event-abort">Event: <code>'abort'</code></a></span></li> 1441cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li> 1451cb0ef41Sopenharmony_ci<li><a href="#event-connect">Event: <code>'connect'</code></a></li> 1461cb0ef41Sopenharmony_ci<li><a href="#event-continue">Event: <code>'continue'</code></a></li> 1471cb0ef41Sopenharmony_ci<li><a href="#event-finish">Event: <code>'finish'</code></a></li> 1481cb0ef41Sopenharmony_ci<li><a href="#event-information">Event: <code>'information'</code></a></li> 1491cb0ef41Sopenharmony_ci<li><a href="#event-response">Event: <code>'response'</code></a></li> 1501cb0ef41Sopenharmony_ci<li><a href="#event-socket">Event: <code>'socket'</code></a></li> 1511cb0ef41Sopenharmony_ci<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li> 1521cb0ef41Sopenharmony_ci<li><a href="#event-upgrade">Event: <code>'upgrade'</code></a></li> 1531cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requestabort"><code>request.abort()</code></a></span></li> 1541cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requestaborted"><code>request.aborted</code></a></span></li> 1551cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li> 1561cb0ef41Sopenharmony_ci<li><a href="#requestcork"><code>request.cork()</code></a></li> 1571cb0ef41Sopenharmony_ci<li><a href="#requestenddata-encoding-callback"><code>request.end([data[, encoding]][, callback])</code></a></li> 1581cb0ef41Sopenharmony_ci<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a> 1591cb0ef41Sopenharmony_ci<ul> 1601cb0ef41Sopenharmony_ci<li><a href="#requestdestroyed"><code>request.destroyed</code></a></li> 1611cb0ef41Sopenharmony_ci</ul> 1621cb0ef41Sopenharmony_ci</li> 1631cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requestfinished"><code>request.finished</code></a></span></li> 1641cb0ef41Sopenharmony_ci<li><a href="#requestflushheaders"><code>request.flushHeaders()</code></a></li> 1651cb0ef41Sopenharmony_ci<li><a href="#requestgetheadername"><code>request.getHeader(name)</code></a></li> 1661cb0ef41Sopenharmony_ci<li><a href="#requestgetheadernames"><code>request.getHeaderNames()</code></a></li> 1671cb0ef41Sopenharmony_ci<li><a href="#requestgetheaders"><code>request.getHeaders()</code></a></li> 1681cb0ef41Sopenharmony_ci<li><a href="#requestgetrawheadernames"><code>request.getRawHeaderNames()</code></a></li> 1691cb0ef41Sopenharmony_ci<li><a href="#requesthasheadername"><code>request.hasHeader(name)</code></a></li> 1701cb0ef41Sopenharmony_ci<li><a href="#requestmaxheaderscount"><code>request.maxHeadersCount</code></a></li> 1711cb0ef41Sopenharmony_ci<li><a href="#requestpath"><code>request.path</code></a></li> 1721cb0ef41Sopenharmony_ci<li><a href="#requestmethod"><code>request.method</code></a></li> 1731cb0ef41Sopenharmony_ci<li><a href="#requesthost"><code>request.host</code></a></li> 1741cb0ef41Sopenharmony_ci<li><a href="#requestprotocol"><code>request.protocol</code></a></li> 1751cb0ef41Sopenharmony_ci<li><a href="#requestremoveheadername"><code>request.removeHeader(name)</code></a></li> 1761cb0ef41Sopenharmony_ci<li><a href="#requestreusedsocket"><code>request.reusedSocket</code></a></li> 1771cb0ef41Sopenharmony_ci<li><a href="#requestsetheadername-value"><code>request.setHeader(name, value)</code></a></li> 1781cb0ef41Sopenharmony_ci<li><a href="#requestsetnodelaynodelay"><code>request.setNoDelay([noDelay])</code></a></li> 1791cb0ef41Sopenharmony_ci<li><a href="#requestsetsocketkeepaliveenable-initialdelay"><code>request.setSocketKeepAlive([enable][, initialDelay])</code></a></li> 1801cb0ef41Sopenharmony_ci<li><a href="#requestsettimeouttimeout-callback"><code>request.setTimeout(timeout[, callback])</code></a></li> 1811cb0ef41Sopenharmony_ci<li><a href="#requestsocket"><code>request.socket</code></a></li> 1821cb0ef41Sopenharmony_ci<li><a href="#requestuncork"><code>request.uncork()</code></a></li> 1831cb0ef41Sopenharmony_ci<li><a href="#requestwritableended"><code>request.writableEnded</code></a></li> 1841cb0ef41Sopenharmony_ci<li><a href="#requestwritablefinished"><code>request.writableFinished</code></a></li> 1851cb0ef41Sopenharmony_ci<li><a href="#requestwritechunk-encoding-callback"><code>request.write(chunk[, encoding][, callback])</code></a></li> 1861cb0ef41Sopenharmony_ci</ul> 1871cb0ef41Sopenharmony_ci</li> 1881cb0ef41Sopenharmony_ci<li><a href="#class-httpserver">Class: <code>http.Server</code></a> 1891cb0ef41Sopenharmony_ci<ul> 1901cb0ef41Sopenharmony_ci<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li> 1911cb0ef41Sopenharmony_ci<li><a href="#event-checkexpectation">Event: <code>'checkExpectation'</code></a></li> 1921cb0ef41Sopenharmony_ci<li><a href="#event-clienterror">Event: <code>'clientError'</code></a></li> 1931cb0ef41Sopenharmony_ci<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 1941cb0ef41Sopenharmony_ci<li><a href="#event-connect_1">Event: <code>'connect'</code></a></li> 1951cb0ef41Sopenharmony_ci<li><a href="#event-connection">Event: <code>'connection'</code></a></li> 1961cb0ef41Sopenharmony_ci<li><a href="#event-droprequest">Event: <code>'dropRequest'</code></a></li> 1971cb0ef41Sopenharmony_ci<li><a href="#event-request">Event: <code>'request'</code></a></li> 1981cb0ef41Sopenharmony_ci<li><a href="#event-upgrade_1">Event: <code>'upgrade'</code></a></li> 1991cb0ef41Sopenharmony_ci<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li> 2001cb0ef41Sopenharmony_ci<li><a href="#servercloseallconnections"><code>server.closeAllConnections()</code></a></li> 2011cb0ef41Sopenharmony_ci<li><a href="#servercloseidleconnections"><code>server.closeIdleConnections()</code></a></li> 2021cb0ef41Sopenharmony_ci<li><a href="#serverheaderstimeout"><code>server.headersTimeout</code></a></li> 2031cb0ef41Sopenharmony_ci<li><a href="#serverlisten"><code>server.listen()</code></a></li> 2041cb0ef41Sopenharmony_ci<li><a href="#serverlistening"><code>server.listening</code></a></li> 2051cb0ef41Sopenharmony_ci<li><a href="#servermaxheaderscount"><code>server.maxHeadersCount</code></a></li> 2061cb0ef41Sopenharmony_ci<li><a href="#serverrequesttimeout"><code>server.requestTimeout</code></a></li> 2071cb0ef41Sopenharmony_ci<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li> 2081cb0ef41Sopenharmony_ci<li><a href="#servermaxrequestspersocket"><code>server.maxRequestsPerSocket</code></a></li> 2091cb0ef41Sopenharmony_ci<li><a href="#servertimeout"><code>server.timeout</code></a></li> 2101cb0ef41Sopenharmony_ci<li><a href="#serverkeepalivetimeout"><code>server.keepAliveTimeout</code></a></li> 2111cb0ef41Sopenharmony_ci</ul> 2121cb0ef41Sopenharmony_ci</li> 2131cb0ef41Sopenharmony_ci<li><a href="#class-httpserverresponse">Class: <code>http.ServerResponse</code></a> 2141cb0ef41Sopenharmony_ci<ul> 2151cb0ef41Sopenharmony_ci<li><a href="#event-close_2">Event: <code>'close'</code></a></li> 2161cb0ef41Sopenharmony_ci<li><a href="#event-finish_1">Event: <code>'finish'</code></a></li> 2171cb0ef41Sopenharmony_ci<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li> 2181cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li> 2191cb0ef41Sopenharmony_ci<li><a href="#responsecork"><code>response.cork()</code></a></li> 2201cb0ef41Sopenharmony_ci<li><a href="#responseenddata-encoding-callback"><code>response.end([data[, encoding]][, callback])</code></a></li> 2211cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#responsefinished"><code>response.finished</code></a></span></li> 2221cb0ef41Sopenharmony_ci<li><a href="#responseflushheaders"><code>response.flushHeaders()</code></a></li> 2231cb0ef41Sopenharmony_ci<li><a href="#responsegetheadername"><code>response.getHeader(name)</code></a></li> 2241cb0ef41Sopenharmony_ci<li><a href="#responsegetheadernames"><code>response.getHeaderNames()</code></a></li> 2251cb0ef41Sopenharmony_ci<li><a href="#responsegetheaders"><code>response.getHeaders()</code></a></li> 2261cb0ef41Sopenharmony_ci<li><a href="#responsehasheadername"><code>response.hasHeader(name)</code></a></li> 2271cb0ef41Sopenharmony_ci<li><a href="#responseheaderssent"><code>response.headersSent</code></a></li> 2281cb0ef41Sopenharmony_ci<li><a href="#responseremoveheadername"><code>response.removeHeader(name)</code></a></li> 2291cb0ef41Sopenharmony_ci<li><a href="#responsereq"><code>response.req</code></a></li> 2301cb0ef41Sopenharmony_ci<li><a href="#responsesenddate"><code>response.sendDate</code></a></li> 2311cb0ef41Sopenharmony_ci<li><a href="#responsesetheadername-value"><code>response.setHeader(name, value)</code></a></li> 2321cb0ef41Sopenharmony_ci<li><a href="#responsesettimeoutmsecs-callback"><code>response.setTimeout(msecs[, callback])</code></a></li> 2331cb0ef41Sopenharmony_ci<li><a href="#responsesocket"><code>response.socket</code></a></li> 2341cb0ef41Sopenharmony_ci<li><a href="#responsestatuscode"><code>response.statusCode</code></a></li> 2351cb0ef41Sopenharmony_ci<li><a href="#responsestatusmessage"><code>response.statusMessage</code></a></li> 2361cb0ef41Sopenharmony_ci<li><a href="#responsestrictcontentlength"><code>response.strictContentLength</code></a></li> 2371cb0ef41Sopenharmony_ci<li><a href="#responseuncork"><code>response.uncork()</code></a></li> 2381cb0ef41Sopenharmony_ci<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li> 2391cb0ef41Sopenharmony_ci<li><a href="#responsewritablefinished"><code>response.writableFinished</code></a></li> 2401cb0ef41Sopenharmony_ci<li><a href="#responsewritechunk-encoding-callback"><code>response.write(chunk[, encoding][, callback])</code></a></li> 2411cb0ef41Sopenharmony_ci<li><a href="#responsewritecontinue"><code>response.writeContinue()</code></a></li> 2421cb0ef41Sopenharmony_ci<li><a href="#responsewriteearlyhintshints-callback"><code>response.writeEarlyHints(hints[, callback])</code></a></li> 2431cb0ef41Sopenharmony_ci<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li> 2441cb0ef41Sopenharmony_ci<li><a href="#responsewriteprocessing"><code>response.writeProcessing()</code></a></li> 2451cb0ef41Sopenharmony_ci</ul> 2461cb0ef41Sopenharmony_ci</li> 2471cb0ef41Sopenharmony_ci<li><a href="#class-httpincomingmessage">Class: <code>http.IncomingMessage</code></a> 2481cb0ef41Sopenharmony_ci<ul> 2491cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#event-aborted">Event: <code>'aborted'</code></a></span></li> 2501cb0ef41Sopenharmony_ci<li><a href="#event-close_3">Event: <code>'close'</code></a></li> 2511cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#messageaborted"><code>message.aborted</code></a></span></li> 2521cb0ef41Sopenharmony_ci<li><a href="#messagecomplete"><code>message.complete</code></a></li> 2531cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#messageconnection"><code>message.connection</code></a></span></li> 2541cb0ef41Sopenharmony_ci<li><a href="#messagedestroyerror"><code>message.destroy([error])</code></a></li> 2551cb0ef41Sopenharmony_ci<li><a href="#messageheaders"><code>message.headers</code></a></li> 2561cb0ef41Sopenharmony_ci<li><a href="#messageheadersdistinct"><code>message.headersDistinct</code></a></li> 2571cb0ef41Sopenharmony_ci<li><a href="#messagehttpversion"><code>message.httpVersion</code></a></li> 2581cb0ef41Sopenharmony_ci<li><a href="#messagemethod"><code>message.method</code></a></li> 2591cb0ef41Sopenharmony_ci<li><a href="#messagerawheaders"><code>message.rawHeaders</code></a></li> 2601cb0ef41Sopenharmony_ci<li><a href="#messagerawtrailers"><code>message.rawTrailers</code></a></li> 2611cb0ef41Sopenharmony_ci<li><a href="#messagesettimeoutmsecs-callback"><code>message.setTimeout(msecs[, callback])</code></a></li> 2621cb0ef41Sopenharmony_ci<li><a href="#messagesocket"><code>message.socket</code></a></li> 2631cb0ef41Sopenharmony_ci<li><a href="#messagestatuscode"><code>message.statusCode</code></a></li> 2641cb0ef41Sopenharmony_ci<li><a href="#messagestatusmessage"><code>message.statusMessage</code></a></li> 2651cb0ef41Sopenharmony_ci<li><a href="#messagetrailers"><code>message.trailers</code></a></li> 2661cb0ef41Sopenharmony_ci<li><a href="#messagetrailersdistinct"><code>message.trailersDistinct</code></a></li> 2671cb0ef41Sopenharmony_ci<li><a href="#messageurl"><code>message.url</code></a></li> 2681cb0ef41Sopenharmony_ci</ul> 2691cb0ef41Sopenharmony_ci</li> 2701cb0ef41Sopenharmony_ci<li><a href="#class-httpoutgoingmessage">Class: <code>http.OutgoingMessage</code></a> 2711cb0ef41Sopenharmony_ci<ul> 2721cb0ef41Sopenharmony_ci<li><a href="#event-drain">Event: <code>'drain'</code></a></li> 2731cb0ef41Sopenharmony_ci<li><a href="#event-finish_2">Event: <code>'finish'</code></a></li> 2741cb0ef41Sopenharmony_ci<li><a href="#event-prefinish">Event: <code>'prefinish'</code></a></li> 2751cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageaddtrailersheaders"><code>outgoingMessage.addTrailers(headers)</code></a></li> 2761cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageappendheadername-value"><code>outgoingMessage.appendHeader(name, value)</code></a></li> 2771cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#outgoingmessageconnection"><code>outgoingMessage.connection</code></a></span></li> 2781cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagecork"><code>outgoingMessage.cork()</code></a></li> 2791cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagedestroyerror"><code>outgoingMessage.destroy([error])</code></a></li> 2801cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageendchunk-encoding-callback"><code>outgoingMessage.end(chunk[, encoding][, callback])</code></a></li> 2811cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageflushheaders"><code>outgoingMessage.flushHeaders()</code></a></li> 2821cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagegetheadername"><code>outgoingMessage.getHeader(name)</code></a></li> 2831cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagegetheadernames"><code>outgoingMessage.getHeaderNames()</code></a></li> 2841cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagegetheaders"><code>outgoingMessage.getHeaders()</code></a></li> 2851cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagehasheadername"><code>outgoingMessage.hasHeader(name)</code></a></li> 2861cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageheaderssent"><code>outgoingMessage.headersSent</code></a></li> 2871cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagepipe"><code>outgoingMessage.pipe()</code></a></li> 2881cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageremoveheadername"><code>outgoingMessage.removeHeader(name)</code></a></li> 2891cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagesetheadername-value"><code>outgoingMessage.setHeader(name, value)</code></a></li> 2901cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagesetheadersheaders"><code>outgoingMessage.setHeaders(headers)</code></a></li> 2911cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagesettimeoutmsesc-callback"><code>outgoingMessage.setTimeout(msesc[, callback])</code></a></li> 2921cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagesocket"><code>outgoingMessage.socket</code></a></li> 2931cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageuncork"><code>outgoingMessage.uncork()</code></a></li> 2941cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritablecorked"><code>outgoingMessage.writableCorked</code></a></li> 2951cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritableended"><code>outgoingMessage.writableEnded</code></a></li> 2961cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritablefinished"><code>outgoingMessage.writableFinished</code></a></li> 2971cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritablehighwatermark"><code>outgoingMessage.writableHighWaterMark</code></a></li> 2981cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritablelength"><code>outgoingMessage.writableLength</code></a></li> 2991cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritableobjectmode"><code>outgoingMessage.writableObjectMode</code></a></li> 3001cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritechunk-encoding-callback"><code>outgoingMessage.write(chunk[, encoding][, callback])</code></a></li> 3011cb0ef41Sopenharmony_ci</ul> 3021cb0ef41Sopenharmony_ci</li> 3031cb0ef41Sopenharmony_ci<li><a href="#httpmethods"><code>http.METHODS</code></a></li> 3041cb0ef41Sopenharmony_ci<li><a href="#httpstatus_codes"><code>http.STATUS_CODES</code></a></li> 3051cb0ef41Sopenharmony_ci<li><a href="#httpcreateserveroptions-requestlistener"><code>http.createServer([options][, requestListener])</code></a></li> 3061cb0ef41Sopenharmony_ci<li><a href="#httpgetoptions-callback"><code>http.get(options[, callback])</code></a></li> 3071cb0ef41Sopenharmony_ci<li><a href="#httpgeturl-options-callback"><code>http.get(url[, options][, callback])</code></a></li> 3081cb0ef41Sopenharmony_ci<li><a href="#httpglobalagent"><code>http.globalAgent</code></a></li> 3091cb0ef41Sopenharmony_ci<li><a href="#httpmaxheadersize"><code>http.maxHeaderSize</code></a></li> 3101cb0ef41Sopenharmony_ci<li><a href="#httprequestoptions-callback"><code>http.request(options[, callback])</code></a></li> 3111cb0ef41Sopenharmony_ci<li><a href="#httprequesturl-options-callback"><code>http.request(url[, options][, callback])</code></a></li> 3121cb0ef41Sopenharmony_ci<li><a href="#httpvalidateheadernamename-label"><code>http.validateHeaderName(name[, label])</code></a></li> 3131cb0ef41Sopenharmony_ci<li><a href="#httpvalidateheadervaluename-value"><code>http.validateHeaderValue(name, value)</code></a></li> 3141cb0ef41Sopenharmony_ci<li><a href="#httpsetmaxidlehttpparsersmax"><code>http.setMaxIdleHTTPParsers(max)</code></a></li> 3151cb0ef41Sopenharmony_ci</ul> 3161cb0ef41Sopenharmony_ci</li> 3171cb0ef41Sopenharmony_ci</ul></div></div> 3181cb0ef41Sopenharmony_ci </li> 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ci 3211cb0ef41Sopenharmony_ci <li class="picker-header"> 3221cb0ef41Sopenharmony_ci <a href="#"> 3231cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3241cb0ef41Sopenharmony_ci Index 3251cb0ef41Sopenharmony_ci </a> 3261cb0ef41Sopenharmony_ci 3271cb0ef41Sopenharmony_ci <div class="picker"><ul> 3281cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 3291cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 3301cb0ef41Sopenharmony_ci 3311cb0ef41Sopenharmony_ci <li> 3321cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 3331cb0ef41Sopenharmony_ci </li> 3341cb0ef41Sopenharmony_ci </ul> 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ci<hr class="line"> 3371cb0ef41Sopenharmony_ci<ul> 3381cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 3391cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 3401cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 3411cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 3421cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 3431cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 3441cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 3451cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 3461cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 3471cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 3481cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 3491cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 3501cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 3511cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 3521cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 3531cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 3541cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 3551cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 3561cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 3571cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 3581cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 3591cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 3601cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http active">HTTP</a></li> 3611cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 3621cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 3631cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 3641cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 3651cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 3661cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 3671cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 3681cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 3691cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 3701cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 3711cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 3721cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 3731cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 3741cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 3751cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 3761cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 3771cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 3781cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 3791cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 3801cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 3811cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 3821cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 3831cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 3841cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 3851cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 3861cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 3871cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 3881cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 3891cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 3901cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 3911cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 3921cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 3931cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 3941cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 3951cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 3961cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 3971cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 3981cb0ef41Sopenharmony_ci</ul> 3991cb0ef41Sopenharmony_ci<hr class="line"> 4001cb0ef41Sopenharmony_ci<ul> 4011cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 4021cb0ef41Sopenharmony_ci</ul></div> 4031cb0ef41Sopenharmony_ci </li> 4041cb0ef41Sopenharmony_ci 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_ci <li class="picker-header"> 4071cb0ef41Sopenharmony_ci <a href="#"> 4081cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 4091cb0ef41Sopenharmony_ci Other versions 4101cb0ef41Sopenharmony_ci </a> 4111cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/http.html">21.x</a></li> 4121cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/http.html">20.x <b>LTS</b></a></li> 4131cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/http.html">19.x</a></li> 4141cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/http.html">18.x <b>LTS</b></a></li> 4151cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/http.html">17.x</a></li> 4161cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/http.html">16.x</a></li> 4171cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/http.html">15.x</a></li> 4181cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/http.html">14.x</a></li> 4191cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/http.html">13.x</a></li> 4201cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/http.html">12.x</a></li> 4211cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/http.html">11.x</a></li> 4221cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/http.html">10.x</a></li> 4231cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/http.html">9.x</a></li> 4241cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/http.html">8.x</a></li> 4251cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/http.html">7.x</a></li> 4261cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/http.html">6.x</a></li> 4271cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/http.html">5.x</a></li> 4281cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/http.html">4.x</a></li> 4291cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/http.html">0.12.x</a></li> 4301cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/http.html">0.10.x</a></li></ol></div> 4311cb0ef41Sopenharmony_ci </li> 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_ci <li class="picker-header"> 4341cb0ef41Sopenharmony_ci <a href="#"> 4351cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 4361cb0ef41Sopenharmony_ci Options 4371cb0ef41Sopenharmony_ci </a> 4381cb0ef41Sopenharmony_ci 4391cb0ef41Sopenharmony_ci <div class="picker"> 4401cb0ef41Sopenharmony_ci <ul> 4411cb0ef41Sopenharmony_ci <li> 4421cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 4431cb0ef41Sopenharmony_ci </li> 4441cb0ef41Sopenharmony_ci <li> 4451cb0ef41Sopenharmony_ci <a href="http.json">View as JSON</a> 4461cb0ef41Sopenharmony_ci </li> 4471cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/http.md">Edit on GitHub</a></li> 4481cb0ef41Sopenharmony_ci </ul> 4491cb0ef41Sopenharmony_ci </div> 4501cb0ef41Sopenharmony_ci </li> 4511cb0ef41Sopenharmony_ci </ul> 4521cb0ef41Sopenharmony_ci </div> 4531cb0ef41Sopenharmony_ci <hr> 4541cb0ef41Sopenharmony_ci </header> 4551cb0ef41Sopenharmony_ci 4561cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 4571cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#http">HTTP</a></span> 4581cb0ef41Sopenharmony_ci<ul> 4591cb0ef41Sopenharmony_ci<li><a href="#class-httpagent">Class: <code>http.Agent</code></a> 4601cb0ef41Sopenharmony_ci<ul> 4611cb0ef41Sopenharmony_ci<li><a href="#new-agentoptions"><code>new Agent([options])</code></a></li> 4621cb0ef41Sopenharmony_ci<li><a href="#agentcreateconnectionoptions-callback"><code>agent.createConnection(options[, callback])</code></a></li> 4631cb0ef41Sopenharmony_ci<li><a href="#agentkeepsocketalivesocket"><code>agent.keepSocketAlive(socket)</code></a></li> 4641cb0ef41Sopenharmony_ci<li><a href="#agentreusesocketsocket-request"><code>agent.reuseSocket(socket, request)</code></a></li> 4651cb0ef41Sopenharmony_ci<li><a href="#agentdestroy"><code>agent.destroy()</code></a></li> 4661cb0ef41Sopenharmony_ci<li><a href="#agentfreesockets"><code>agent.freeSockets</code></a></li> 4671cb0ef41Sopenharmony_ci<li><a href="#agentgetnameoptions"><code>agent.getName([options])</code></a></li> 4681cb0ef41Sopenharmony_ci<li><a href="#agentmaxfreesockets"><code>agent.maxFreeSockets</code></a></li> 4691cb0ef41Sopenharmony_ci<li><a href="#agentmaxsockets"><code>agent.maxSockets</code></a></li> 4701cb0ef41Sopenharmony_ci<li><a href="#agentmaxtotalsockets"><code>agent.maxTotalSockets</code></a></li> 4711cb0ef41Sopenharmony_ci<li><a href="#agentrequests"><code>agent.requests</code></a></li> 4721cb0ef41Sopenharmony_ci<li><a href="#agentsockets"><code>agent.sockets</code></a></li> 4731cb0ef41Sopenharmony_ci</ul> 4741cb0ef41Sopenharmony_ci</li> 4751cb0ef41Sopenharmony_ci<li><a href="#class-httpclientrequest">Class: <code>http.ClientRequest</code></a> 4761cb0ef41Sopenharmony_ci<ul> 4771cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#event-abort">Event: <code>'abort'</code></a></span></li> 4781cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li> 4791cb0ef41Sopenharmony_ci<li><a href="#event-connect">Event: <code>'connect'</code></a></li> 4801cb0ef41Sopenharmony_ci<li><a href="#event-continue">Event: <code>'continue'</code></a></li> 4811cb0ef41Sopenharmony_ci<li><a href="#event-finish">Event: <code>'finish'</code></a></li> 4821cb0ef41Sopenharmony_ci<li><a href="#event-information">Event: <code>'information'</code></a></li> 4831cb0ef41Sopenharmony_ci<li><a href="#event-response">Event: <code>'response'</code></a></li> 4841cb0ef41Sopenharmony_ci<li><a href="#event-socket">Event: <code>'socket'</code></a></li> 4851cb0ef41Sopenharmony_ci<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li> 4861cb0ef41Sopenharmony_ci<li><a href="#event-upgrade">Event: <code>'upgrade'</code></a></li> 4871cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requestabort"><code>request.abort()</code></a></span></li> 4881cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requestaborted"><code>request.aborted</code></a></span></li> 4891cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li> 4901cb0ef41Sopenharmony_ci<li><a href="#requestcork"><code>request.cork()</code></a></li> 4911cb0ef41Sopenharmony_ci<li><a href="#requestenddata-encoding-callback"><code>request.end([data[, encoding]][, callback])</code></a></li> 4921cb0ef41Sopenharmony_ci<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a> 4931cb0ef41Sopenharmony_ci<ul> 4941cb0ef41Sopenharmony_ci<li><a href="#requestdestroyed"><code>request.destroyed</code></a></li> 4951cb0ef41Sopenharmony_ci</ul> 4961cb0ef41Sopenharmony_ci</li> 4971cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requestfinished"><code>request.finished</code></a></span></li> 4981cb0ef41Sopenharmony_ci<li><a href="#requestflushheaders"><code>request.flushHeaders()</code></a></li> 4991cb0ef41Sopenharmony_ci<li><a href="#requestgetheadername"><code>request.getHeader(name)</code></a></li> 5001cb0ef41Sopenharmony_ci<li><a href="#requestgetheadernames"><code>request.getHeaderNames()</code></a></li> 5011cb0ef41Sopenharmony_ci<li><a href="#requestgetheaders"><code>request.getHeaders()</code></a></li> 5021cb0ef41Sopenharmony_ci<li><a href="#requestgetrawheadernames"><code>request.getRawHeaderNames()</code></a></li> 5031cb0ef41Sopenharmony_ci<li><a href="#requesthasheadername"><code>request.hasHeader(name)</code></a></li> 5041cb0ef41Sopenharmony_ci<li><a href="#requestmaxheaderscount"><code>request.maxHeadersCount</code></a></li> 5051cb0ef41Sopenharmony_ci<li><a href="#requestpath"><code>request.path</code></a></li> 5061cb0ef41Sopenharmony_ci<li><a href="#requestmethod"><code>request.method</code></a></li> 5071cb0ef41Sopenharmony_ci<li><a href="#requesthost"><code>request.host</code></a></li> 5081cb0ef41Sopenharmony_ci<li><a href="#requestprotocol"><code>request.protocol</code></a></li> 5091cb0ef41Sopenharmony_ci<li><a href="#requestremoveheadername"><code>request.removeHeader(name)</code></a></li> 5101cb0ef41Sopenharmony_ci<li><a href="#requestreusedsocket"><code>request.reusedSocket</code></a></li> 5111cb0ef41Sopenharmony_ci<li><a href="#requestsetheadername-value"><code>request.setHeader(name, value)</code></a></li> 5121cb0ef41Sopenharmony_ci<li><a href="#requestsetnodelaynodelay"><code>request.setNoDelay([noDelay])</code></a></li> 5131cb0ef41Sopenharmony_ci<li><a href="#requestsetsocketkeepaliveenable-initialdelay"><code>request.setSocketKeepAlive([enable][, initialDelay])</code></a></li> 5141cb0ef41Sopenharmony_ci<li><a href="#requestsettimeouttimeout-callback"><code>request.setTimeout(timeout[, callback])</code></a></li> 5151cb0ef41Sopenharmony_ci<li><a href="#requestsocket"><code>request.socket</code></a></li> 5161cb0ef41Sopenharmony_ci<li><a href="#requestuncork"><code>request.uncork()</code></a></li> 5171cb0ef41Sopenharmony_ci<li><a href="#requestwritableended"><code>request.writableEnded</code></a></li> 5181cb0ef41Sopenharmony_ci<li><a href="#requestwritablefinished"><code>request.writableFinished</code></a></li> 5191cb0ef41Sopenharmony_ci<li><a href="#requestwritechunk-encoding-callback"><code>request.write(chunk[, encoding][, callback])</code></a></li> 5201cb0ef41Sopenharmony_ci</ul> 5211cb0ef41Sopenharmony_ci</li> 5221cb0ef41Sopenharmony_ci<li><a href="#class-httpserver">Class: <code>http.Server</code></a> 5231cb0ef41Sopenharmony_ci<ul> 5241cb0ef41Sopenharmony_ci<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li> 5251cb0ef41Sopenharmony_ci<li><a href="#event-checkexpectation">Event: <code>'checkExpectation'</code></a></li> 5261cb0ef41Sopenharmony_ci<li><a href="#event-clienterror">Event: <code>'clientError'</code></a></li> 5271cb0ef41Sopenharmony_ci<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 5281cb0ef41Sopenharmony_ci<li><a href="#event-connect_1">Event: <code>'connect'</code></a></li> 5291cb0ef41Sopenharmony_ci<li><a href="#event-connection">Event: <code>'connection'</code></a></li> 5301cb0ef41Sopenharmony_ci<li><a href="#event-droprequest">Event: <code>'dropRequest'</code></a></li> 5311cb0ef41Sopenharmony_ci<li><a href="#event-request">Event: <code>'request'</code></a></li> 5321cb0ef41Sopenharmony_ci<li><a href="#event-upgrade_1">Event: <code>'upgrade'</code></a></li> 5331cb0ef41Sopenharmony_ci<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li> 5341cb0ef41Sopenharmony_ci<li><a href="#servercloseallconnections"><code>server.closeAllConnections()</code></a></li> 5351cb0ef41Sopenharmony_ci<li><a href="#servercloseidleconnections"><code>server.closeIdleConnections()</code></a></li> 5361cb0ef41Sopenharmony_ci<li><a href="#serverheaderstimeout"><code>server.headersTimeout</code></a></li> 5371cb0ef41Sopenharmony_ci<li><a href="#serverlisten"><code>server.listen()</code></a></li> 5381cb0ef41Sopenharmony_ci<li><a href="#serverlistening"><code>server.listening</code></a></li> 5391cb0ef41Sopenharmony_ci<li><a href="#servermaxheaderscount"><code>server.maxHeadersCount</code></a></li> 5401cb0ef41Sopenharmony_ci<li><a href="#serverrequesttimeout"><code>server.requestTimeout</code></a></li> 5411cb0ef41Sopenharmony_ci<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li> 5421cb0ef41Sopenharmony_ci<li><a href="#servermaxrequestspersocket"><code>server.maxRequestsPerSocket</code></a></li> 5431cb0ef41Sopenharmony_ci<li><a href="#servertimeout"><code>server.timeout</code></a></li> 5441cb0ef41Sopenharmony_ci<li><a href="#serverkeepalivetimeout"><code>server.keepAliveTimeout</code></a></li> 5451cb0ef41Sopenharmony_ci</ul> 5461cb0ef41Sopenharmony_ci</li> 5471cb0ef41Sopenharmony_ci<li><a href="#class-httpserverresponse">Class: <code>http.ServerResponse</code></a> 5481cb0ef41Sopenharmony_ci<ul> 5491cb0ef41Sopenharmony_ci<li><a href="#event-close_2">Event: <code>'close'</code></a></li> 5501cb0ef41Sopenharmony_ci<li><a href="#event-finish_1">Event: <code>'finish'</code></a></li> 5511cb0ef41Sopenharmony_ci<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li> 5521cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li> 5531cb0ef41Sopenharmony_ci<li><a href="#responsecork"><code>response.cork()</code></a></li> 5541cb0ef41Sopenharmony_ci<li><a href="#responseenddata-encoding-callback"><code>response.end([data[, encoding]][, callback])</code></a></li> 5551cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#responsefinished"><code>response.finished</code></a></span></li> 5561cb0ef41Sopenharmony_ci<li><a href="#responseflushheaders"><code>response.flushHeaders()</code></a></li> 5571cb0ef41Sopenharmony_ci<li><a href="#responsegetheadername"><code>response.getHeader(name)</code></a></li> 5581cb0ef41Sopenharmony_ci<li><a href="#responsegetheadernames"><code>response.getHeaderNames()</code></a></li> 5591cb0ef41Sopenharmony_ci<li><a href="#responsegetheaders"><code>response.getHeaders()</code></a></li> 5601cb0ef41Sopenharmony_ci<li><a href="#responsehasheadername"><code>response.hasHeader(name)</code></a></li> 5611cb0ef41Sopenharmony_ci<li><a href="#responseheaderssent"><code>response.headersSent</code></a></li> 5621cb0ef41Sopenharmony_ci<li><a href="#responseremoveheadername"><code>response.removeHeader(name)</code></a></li> 5631cb0ef41Sopenharmony_ci<li><a href="#responsereq"><code>response.req</code></a></li> 5641cb0ef41Sopenharmony_ci<li><a href="#responsesenddate"><code>response.sendDate</code></a></li> 5651cb0ef41Sopenharmony_ci<li><a href="#responsesetheadername-value"><code>response.setHeader(name, value)</code></a></li> 5661cb0ef41Sopenharmony_ci<li><a href="#responsesettimeoutmsecs-callback"><code>response.setTimeout(msecs[, callback])</code></a></li> 5671cb0ef41Sopenharmony_ci<li><a href="#responsesocket"><code>response.socket</code></a></li> 5681cb0ef41Sopenharmony_ci<li><a href="#responsestatuscode"><code>response.statusCode</code></a></li> 5691cb0ef41Sopenharmony_ci<li><a href="#responsestatusmessage"><code>response.statusMessage</code></a></li> 5701cb0ef41Sopenharmony_ci<li><a href="#responsestrictcontentlength"><code>response.strictContentLength</code></a></li> 5711cb0ef41Sopenharmony_ci<li><a href="#responseuncork"><code>response.uncork()</code></a></li> 5721cb0ef41Sopenharmony_ci<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li> 5731cb0ef41Sopenharmony_ci<li><a href="#responsewritablefinished"><code>response.writableFinished</code></a></li> 5741cb0ef41Sopenharmony_ci<li><a href="#responsewritechunk-encoding-callback"><code>response.write(chunk[, encoding][, callback])</code></a></li> 5751cb0ef41Sopenharmony_ci<li><a href="#responsewritecontinue"><code>response.writeContinue()</code></a></li> 5761cb0ef41Sopenharmony_ci<li><a href="#responsewriteearlyhintshints-callback"><code>response.writeEarlyHints(hints[, callback])</code></a></li> 5771cb0ef41Sopenharmony_ci<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li> 5781cb0ef41Sopenharmony_ci<li><a href="#responsewriteprocessing"><code>response.writeProcessing()</code></a></li> 5791cb0ef41Sopenharmony_ci</ul> 5801cb0ef41Sopenharmony_ci</li> 5811cb0ef41Sopenharmony_ci<li><a href="#class-httpincomingmessage">Class: <code>http.IncomingMessage</code></a> 5821cb0ef41Sopenharmony_ci<ul> 5831cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#event-aborted">Event: <code>'aborted'</code></a></span></li> 5841cb0ef41Sopenharmony_ci<li><a href="#event-close_3">Event: <code>'close'</code></a></li> 5851cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#messageaborted"><code>message.aborted</code></a></span></li> 5861cb0ef41Sopenharmony_ci<li><a href="#messagecomplete"><code>message.complete</code></a></li> 5871cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#messageconnection"><code>message.connection</code></a></span></li> 5881cb0ef41Sopenharmony_ci<li><a href="#messagedestroyerror"><code>message.destroy([error])</code></a></li> 5891cb0ef41Sopenharmony_ci<li><a href="#messageheaders"><code>message.headers</code></a></li> 5901cb0ef41Sopenharmony_ci<li><a href="#messageheadersdistinct"><code>message.headersDistinct</code></a></li> 5911cb0ef41Sopenharmony_ci<li><a href="#messagehttpversion"><code>message.httpVersion</code></a></li> 5921cb0ef41Sopenharmony_ci<li><a href="#messagemethod"><code>message.method</code></a></li> 5931cb0ef41Sopenharmony_ci<li><a href="#messagerawheaders"><code>message.rawHeaders</code></a></li> 5941cb0ef41Sopenharmony_ci<li><a href="#messagerawtrailers"><code>message.rawTrailers</code></a></li> 5951cb0ef41Sopenharmony_ci<li><a href="#messagesettimeoutmsecs-callback"><code>message.setTimeout(msecs[, callback])</code></a></li> 5961cb0ef41Sopenharmony_ci<li><a href="#messagesocket"><code>message.socket</code></a></li> 5971cb0ef41Sopenharmony_ci<li><a href="#messagestatuscode"><code>message.statusCode</code></a></li> 5981cb0ef41Sopenharmony_ci<li><a href="#messagestatusmessage"><code>message.statusMessage</code></a></li> 5991cb0ef41Sopenharmony_ci<li><a href="#messagetrailers"><code>message.trailers</code></a></li> 6001cb0ef41Sopenharmony_ci<li><a href="#messagetrailersdistinct"><code>message.trailersDistinct</code></a></li> 6011cb0ef41Sopenharmony_ci<li><a href="#messageurl"><code>message.url</code></a></li> 6021cb0ef41Sopenharmony_ci</ul> 6031cb0ef41Sopenharmony_ci</li> 6041cb0ef41Sopenharmony_ci<li><a href="#class-httpoutgoingmessage">Class: <code>http.OutgoingMessage</code></a> 6051cb0ef41Sopenharmony_ci<ul> 6061cb0ef41Sopenharmony_ci<li><a href="#event-drain">Event: <code>'drain'</code></a></li> 6071cb0ef41Sopenharmony_ci<li><a href="#event-finish_2">Event: <code>'finish'</code></a></li> 6081cb0ef41Sopenharmony_ci<li><a href="#event-prefinish">Event: <code>'prefinish'</code></a></li> 6091cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageaddtrailersheaders"><code>outgoingMessage.addTrailers(headers)</code></a></li> 6101cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageappendheadername-value"><code>outgoingMessage.appendHeader(name, value)</code></a></li> 6111cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#outgoingmessageconnection"><code>outgoingMessage.connection</code></a></span></li> 6121cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagecork"><code>outgoingMessage.cork()</code></a></li> 6131cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagedestroyerror"><code>outgoingMessage.destroy([error])</code></a></li> 6141cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageendchunk-encoding-callback"><code>outgoingMessage.end(chunk[, encoding][, callback])</code></a></li> 6151cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageflushheaders"><code>outgoingMessage.flushHeaders()</code></a></li> 6161cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagegetheadername"><code>outgoingMessage.getHeader(name)</code></a></li> 6171cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagegetheadernames"><code>outgoingMessage.getHeaderNames()</code></a></li> 6181cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagegetheaders"><code>outgoingMessage.getHeaders()</code></a></li> 6191cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagehasheadername"><code>outgoingMessage.hasHeader(name)</code></a></li> 6201cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageheaderssent"><code>outgoingMessage.headersSent</code></a></li> 6211cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagepipe"><code>outgoingMessage.pipe()</code></a></li> 6221cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageremoveheadername"><code>outgoingMessage.removeHeader(name)</code></a></li> 6231cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagesetheadername-value"><code>outgoingMessage.setHeader(name, value)</code></a></li> 6241cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagesetheadersheaders"><code>outgoingMessage.setHeaders(headers)</code></a></li> 6251cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagesettimeoutmsesc-callback"><code>outgoingMessage.setTimeout(msesc[, callback])</code></a></li> 6261cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagesocket"><code>outgoingMessage.socket</code></a></li> 6271cb0ef41Sopenharmony_ci<li><a href="#outgoingmessageuncork"><code>outgoingMessage.uncork()</code></a></li> 6281cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritablecorked"><code>outgoingMessage.writableCorked</code></a></li> 6291cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritableended"><code>outgoingMessage.writableEnded</code></a></li> 6301cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritablefinished"><code>outgoingMessage.writableFinished</code></a></li> 6311cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritablehighwatermark"><code>outgoingMessage.writableHighWaterMark</code></a></li> 6321cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritablelength"><code>outgoingMessage.writableLength</code></a></li> 6331cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritableobjectmode"><code>outgoingMessage.writableObjectMode</code></a></li> 6341cb0ef41Sopenharmony_ci<li><a href="#outgoingmessagewritechunk-encoding-callback"><code>outgoingMessage.write(chunk[, encoding][, callback])</code></a></li> 6351cb0ef41Sopenharmony_ci</ul> 6361cb0ef41Sopenharmony_ci</li> 6371cb0ef41Sopenharmony_ci<li><a href="#httpmethods"><code>http.METHODS</code></a></li> 6381cb0ef41Sopenharmony_ci<li><a href="#httpstatus_codes"><code>http.STATUS_CODES</code></a></li> 6391cb0ef41Sopenharmony_ci<li><a href="#httpcreateserveroptions-requestlistener"><code>http.createServer([options][, requestListener])</code></a></li> 6401cb0ef41Sopenharmony_ci<li><a href="#httpgetoptions-callback"><code>http.get(options[, callback])</code></a></li> 6411cb0ef41Sopenharmony_ci<li><a href="#httpgeturl-options-callback"><code>http.get(url[, options][, callback])</code></a></li> 6421cb0ef41Sopenharmony_ci<li><a href="#httpglobalagent"><code>http.globalAgent</code></a></li> 6431cb0ef41Sopenharmony_ci<li><a href="#httpmaxheadersize"><code>http.maxHeaderSize</code></a></li> 6441cb0ef41Sopenharmony_ci<li><a href="#httprequestoptions-callback"><code>http.request(options[, callback])</code></a></li> 6451cb0ef41Sopenharmony_ci<li><a href="#httprequesturl-options-callback"><code>http.request(url[, options][, callback])</code></a></li> 6461cb0ef41Sopenharmony_ci<li><a href="#httpvalidateheadernamename-label"><code>http.validateHeaderName(name[, label])</code></a></li> 6471cb0ef41Sopenharmony_ci<li><a href="#httpvalidateheadervaluename-value"><code>http.validateHeaderValue(name, value)</code></a></li> 6481cb0ef41Sopenharmony_ci<li><a href="#httpsetmaxidlehttpparsersmax"><code>http.setMaxIdleHTTPParsers(max)</code></a></li> 6491cb0ef41Sopenharmony_ci</ul> 6501cb0ef41Sopenharmony_ci</li> 6511cb0ef41Sopenharmony_ci</ul></details> 6521cb0ef41Sopenharmony_ci 6531cb0ef41Sopenharmony_ci <div id="apicontent"> 6541cb0ef41Sopenharmony_ci <h2>HTTP<span><a class="mark" href="#http" id="http">#</a></span><a aria-hidden="true" class="legacy" id="http_http"></a></h2> 6551cb0ef41Sopenharmony_ci 6561cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 6571cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/http.js">lib/http.js</a></p> 6581cb0ef41Sopenharmony_ci<p>To use the HTTP server and client one must <code>require('node:http')</code>.</p> 6591cb0ef41Sopenharmony_ci<p>The HTTP interfaces in Node.js are designed to support many features 6601cb0ef41Sopenharmony_ciof the protocol which have been traditionally difficult to use. 6611cb0ef41Sopenharmony_ciIn particular, large, possibly chunk-encoded, messages. The interface is 6621cb0ef41Sopenharmony_cicareful to never buffer entire requests or responses, so the 6631cb0ef41Sopenharmony_ciuser is able to stream data.</p> 6641cb0ef41Sopenharmony_ci<p>HTTP message headers are represented by an object like this:</p> 6651cb0ef41Sopenharmony_ci<!-- eslint-skip --> 6661cb0ef41Sopenharmony_ci<pre><code class="language-js">{ <span class="hljs-string">'content-length'</span>: <span class="hljs-string">'123'</span>, 6671cb0ef41Sopenharmony_ci <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain'</span>, 6681cb0ef41Sopenharmony_ci <span class="hljs-string">'connection'</span>: <span class="hljs-string">'keep-alive'</span>, 6691cb0ef41Sopenharmony_ci <span class="hljs-string">'host'</span>: <span class="hljs-string">'example.com'</span>, 6701cb0ef41Sopenharmony_ci <span class="hljs-string">'accept'</span>: <span class="hljs-string">'*/*'</span> }</code> <button class="copy-button">copy</button></pre> 6711cb0ef41Sopenharmony_ci<p>Keys are lowercased. Values are not modified.</p> 6721cb0ef41Sopenharmony_ci<p>In order to support the full spectrum of possible HTTP applications, the Node.js 6731cb0ef41Sopenharmony_ciHTTP API is very low-level. It deals with stream handling and message 6741cb0ef41Sopenharmony_ciparsing only. It parses a message into headers and body but it does not 6751cb0ef41Sopenharmony_ciparse the actual headers or the body.</p> 6761cb0ef41Sopenharmony_ci<p>See <a href="#messageheaders"><code>message.headers</code></a> for details on how duplicate headers are handled.</p> 6771cb0ef41Sopenharmony_ci<p>The raw headers as they were received are retained in the <code>rawHeaders</code> 6781cb0ef41Sopenharmony_ciproperty, which is an array of <code>[key, value, key2, value2, ...]</code>. For 6791cb0ef41Sopenharmony_ciexample, the previous message header object might have a <code>rawHeaders</code> 6801cb0ef41Sopenharmony_cilist like the following:</p> 6811cb0ef41Sopenharmony_ci<!-- eslint-disable semi --> 6821cb0ef41Sopenharmony_ci<pre><code class="language-js">[ <span class="hljs-string">'ConTent-Length'</span>, <span class="hljs-string">'123456'</span>, 6831cb0ef41Sopenharmony_ci <span class="hljs-string">'content-LENGTH'</span>, <span class="hljs-string">'123'</span>, 6841cb0ef41Sopenharmony_ci <span class="hljs-string">'content-type'</span>, <span class="hljs-string">'text/plain'</span>, 6851cb0ef41Sopenharmony_ci <span class="hljs-string">'CONNECTION'</span>, <span class="hljs-string">'keep-alive'</span>, 6861cb0ef41Sopenharmony_ci <span class="hljs-string">'Host'</span>, <span class="hljs-string">'example.com'</span>, 6871cb0ef41Sopenharmony_ci <span class="hljs-string">'accepT'</span>, <span class="hljs-string">'*/*'</span> ]</code> <button class="copy-button">copy</button></pre> 6881cb0ef41Sopenharmony_ci<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> 6891cb0ef41Sopenharmony_ci<div class="api_metadata"> 6901cb0ef41Sopenharmony_ci<span>Added in: v0.3.4</span> 6911cb0ef41Sopenharmony_ci</div> 6921cb0ef41Sopenharmony_ci<p>An <code>Agent</code> is responsible for managing connection persistence 6931cb0ef41Sopenharmony_ciand reuse for HTTP clients. It maintains a queue of pending requests 6941cb0ef41Sopenharmony_cifor a given host and port, reusing a single socket connection for each 6951cb0ef41Sopenharmony_ciuntil the queue is empty, at which time the socket is either destroyed 6961cb0ef41Sopenharmony_cior put into a pool where it is kept to be used again for requests to the 6971cb0ef41Sopenharmony_cisame host and port. Whether it is destroyed or pooled depends on the 6981cb0ef41Sopenharmony_ci<code>keepAlive</code> <a href="#new-agentoptions">option</a>.</p> 6991cb0ef41Sopenharmony_ci<p>Pooled connections have TCP Keep-Alive enabled for them, but servers may 7001cb0ef41Sopenharmony_cistill close idle connections, in which case they will be removed from the 7011cb0ef41Sopenharmony_cipool and a new connection will be made when a new HTTP request is made for 7021cb0ef41Sopenharmony_cithat host and port. Servers may also refuse to allow multiple requests 7031cb0ef41Sopenharmony_ciover the same connection, in which case the connection will have to be 7041cb0ef41Sopenharmony_ciremade for every request and cannot be pooled. The <code>Agent</code> will still make 7051cb0ef41Sopenharmony_cithe requests to that server, but each one will occur over a new connection.</p> 7061cb0ef41Sopenharmony_ci<p>When a connection is closed by the client or the server, it is removed 7071cb0ef41Sopenharmony_cifrom the pool. Any unused sockets in the pool will be unrefed so as not 7081cb0ef41Sopenharmony_cito keep the Node.js process running when there are no outstanding requests. 7091cb0ef41Sopenharmony_ci(see <a href="net.html#socketunref"><code>socket.unref()</code></a>).</p> 7101cb0ef41Sopenharmony_ci<p>It is good practice, to <a href="#agentdestroy"><code>destroy()</code></a> an <code>Agent</code> instance when it is no 7111cb0ef41Sopenharmony_cilonger in use, because unused sockets consume OS resources.</p> 7121cb0ef41Sopenharmony_ci<p>Sockets are removed from an agent when the socket emits either 7131cb0ef41Sopenharmony_cia <code>'close'</code> event or an <code>'agentRemove'</code> event. When intending to keep one 7141cb0ef41Sopenharmony_ciHTTP request open for a long time without keeping it in the agent, something 7151cb0ef41Sopenharmony_cilike the following may be done:</p> 7161cb0ef41Sopenharmony_ci<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> { 7171cb0ef41Sopenharmony_ci <span class="hljs-comment">// Do stuff</span> 7181cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'socket'</span>, <span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { 7191cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'agentRemove'</span>); 7201cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 7211cb0ef41Sopenharmony_ci<p>An agent may also be used for an individual request. By providing 7221cb0ef41Sopenharmony_ci<code>{agent: false}</code> as an option to the <code>http.get()</code> or <code>http.request()</code> 7231cb0ef41Sopenharmony_cifunctions, a one-time use <code>Agent</code> with default options will be used 7241cb0ef41Sopenharmony_cifor the client connection.</p> 7251cb0ef41Sopenharmony_ci<p><code>agent:false</code>:</p> 7261cb0ef41Sopenharmony_ci<pre><code class="language-js">http.<span class="hljs-title function_">get</span>({ 7271cb0ef41Sopenharmony_ci <span class="hljs-attr">hostname</span>: <span class="hljs-string">'localhost'</span>, 7281cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>, 7291cb0ef41Sopenharmony_ci <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>, 7301cb0ef41Sopenharmony_ci <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> 7311cb0ef41Sopenharmony_ci}, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 7321cb0ef41Sopenharmony_ci <span class="hljs-comment">// Do stuff with response</span> 7331cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 7341cb0ef41Sopenharmony_ci<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> 7351cb0ef41Sopenharmony_ci<div class="api_metadata"> 7361cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7371cb0ef41Sopenharmony_ci<table> 7381cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 7391cb0ef41Sopenharmony_ci<tr><td>v15.6.0, v14.17.0</td> 7401cb0ef41Sopenharmony_ci<td><p>Change the default scheduling from 'fifo' to 'lifo'.</p></td></tr> 7411cb0ef41Sopenharmony_ci<tr><td>v14.5.0, v12.20.0</td> 7421cb0ef41Sopenharmony_ci<td><p>Add <code>scheduling</code> option to specify the free socket scheduling strategy.</p></td></tr> 7431cb0ef41Sopenharmony_ci<tr><td>v14.5.0, v12.19.0</td> 7441cb0ef41Sopenharmony_ci<td><p>Add <code>maxTotalSockets</code> option to agent constructor.</p></td></tr> 7451cb0ef41Sopenharmony_ci<tr><td>v0.3.4</td> 7461cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.4</span></p></td></tr> 7471cb0ef41Sopenharmony_ci</tbody></table> 7481cb0ef41Sopenharmony_ci</details> 7491cb0ef41Sopenharmony_ci</div> 7501cb0ef41Sopenharmony_ci<ul> 7511cb0ef41Sopenharmony_ci<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. 7521cb0ef41Sopenharmony_ciCan have the following fields: 7531cb0ef41Sopenharmony_ci<ul> 7541cb0ef41Sopenharmony_ci<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 7551cb0ef41Sopenharmony_cioutstanding requests, so they can be used for future requests without 7561cb0ef41Sopenharmony_cihaving to reestablish a TCP connection. Not to be confused with the 7571cb0ef41Sopenharmony_ci<code>keep-alive</code> value of the <code>Connection</code> header. The <code>Connection: keep-alive</code> 7581cb0ef41Sopenharmony_ciheader is always sent when using an agent except when the <code>Connection</code> 7591cb0ef41Sopenharmony_ciheader is explicitly specified or when the <code>keepAlive</code> and <code>maxSockets</code> 7601cb0ef41Sopenharmony_cioptions are respectively set to <code>false</code> and <code>Infinity</code>, in which case 7611cb0ef41Sopenharmony_ci<code>Connection: close</code> will be used. <strong>Default:</strong> <code>false</code>.</li> 7621cb0ef41Sopenharmony_ci<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 7631cb0ef41Sopenharmony_cithe <a href="net.html#socketsetkeepaliveenable-initialdelay">initial delay</a> 7641cb0ef41Sopenharmony_cifor TCP Keep-Alive packets. Ignored when the 7651cb0ef41Sopenharmony_ci<code>keepAlive</code> option is <code>false</code> or <code>undefined</code>. <strong>Default:</strong> <code>1000</code>.</li> 7661cb0ef41Sopenharmony_ci<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. 7671cb0ef41Sopenharmony_ciIf the same host opens multiple concurrent connections, each request 7681cb0ef41Sopenharmony_ciwill use new socket until the <code>maxSockets</code> value is reached. 7691cb0ef41Sopenharmony_ciIf the host attempts to open more connections than <code>maxSockets</code>, 7701cb0ef41Sopenharmony_cithe additional requests will enter into a pending request queue, and 7711cb0ef41Sopenharmony_ciwill enter active connection state when an existing connection terminates. 7721cb0ef41Sopenharmony_ciThis makes sure there are at most <code>maxSockets</code> active connections at 7731cb0ef41Sopenharmony_ciany point in time, from a given host. 7741cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 7751cb0ef41Sopenharmony_ci<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 7761cb0ef41Sopenharmony_ciall hosts in total. Each request will use a new socket 7771cb0ef41Sopenharmony_ciuntil the maximum is reached. 7781cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Infinity</code>.</li> 7791cb0ef41Sopenharmony_ci<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 7801cb0ef41Sopenharmony_ciin a free state. Only relevant if <code>keepAlive</code> is set to <code>true</code>. 7811cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>256</code>.</li> 7821cb0ef41Sopenharmony_ci<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 7831cb0ef41Sopenharmony_cithe next free socket to use. It can be <code>'fifo'</code> or <code>'lifo'</code>. 7841cb0ef41Sopenharmony_ciThe main difference between the two scheduling strategies is that <code>'lifo'</code> 7851cb0ef41Sopenharmony_ciselects the most recently used socket, while <code>'fifo'</code> selects 7861cb0ef41Sopenharmony_cithe least recently used socket. 7871cb0ef41Sopenharmony_ciIn case of a low rate of request per second, the <code>'lifo'</code> scheduling 7881cb0ef41Sopenharmony_ciwill lower the risk of picking a socket that might have been closed 7891cb0ef41Sopenharmony_ciby the server due to inactivity. 7901cb0ef41Sopenharmony_ciIn case of a high rate of request per second, 7911cb0ef41Sopenharmony_cithe <code>'fifo'</code> scheduling will maximize the number of open sockets, 7921cb0ef41Sopenharmony_ciwhile the <code>'lifo'</code> scheduling will keep it as low as possible. 7931cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'lifo'</code>.</li> 7941cb0ef41Sopenharmony_ci<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. 7951cb0ef41Sopenharmony_ciThis will set the timeout when the socket is created.</li> 7961cb0ef41Sopenharmony_ci</ul> 7971cb0ef41Sopenharmony_ci</li> 7981cb0ef41Sopenharmony_ci</ul> 7991cb0ef41Sopenharmony_ci<p><code>options</code> in <a href="net.html#socketconnectoptions-connectlistener"><code>socket.connect()</code></a> are also supported.</p> 8001cb0ef41Sopenharmony_ci<p>The default <a href="#httpglobalagent"><code>http.globalAgent</code></a> that is used by <a href="#httprequestoptions-callback"><code>http.request()</code></a> has all 8011cb0ef41Sopenharmony_ciof these values set to their respective defaults.</p> 8021cb0ef41Sopenharmony_ci<p>To configure any of them, a custom <a href="#class-httpagent"><code>http.Agent</code></a> instance must be created.</p> 8031cb0ef41Sopenharmony_ci 8041cb0ef41Sopenharmony_ci<pre class="with-59-chars"><input class="js-flavor-selector" 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>; 8051cb0ef41Sopenharmony_ci<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> }); 8061cb0ef41Sopenharmony_cioptions.<span class="hljs-property">agent</span> = keepAliveAgent; 8071cb0ef41Sopenharmony_ci<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>); 8081cb0ef41Sopenharmony_ci<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> }); 8091cb0ef41Sopenharmony_cioptions.<span class="hljs-property">agent</span> = keepAliveAgent; 8101cb0ef41Sopenharmony_cihttp.<span class="hljs-title function_">request</span>(options, onResponseCallback);</code><button class="copy-button">copy</button></pre> 8111cb0ef41Sopenharmony_ci<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> 8121cb0ef41Sopenharmony_ci<div class="api_metadata"> 8131cb0ef41Sopenharmony_ci<span>Added in: v0.11.4</span> 8141cb0ef41Sopenharmony_ci</div> 8151cb0ef41Sopenharmony_ci<ul> 8161cb0ef41Sopenharmony_ci<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 8171cb0ef41Sopenharmony_ci<a href="net.html#netcreateconnectionoptions-connectlistener"><code>net.createConnection()</code></a> for the format of the options</li> 8181cb0ef41Sopenharmony_ci<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> 8191cb0ef41Sopenharmony_ci<li>Returns: <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 8201cb0ef41Sopenharmony_ci</ul> 8211cb0ef41Sopenharmony_ci<p>Produces a socket/stream to be used for HTTP requests.</p> 8221cb0ef41Sopenharmony_ci<p>By default, this function is the same as <a href="net.html#netcreateconnectionoptions-connectlistener"><code>net.createConnection()</code></a>. However, 8231cb0ef41Sopenharmony_cicustom agents may override this method in case greater flexibility is desired.</p> 8241cb0ef41Sopenharmony_ci<p>A socket/stream can be supplied in one of two ways: by returning the 8251cb0ef41Sopenharmony_cisocket/stream from this function, or by passing the socket/stream to <code>callback</code>.</p> 8261cb0ef41Sopenharmony_ci<p>This method is guaranteed to return an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 8271cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 8281cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 8291cb0ef41Sopenharmony_ci<p><code>callback</code> has a signature of <code>(err, stream)</code>.</p> 8301cb0ef41Sopenharmony_ci<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> 8311cb0ef41Sopenharmony_ci<div class="api_metadata"> 8321cb0ef41Sopenharmony_ci<span>Added in: v8.1.0</span> 8331cb0ef41Sopenharmony_ci</div> 8341cb0ef41Sopenharmony_ci<ul> 8351cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 8361cb0ef41Sopenharmony_ci</ul> 8371cb0ef41Sopenharmony_ci<p>Called when <code>socket</code> is detached from a request and could be persisted by the 8381cb0ef41Sopenharmony_ci<code>Agent</code>. Default behavior is to:</p> 8391cb0ef41Sopenharmony_ci<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>); 8401cb0ef41Sopenharmony_cisocket.<span class="hljs-title function_">unref</span>(); 8411cb0ef41Sopenharmony_ci<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;</code> <button class="copy-button">copy</button></pre> 8421cb0ef41Sopenharmony_ci<p>This method can be overridden by a particular <code>Agent</code> subclass. If this 8431cb0ef41Sopenharmony_cimethod returns a falsy value, the socket will be destroyed instead of persisting 8441cb0ef41Sopenharmony_ciit for use with the next request.</p> 8451cb0ef41Sopenharmony_ci<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 8461cb0ef41Sopenharmony_ci<a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>.</p> 8471cb0ef41Sopenharmony_ci<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> 8481cb0ef41Sopenharmony_ci<div class="api_metadata"> 8491cb0ef41Sopenharmony_ci<span>Added in: v8.1.0</span> 8501cb0ef41Sopenharmony_ci</div> 8511cb0ef41Sopenharmony_ci<ul> 8521cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 8531cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 8541cb0ef41Sopenharmony_ci</ul> 8551cb0ef41Sopenharmony_ci<p>Called when <code>socket</code> is attached to <code>request</code> after being persisted because of 8561cb0ef41Sopenharmony_cithe keep-alive options. Default behavior is to:</p> 8571cb0ef41Sopenharmony_ci<pre><code class="language-js">socket.<span class="hljs-title function_">ref</span>();</code> <button class="copy-button">copy</button></pre> 8581cb0ef41Sopenharmony_ci<p>This method can be overridden by a particular <code>Agent</code> subclass.</p> 8591cb0ef41Sopenharmony_ci<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 8601cb0ef41Sopenharmony_ci<a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>.</p> 8611cb0ef41Sopenharmony_ci<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> 8621cb0ef41Sopenharmony_ci<div class="api_metadata"> 8631cb0ef41Sopenharmony_ci<span>Added in: v0.11.4</span> 8641cb0ef41Sopenharmony_ci</div> 8651cb0ef41Sopenharmony_ci<p>Destroy any sockets that are currently in use by the agent.</p> 8661cb0ef41Sopenharmony_ci<p>It is usually not necessary to do this. However, if using an 8671cb0ef41Sopenharmony_ciagent with <code>keepAlive</code> enabled, then it is best to explicitly shut down 8681cb0ef41Sopenharmony_cithe agent when it is no longer needed. Otherwise, 8691cb0ef41Sopenharmony_cisockets might stay open for quite a long time before the server 8701cb0ef41Sopenharmony_citerminates them.</p> 8711cb0ef41Sopenharmony_ci<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> 8721cb0ef41Sopenharmony_ci<div class="api_metadata"> 8731cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 8741cb0ef41Sopenharmony_ci<table> 8751cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 8761cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td> 8771cb0ef41Sopenharmony_ci<td><p>The property now has a <code>null</code> prototype.</p></td></tr> 8781cb0ef41Sopenharmony_ci<tr><td>v0.11.4</td> 8791cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.4</span></p></td></tr> 8801cb0ef41Sopenharmony_ci</tbody></table> 8811cb0ef41Sopenharmony_ci</details> 8821cb0ef41Sopenharmony_ci</div> 8831cb0ef41Sopenharmony_ci<ul> 8841cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 8851cb0ef41Sopenharmony_ci</ul> 8861cb0ef41Sopenharmony_ci<p>An object which contains arrays of sockets currently awaiting use by 8871cb0ef41Sopenharmony_cithe agent when <code>keepAlive</code> is enabled. Do not modify.</p> 8881cb0ef41Sopenharmony_ci<p>Sockets in the <code>freeSockets</code> list will be automatically destroyed and 8891cb0ef41Sopenharmony_ciremoved from the array on <code>'timeout'</code>.</p> 8901cb0ef41Sopenharmony_ci<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> 8911cb0ef41Sopenharmony_ci<div class="api_metadata"> 8921cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 8931cb0ef41Sopenharmony_ci<table> 8941cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 8951cb0ef41Sopenharmony_ci<tr><td>v17.7.0</td> 8961cb0ef41Sopenharmony_ci<td><p>The <code>options</code> parameter is now optional.</p></td></tr> 8971cb0ef41Sopenharmony_ci<tr><td>v0.11.4</td> 8981cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.4</span></p></td></tr> 8991cb0ef41Sopenharmony_ci</tbody></table> 9001cb0ef41Sopenharmony_ci</details> 9011cb0ef41Sopenharmony_ci</div> 9021cb0ef41Sopenharmony_ci<ul> 9031cb0ef41Sopenharmony_ci<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 9041cb0ef41Sopenharmony_ci<ul> 9051cb0ef41Sopenharmony_ci<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 9061cb0ef41Sopenharmony_cirequest to</li> 9071cb0ef41Sopenharmony_ci<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> 9081cb0ef41Sopenharmony_ci<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 9091cb0ef41Sopenharmony_ciwhen issuing the request</li> 9101cb0ef41Sopenharmony_ci<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> 9111cb0ef41Sopenharmony_ci</ul> 9121cb0ef41Sopenharmony_ci</li> 9131cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 9141cb0ef41Sopenharmony_ci</ul> 9151cb0ef41Sopenharmony_ci<p>Get a unique name for a set of request options, to determine whether a 9161cb0ef41Sopenharmony_ciconnection can be reused. For an HTTP agent, this returns 9171cb0ef41Sopenharmony_ci<code>host:port:localAddress</code> or <code>host:port:localAddress:family</code>. For an HTTPS agent, 9181cb0ef41Sopenharmony_cithe name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options 9191cb0ef41Sopenharmony_cithat determine socket reusability.</p> 9201cb0ef41Sopenharmony_ci<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> 9211cb0ef41Sopenharmony_ci<div class="api_metadata"> 9221cb0ef41Sopenharmony_ci<span>Added in: v0.11.7</span> 9231cb0ef41Sopenharmony_ci</div> 9241cb0ef41Sopenharmony_ci<ul> 9251cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 9261cb0ef41Sopenharmony_ci</ul> 9271cb0ef41Sopenharmony_ci<p>By default set to 256. For agents with <code>keepAlive</code> enabled, this 9281cb0ef41Sopenharmony_cisets the maximum number of sockets that will be left open in the free 9291cb0ef41Sopenharmony_cistate.</p> 9301cb0ef41Sopenharmony_ci<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> 9311cb0ef41Sopenharmony_ci<div class="api_metadata"> 9321cb0ef41Sopenharmony_ci<span>Added in: v0.3.6</span> 9331cb0ef41Sopenharmony_ci</div> 9341cb0ef41Sopenharmony_ci<ul> 9351cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 9361cb0ef41Sopenharmony_ci</ul> 9371cb0ef41Sopenharmony_ci<p>By default set to <code>Infinity</code>. Determines how many concurrent sockets the agent 9381cb0ef41Sopenharmony_cican have open per origin. Origin is the returned value of <a href="#agentgetnameoptions"><code>agent.getName()</code></a>.</p> 9391cb0ef41Sopenharmony_ci<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> 9401cb0ef41Sopenharmony_ci<div class="api_metadata"> 9411cb0ef41Sopenharmony_ci<span>Added in: v14.5.0, v12.19.0</span> 9421cb0ef41Sopenharmony_ci</div> 9431cb0ef41Sopenharmony_ci<ul> 9441cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 9451cb0ef41Sopenharmony_ci</ul> 9461cb0ef41Sopenharmony_ci<p>By default set to <code>Infinity</code>. Determines how many concurrent sockets the agent 9471cb0ef41Sopenharmony_cican have open. Unlike <code>maxSockets</code>, this parameter applies across all origins.</p> 9481cb0ef41Sopenharmony_ci<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> 9491cb0ef41Sopenharmony_ci<div class="api_metadata"> 9501cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 9511cb0ef41Sopenharmony_ci<table> 9521cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 9531cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td> 9541cb0ef41Sopenharmony_ci<td><p>The property now has a <code>null</code> prototype.</p></td></tr> 9551cb0ef41Sopenharmony_ci<tr><td>v0.5.9</td> 9561cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.9</span></p></td></tr> 9571cb0ef41Sopenharmony_ci</tbody></table> 9581cb0ef41Sopenharmony_ci</details> 9591cb0ef41Sopenharmony_ci</div> 9601cb0ef41Sopenharmony_ci<ul> 9611cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 9621cb0ef41Sopenharmony_ci</ul> 9631cb0ef41Sopenharmony_ci<p>An object which contains queues of requests that have not yet been assigned to 9641cb0ef41Sopenharmony_cisockets. Do not modify.</p> 9651cb0ef41Sopenharmony_ci<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> 9661cb0ef41Sopenharmony_ci<div class="api_metadata"> 9671cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 9681cb0ef41Sopenharmony_ci<table> 9691cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 9701cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td> 9711cb0ef41Sopenharmony_ci<td><p>The property now has a <code>null</code> prototype.</p></td></tr> 9721cb0ef41Sopenharmony_ci<tr><td>v0.3.6</td> 9731cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.6</span></p></td></tr> 9741cb0ef41Sopenharmony_ci</tbody></table> 9751cb0ef41Sopenharmony_ci</details> 9761cb0ef41Sopenharmony_ci</div> 9771cb0ef41Sopenharmony_ci<ul> 9781cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 9791cb0ef41Sopenharmony_ci</ul> 9801cb0ef41Sopenharmony_ci<p>An object which contains arrays of sockets currently in use by the 9811cb0ef41Sopenharmony_ciagent. Do not modify.</p> 9821cb0ef41Sopenharmony_ci</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> 9831cb0ef41Sopenharmony_ci<div class="api_metadata"> 9841cb0ef41Sopenharmony_ci<span>Added in: v0.1.17</span> 9851cb0ef41Sopenharmony_ci</div> 9861cb0ef41Sopenharmony_ci<ul> 9871cb0ef41Sopenharmony_ci<li>Extends: <a href="http.html#class-httpoutgoingmessage" class="type"><http.OutgoingMessage></a></li> 9881cb0ef41Sopenharmony_ci</ul> 9891cb0ef41Sopenharmony_ci<p>This object is created internally and returned from <a href="#httprequestoptions-callback"><code>http.request()</code></a>. It 9901cb0ef41Sopenharmony_cirepresents an <em>in-progress</em> request whose header has already been queued. The 9911cb0ef41Sopenharmony_ciheader is still mutable using the <a href="#requestsetheadername-value"><code>setHeader(name, value)</code></a>, 9921cb0ef41Sopenharmony_ci<a href="#requestgetheadername"><code>getHeader(name)</code></a>, <a href="#requestremoveheadername"><code>removeHeader(name)</code></a> API. The actual header will 9931cb0ef41Sopenharmony_cibe sent along with the first data chunk or when calling <a href="#requestenddata-encoding-callback"><code>request.end()</code></a>.</p> 9941cb0ef41Sopenharmony_ci<p>To get the response, add a listener for <a href="#event-response"><code>'response'</code></a> to the request object. 9951cb0ef41Sopenharmony_ci<a href="#event-response"><code>'response'</code></a> will be emitted from the request object when the response 9961cb0ef41Sopenharmony_ciheaders have been received. The <a href="#event-response"><code>'response'</code></a> event is executed with one 9971cb0ef41Sopenharmony_ciargument which is an instance of <a href="#class-httpincomingmessage"><code>http.IncomingMessage</code></a>.</p> 9981cb0ef41Sopenharmony_ci<p>During the <a href="#event-response"><code>'response'</code></a> event, one can add listeners to the 9991cb0ef41Sopenharmony_ciresponse object; particularly to listen for the <code>'data'</code> event.</p> 10001cb0ef41Sopenharmony_ci<p>If no <a href="#event-response"><code>'response'</code></a> handler is added, then the response will be 10011cb0ef41Sopenharmony_cientirely discarded. However, if a <a href="#event-response"><code>'response'</code></a> event handler is added, 10021cb0ef41Sopenharmony_cithen the data from the response object <strong>must</strong> be consumed, either by 10031cb0ef41Sopenharmony_cicalling <code>response.read()</code> whenever there is a <code>'readable'</code> event, or 10041cb0ef41Sopenharmony_ciby adding a <code>'data'</code> handler, or by calling the <code>.resume()</code> method. 10051cb0ef41Sopenharmony_ciUntil the data is consumed, the <code>'end'</code> event will not fire. Also, until 10061cb0ef41Sopenharmony_cithe data is read it will consume memory that can eventually lead to a 10071cb0ef41Sopenharmony_ci'process out of memory' error.</p> 10081cb0ef41Sopenharmony_ci<p>For backward compatibility, <code>res</code> will only emit <code>'error'</code> if there is an 10091cb0ef41Sopenharmony_ci<code>'error'</code> listener registered.</p> 10101cb0ef41Sopenharmony_ci<p>Set <code>Content-Length</code> header to limit the response body size. 10111cb0ef41Sopenharmony_ciIf <a href="#responsestrictcontentlength"><code>response.strictContentLength</code></a> is set to <code>true</code>, mismatching the 10121cb0ef41Sopenharmony_ci<code>Content-Length</code> header value will result in an <code>Error</code> being thrown, 10131cb0ef41Sopenharmony_ciidentified by <code>code:</code> <a href="errors.html#err_http_content_length_mismatch"><code>'ERR_HTTP_CONTENT_LENGTH_MISMATCH'</code></a>.</p> 10141cb0ef41Sopenharmony_ci<p><code>Content-Length</code> value should be in bytes, not characters. Use 10151cb0ef41Sopenharmony_ci<a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength()</code></a> to determine the length of the body in bytes.</p> 10161cb0ef41Sopenharmony_ci<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> 10171cb0ef41Sopenharmony_ci<div class="api_metadata"> 10181cb0ef41Sopenharmony_ci<span>Added in: v1.4.1</span><span>Deprecated since: v17.0.0, v16.12.0</span> 10191cb0ef41Sopenharmony_ci</div> 10201cb0ef41Sopenharmony_ci<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> 10211cb0ef41Sopenharmony_ci<p>Emitted when the request has been aborted by the client. This event is only 10221cb0ef41Sopenharmony_ciemitted on the first call to <code>abort()</code>.</p> 10231cb0ef41Sopenharmony_ci<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> 10241cb0ef41Sopenharmony_ci<div class="api_metadata"> 10251cb0ef41Sopenharmony_ci<span>Added in: v0.5.4</span> 10261cb0ef41Sopenharmony_ci</div> 10271cb0ef41Sopenharmony_ci<p>Indicates that the request is completed, or its underlying connection was 10281cb0ef41Sopenharmony_citerminated prematurely (before the response completion).</p> 10291cb0ef41Sopenharmony_ci<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> 10301cb0ef41Sopenharmony_ci<div class="api_metadata"> 10311cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 10321cb0ef41Sopenharmony_ci</div> 10331cb0ef41Sopenharmony_ci<ul> 10341cb0ef41Sopenharmony_ci<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 10351cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 10361cb0ef41Sopenharmony_ci<li><code>head</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 10371cb0ef41Sopenharmony_ci</ul> 10381cb0ef41Sopenharmony_ci<p>Emitted each time a server responds to a request with a <code>CONNECT</code> method. If 10391cb0ef41Sopenharmony_cithis event is not being listened for, clients receiving a <code>CONNECT</code> method will 10401cb0ef41Sopenharmony_cihave their connections closed.</p> 10411cb0ef41Sopenharmony_ci<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 10421cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 10431cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 10441cb0ef41Sopenharmony_ci<p>A client and server pair demonstrating how to listen for the <code>'connect'</code> event:</p> 10451cb0ef41Sopenharmony_ci 10461cb0ef41Sopenharmony_ci<pre class="with-50-chars"><input class="js-flavor-selector" 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>; 10471cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:net'</span>; 10481cb0ef41Sopenharmony_ci<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>; 10491cb0ef41Sopenharmony_ci 10501cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create an HTTP tunneling proxy</span> 10511cb0ef41Sopenharmony_ci<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> { 10521cb0ef41Sopenharmony_ci 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> }); 10531cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>); 10541cb0ef41Sopenharmony_ci}); 10551cb0ef41Sopenharmony_ciproxy.<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> { 10561cb0ef41Sopenharmony_ci <span class="hljs-comment">// Connect to an origin server</span> 10571cb0ef41Sopenharmony_ci <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>); 10581cb0ef41Sopenharmony_ci <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> { 10591cb0ef41Sopenharmony_ci clientSocket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 200 Connection Established\r\n'</span> + 10601cb0ef41Sopenharmony_ci <span class="hljs-string">'Proxy-agent: Node.js-Proxy\r\n'</span> + 10611cb0ef41Sopenharmony_ci <span class="hljs-string">'\r\n'</span>); 10621cb0ef41Sopenharmony_ci serverSocket.<span class="hljs-title function_">write</span>(head); 10631cb0ef41Sopenharmony_ci serverSocket.<span class="hljs-title function_">pipe</span>(clientSocket); 10641cb0ef41Sopenharmony_ci clientSocket.<span class="hljs-title function_">pipe</span>(serverSocket); 10651cb0ef41Sopenharmony_ci }); 10661cb0ef41Sopenharmony_ci}); 10671cb0ef41Sopenharmony_ci 10681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Now that proxy is running</span> 10691cb0ef41Sopenharmony_ciproxy.<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> { 10701cb0ef41Sopenharmony_ci 10711cb0ef41Sopenharmony_ci <span class="hljs-comment">// Make a request to a tunneling proxy</span> 10721cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> options = { 10731cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>, 10741cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 10751cb0ef41Sopenharmony_ci <span class="hljs-attr">method</span>: <span class="hljs-string">'CONNECT'</span>, 10761cb0ef41Sopenharmony_ci <span class="hljs-attr">path</span>: <span class="hljs-string">'www.google.com:80'</span>, 10771cb0ef41Sopenharmony_ci }; 10781cb0ef41Sopenharmony_ci 10791cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> req = <span class="hljs-title function_">request</span>(options); 10801cb0ef41Sopenharmony_ci req.<span class="hljs-title function_">end</span>(); 10811cb0ef41Sopenharmony_ci 10821cb0ef41Sopenharmony_ci 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> { 10831cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got connected!'</span>); 10841cb0ef41Sopenharmony_ci 10851cb0ef41Sopenharmony_ci <span class="hljs-comment">// Make a request over an HTTP tunnel</span> 10861cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'GET / HTTP/1.1\r\n'</span> + 10871cb0ef41Sopenharmony_ci <span class="hljs-string">'Host: www.google.com:80\r\n'</span> + 10881cb0ef41Sopenharmony_ci <span class="hljs-string">'Connection: close\r\n'</span> + 10891cb0ef41Sopenharmony_ci <span class="hljs-string">'\r\n'</span>); 10901cb0ef41Sopenharmony_ci 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> { 10911cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk.<span class="hljs-title function_">toString</span>()); 10921cb0ef41Sopenharmony_ci }); 10931cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 10941cb0ef41Sopenharmony_ci proxy.<span class="hljs-title function_">close</span>(); 10951cb0ef41Sopenharmony_ci }); 10961cb0ef41Sopenharmony_ci }); 10971cb0ef41Sopenharmony_ci});</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>); 10981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>); 10991cb0ef41Sopenharmony_ci<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>); 11001cb0ef41Sopenharmony_ci 11011cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create an HTTP tunneling proxy</span> 11021cb0ef41Sopenharmony_ci<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> { 11031cb0ef41Sopenharmony_ci 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> }); 11041cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>); 11051cb0ef41Sopenharmony_ci}); 11061cb0ef41Sopenharmony_ciproxy.<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> { 11071cb0ef41Sopenharmony_ci <span class="hljs-comment">// Connect to an origin server</span> 11081cb0ef41Sopenharmony_ci <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>); 11091cb0ef41Sopenharmony_ci <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> { 11101cb0ef41Sopenharmony_ci clientSocket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 200 Connection Established\r\n'</span> + 11111cb0ef41Sopenharmony_ci <span class="hljs-string">'Proxy-agent: Node.js-Proxy\r\n'</span> + 11121cb0ef41Sopenharmony_ci <span class="hljs-string">'\r\n'</span>); 11131cb0ef41Sopenharmony_ci serverSocket.<span class="hljs-title function_">write</span>(head); 11141cb0ef41Sopenharmony_ci serverSocket.<span class="hljs-title function_">pipe</span>(clientSocket); 11151cb0ef41Sopenharmony_ci clientSocket.<span class="hljs-title function_">pipe</span>(serverSocket); 11161cb0ef41Sopenharmony_ci }); 11171cb0ef41Sopenharmony_ci}); 11181cb0ef41Sopenharmony_ci 11191cb0ef41Sopenharmony_ci<span class="hljs-comment">// Now that proxy is running</span> 11201cb0ef41Sopenharmony_ciproxy.<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> { 11211cb0ef41Sopenharmony_ci 11221cb0ef41Sopenharmony_ci <span class="hljs-comment">// Make a request to a tunneling proxy</span> 11231cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> options = { 11241cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>, 11251cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 11261cb0ef41Sopenharmony_ci <span class="hljs-attr">method</span>: <span class="hljs-string">'CONNECT'</span>, 11271cb0ef41Sopenharmony_ci <span class="hljs-attr">path</span>: <span class="hljs-string">'www.google.com:80'</span>, 11281cb0ef41Sopenharmony_ci }; 11291cb0ef41Sopenharmony_ci 11301cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options); 11311cb0ef41Sopenharmony_ci req.<span class="hljs-title function_">end</span>(); 11321cb0ef41Sopenharmony_ci 11331cb0ef41Sopenharmony_ci 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> { 11341cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got connected!'</span>); 11351cb0ef41Sopenharmony_ci 11361cb0ef41Sopenharmony_ci <span class="hljs-comment">// Make a request over an HTTP tunnel</span> 11371cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'GET / HTTP/1.1\r\n'</span> + 11381cb0ef41Sopenharmony_ci <span class="hljs-string">'Host: www.google.com:80\r\n'</span> + 11391cb0ef41Sopenharmony_ci <span class="hljs-string">'Connection: close\r\n'</span> + 11401cb0ef41Sopenharmony_ci <span class="hljs-string">'\r\n'</span>); 11411cb0ef41Sopenharmony_ci 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> { 11421cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk.<span class="hljs-title function_">toString</span>()); 11431cb0ef41Sopenharmony_ci }); 11441cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 11451cb0ef41Sopenharmony_ci proxy.<span class="hljs-title function_">close</span>(); 11461cb0ef41Sopenharmony_ci }); 11471cb0ef41Sopenharmony_ci }); 11481cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 11491cb0ef41Sopenharmony_ci<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> 11501cb0ef41Sopenharmony_ci<div class="api_metadata"> 11511cb0ef41Sopenharmony_ci<span>Added in: v0.3.2</span> 11521cb0ef41Sopenharmony_ci</div> 11531cb0ef41Sopenharmony_ci<p>Emitted when the server sends a '100 Continue' HTTP response, usually because 11541cb0ef41Sopenharmony_cithe request contained 'Expect: 100-continue'. This is an instruction that 11551cb0ef41Sopenharmony_cithe client should send the request body.</p> 11561cb0ef41Sopenharmony_ci<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> 11571cb0ef41Sopenharmony_ci<div class="api_metadata"> 11581cb0ef41Sopenharmony_ci<span>Added in: v0.3.6</span> 11591cb0ef41Sopenharmony_ci</div> 11601cb0ef41Sopenharmony_ci<p>Emitted when the request has been sent. More specifically, this event is emitted 11611cb0ef41Sopenharmony_ciwhen the last segment of the response headers and body have been handed off to 11621cb0ef41Sopenharmony_cithe operating system for transmission over the network. It does not imply that 11631cb0ef41Sopenharmony_cithe server has received anything yet.</p> 11641cb0ef41Sopenharmony_ci<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> 11651cb0ef41Sopenharmony_ci<div class="api_metadata"> 11661cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span> 11671cb0ef41Sopenharmony_ci</div> 11681cb0ef41Sopenharmony_ci<ul> 11691cb0ef41Sopenharmony_ci<li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 11701cb0ef41Sopenharmony_ci<ul> 11711cb0ef41Sopenharmony_ci<li><code>httpVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 11721cb0ef41Sopenharmony_ci<li><code>httpVersionMajor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 11731cb0ef41Sopenharmony_ci<li><code>httpVersionMinor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 11741cb0ef41Sopenharmony_ci<li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 11751cb0ef41Sopenharmony_ci<li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 11761cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 11771cb0ef41Sopenharmony_ci<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 11781cb0ef41Sopenharmony_ci</ul> 11791cb0ef41Sopenharmony_ci</li> 11801cb0ef41Sopenharmony_ci</ul> 11811cb0ef41Sopenharmony_ci<p>Emitted when the server sends a 1xx intermediate response (excluding 101 11821cb0ef41Sopenharmony_ciUpgrade). The listeners of this event will receive an object containing the 11831cb0ef41Sopenharmony_ciHTTP version, status code, status message, key-value headers object, 11841cb0ef41Sopenharmony_ciand array with the raw header names followed by their respective values.</p> 11851cb0ef41Sopenharmony_ci 11861cb0ef41Sopenharmony_ci<pre class="with-36-chars"><input class="js-flavor-selector" 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>; 11871cb0ef41Sopenharmony_ci 11881cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = { 11891cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 11901cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>, 11911cb0ef41Sopenharmony_ci <span class="hljs-attr">path</span>: <span class="hljs-string">'/length_request'</span>, 11921cb0ef41Sopenharmony_ci}; 11931cb0ef41Sopenharmony_ci 11941cb0ef41Sopenharmony_ci<span class="hljs-comment">// Make a request</span> 11951cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> req = <span class="hljs-title function_">request</span>(options); 11961cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">end</span>(); 11971cb0ef41Sopenharmony_ci 11981cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">on</span>(<span class="hljs-string">'information'</span>, <span class="hljs-function">(<span class="hljs-params">info</span>) =></span> { 11991cb0ef41Sopenharmony_ci <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>); 12001cb0ef41Sopenharmony_ci});</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>); 12011cb0ef41Sopenharmony_ci 12021cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = { 12031cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 12041cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>, 12051cb0ef41Sopenharmony_ci <span class="hljs-attr">path</span>: <span class="hljs-string">'/length_request'</span>, 12061cb0ef41Sopenharmony_ci}; 12071cb0ef41Sopenharmony_ci 12081cb0ef41Sopenharmony_ci<span class="hljs-comment">// Make a request</span> 12091cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options); 12101cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">end</span>(); 12111cb0ef41Sopenharmony_ci 12121cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">on</span>(<span class="hljs-string">'information'</span>, <span class="hljs-function">(<span class="hljs-params">info</span>) =></span> { 12131cb0ef41Sopenharmony_ci <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>); 12141cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 12151cb0ef41Sopenharmony_ci<p>101 Upgrade statuses do not fire this event due to their break from the 12161cb0ef41Sopenharmony_citraditional HTTP request/response chain, such as web sockets, in-place TLS 12171cb0ef41Sopenharmony_ciupgrades, or HTTP 2.0. To be notified of 101 Upgrade notices, listen for the 12181cb0ef41Sopenharmony_ci<a href="#event-upgrade"><code>'upgrade'</code></a> event instead.</p> 12191cb0ef41Sopenharmony_ci<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> 12201cb0ef41Sopenharmony_ci<div class="api_metadata"> 12211cb0ef41Sopenharmony_ci<span>Added in: v0.1.0</span> 12221cb0ef41Sopenharmony_ci</div> 12231cb0ef41Sopenharmony_ci<ul> 12241cb0ef41Sopenharmony_ci<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 12251cb0ef41Sopenharmony_ci</ul> 12261cb0ef41Sopenharmony_ci<p>Emitted when a response is received to this request. This event is emitted only 12271cb0ef41Sopenharmony_cionce.</p> 12281cb0ef41Sopenharmony_ci<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> 12291cb0ef41Sopenharmony_ci<div class="api_metadata"> 12301cb0ef41Sopenharmony_ci<span>Added in: v0.5.3</span> 12311cb0ef41Sopenharmony_ci</div> 12321cb0ef41Sopenharmony_ci<ul> 12331cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 12341cb0ef41Sopenharmony_ci</ul> 12351cb0ef41Sopenharmony_ci<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 12361cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 12371cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 12381cb0ef41Sopenharmony_ci<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> 12391cb0ef41Sopenharmony_ci<div class="api_metadata"> 12401cb0ef41Sopenharmony_ci<span>Added in: v0.7.8</span> 12411cb0ef41Sopenharmony_ci</div> 12421cb0ef41Sopenharmony_ci<p>Emitted when the underlying socket times out from inactivity. This only notifies 12431cb0ef41Sopenharmony_cithat the socket has been idle. The request must be destroyed manually.</p> 12441cb0ef41Sopenharmony_ci<p>See also: <a href="#requestsettimeouttimeout-callback"><code>request.setTimeout()</code></a>.</p> 12451cb0ef41Sopenharmony_ci<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> 12461cb0ef41Sopenharmony_ci<div class="api_metadata"> 12471cb0ef41Sopenharmony_ci<span>Added in: v0.1.94</span> 12481cb0ef41Sopenharmony_ci</div> 12491cb0ef41Sopenharmony_ci<ul> 12501cb0ef41Sopenharmony_ci<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 12511cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 12521cb0ef41Sopenharmony_ci<li><code>head</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 12531cb0ef41Sopenharmony_ci</ul> 12541cb0ef41Sopenharmony_ci<p>Emitted each time a server responds to a request with an upgrade. If this 12551cb0ef41Sopenharmony_cievent is not being listened for and the response status code is 101 Switching 12561cb0ef41Sopenharmony_ciProtocols, clients receiving an upgrade header will have their connections 12571cb0ef41Sopenharmony_ciclosed.</p> 12581cb0ef41Sopenharmony_ci<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 12591cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 12601cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 12611cb0ef41Sopenharmony_ci<p>A client server pair demonstrating how to listen for the <code>'upgrade'</code> event.</p> 12621cb0ef41Sopenharmony_ci 12631cb0ef41Sopenharmony_ci<pre class="with-35-chars"><input class="js-flavor-selector" 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>; 12641cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 12651cb0ef41Sopenharmony_ci 12661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create an HTTP server</span> 12671cb0ef41Sopenharmony_ci<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> { 12681cb0ef41Sopenharmony_ci 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> }); 12691cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>); 12701cb0ef41Sopenharmony_ci}); 12711cb0ef41Sopenharmony_ciserver.<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> { 12721cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'</span> + 12731cb0ef41Sopenharmony_ci <span class="hljs-string">'Upgrade: WebSocket\r\n'</span> + 12741cb0ef41Sopenharmony_ci <span class="hljs-string">'Connection: Upgrade\r\n'</span> + 12751cb0ef41Sopenharmony_ci <span class="hljs-string">'\r\n'</span>); 12761cb0ef41Sopenharmony_ci 12771cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">pipe</span>(socket); <span class="hljs-comment">// echo back</span> 12781cb0ef41Sopenharmony_ci}); 12791cb0ef41Sopenharmony_ci 12801cb0ef41Sopenharmony_ci<span class="hljs-comment">// Now that server is running</span> 12811cb0ef41Sopenharmony_ciserver.<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> { 12821cb0ef41Sopenharmony_ci 12831cb0ef41Sopenharmony_ci <span class="hljs-comment">// make a request</span> 12841cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> options = { 12851cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>, 12861cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 12871cb0ef41Sopenharmony_ci <span class="hljs-attr">headers</span>: { 12881cb0ef41Sopenharmony_ci <span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Upgrade'</span>, 12891cb0ef41Sopenharmony_ci <span class="hljs-string">'Upgrade'</span>: <span class="hljs-string">'websocket'</span>, 12901cb0ef41Sopenharmony_ci }, 12911cb0ef41Sopenharmony_ci }; 12921cb0ef41Sopenharmony_ci 12931cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options); 12941cb0ef41Sopenharmony_ci req.<span class="hljs-title function_">end</span>(); 12951cb0ef41Sopenharmony_ci 12961cb0ef41Sopenharmony_ci 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> { 12971cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got upgraded!'</span>); 12981cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">end</span>(); 12991cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>); 13001cb0ef41Sopenharmony_ci }); 13011cb0ef41Sopenharmony_ci});</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>); 13021cb0ef41Sopenharmony_ci 13031cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create an HTTP server</span> 13041cb0ef41Sopenharmony_ci<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> { 13051cb0ef41Sopenharmony_ci 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> }); 13061cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>); 13071cb0ef41Sopenharmony_ci}); 13081cb0ef41Sopenharmony_ciserver.<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> { 13091cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'</span> + 13101cb0ef41Sopenharmony_ci <span class="hljs-string">'Upgrade: WebSocket\r\n'</span> + 13111cb0ef41Sopenharmony_ci <span class="hljs-string">'Connection: Upgrade\r\n'</span> + 13121cb0ef41Sopenharmony_ci <span class="hljs-string">'\r\n'</span>); 13131cb0ef41Sopenharmony_ci 13141cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">pipe</span>(socket); <span class="hljs-comment">// echo back</span> 13151cb0ef41Sopenharmony_ci}); 13161cb0ef41Sopenharmony_ci 13171cb0ef41Sopenharmony_ci<span class="hljs-comment">// Now that server is running</span> 13181cb0ef41Sopenharmony_ciserver.<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> { 13191cb0ef41Sopenharmony_ci 13201cb0ef41Sopenharmony_ci <span class="hljs-comment">// make a request</span> 13211cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> options = { 13221cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>, 13231cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 13241cb0ef41Sopenharmony_ci <span class="hljs-attr">headers</span>: { 13251cb0ef41Sopenharmony_ci <span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Upgrade'</span>, 13261cb0ef41Sopenharmony_ci <span class="hljs-string">'Upgrade'</span>: <span class="hljs-string">'websocket'</span>, 13271cb0ef41Sopenharmony_ci }, 13281cb0ef41Sopenharmony_ci }; 13291cb0ef41Sopenharmony_ci 13301cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options); 13311cb0ef41Sopenharmony_ci req.<span class="hljs-title function_">end</span>(); 13321cb0ef41Sopenharmony_ci 13331cb0ef41Sopenharmony_ci 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> { 13341cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got upgraded!'</span>); 13351cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">end</span>(); 13361cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>); 13371cb0ef41Sopenharmony_ci }); 13381cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 13391cb0ef41Sopenharmony_ci<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> 13401cb0ef41Sopenharmony_ci<div class="api_metadata"> 13411cb0ef41Sopenharmony_ci<span>Added in: v0.3.8</span><span>Deprecated since: v14.1.0, v13.14.0</span> 13421cb0ef41Sopenharmony_ci</div> 13431cb0ef41Sopenharmony_ci<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> 13441cb0ef41Sopenharmony_ci<p>Marks the request as aborting. Calling this will cause remaining data 13451cb0ef41Sopenharmony_ciin the response to be dropped and the socket to be destroyed.</p> 13461cb0ef41Sopenharmony_ci<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> 13471cb0ef41Sopenharmony_ci<div class="api_metadata"> 13481cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 13491cb0ef41Sopenharmony_ci<table> 13501cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 13511cb0ef41Sopenharmony_ci<tr><td>v17.0.0, v16.12.0</td> 13521cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v17.0.0, v16.12.0</span></p></td></tr> 13531cb0ef41Sopenharmony_ci<tr><td>v11.0.0</td> 13541cb0ef41Sopenharmony_ci<td><p>The <code>aborted</code> property is no longer a timestamp number.</p></td></tr> 13551cb0ef41Sopenharmony_ci<tr><td>v0.11.14</td> 13561cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.14</span></p></td></tr> 13571cb0ef41Sopenharmony_ci</tbody></table> 13581cb0ef41Sopenharmony_ci</details> 13591cb0ef41Sopenharmony_ci</div> 13601cb0ef41Sopenharmony_ci<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> 13611cb0ef41Sopenharmony_ci<ul> 13621cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 13631cb0ef41Sopenharmony_ci</ul> 13641cb0ef41Sopenharmony_ci<p>The <code>request.aborted</code> property will be <code>true</code> if the request has 13651cb0ef41Sopenharmony_cibeen aborted.</p> 13661cb0ef41Sopenharmony_ci<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> 13671cb0ef41Sopenharmony_ci<div class="api_metadata"> 13681cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span><span>Deprecated since: v13.0.0</span> 13691cb0ef41Sopenharmony_ci</div> 13701cb0ef41Sopenharmony_ci<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> 13711cb0ef41Sopenharmony_ci<ul> 13721cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 13731cb0ef41Sopenharmony_ci</ul> 13741cb0ef41Sopenharmony_ci<p>See <a href="#requestsocket"><code>request.socket</code></a>.</p> 13751cb0ef41Sopenharmony_ci<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> 13761cb0ef41Sopenharmony_ci<div class="api_metadata"> 13771cb0ef41Sopenharmony_ci<span>Added in: v13.2.0, v12.16.0</span> 13781cb0ef41Sopenharmony_ci</div> 13791cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writablecork"><code>writable.cork()</code></a>.</p> 13801cb0ef41Sopenharmony_ci<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> 13811cb0ef41Sopenharmony_ci<div class="api_metadata"> 13821cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 13831cb0ef41Sopenharmony_ci<table> 13841cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 13851cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 13861cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 13871cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 13881cb0ef41Sopenharmony_ci<td><p>This method now returns a reference to <code>ClientRequest</code>.</p></td></tr> 13891cb0ef41Sopenharmony_ci<tr><td>v0.1.90</td> 13901cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.90</span></p></td></tr> 13911cb0ef41Sopenharmony_ci</tbody></table> 13921cb0ef41Sopenharmony_ci</details> 13931cb0ef41Sopenharmony_ci</div> 13941cb0ef41Sopenharmony_ci<ul> 13951cb0ef41Sopenharmony_ci<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> 13961cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 13971cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 13981cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 13991cb0ef41Sopenharmony_ci</ul> 14001cb0ef41Sopenharmony_ci<p>Finishes sending the request. If any parts of the body are 14011cb0ef41Sopenharmony_ciunsent, it will flush them to the stream. If the request is 14021cb0ef41Sopenharmony_cichunked, this will send the terminating <code>'0\r\n\r\n'</code>.</p> 14031cb0ef41Sopenharmony_ci<p>If <code>data</code> is specified, it is equivalent to calling 14041cb0ef41Sopenharmony_ci<a href="#requestwritechunk-encoding-callback"><code>request.write(data, encoding)</code></a> followed by <code>request.end(callback)</code>.</p> 14051cb0ef41Sopenharmony_ci<p>If <code>callback</code> is specified, it will be called when the request stream 14061cb0ef41Sopenharmony_ciis finished.</p> 14071cb0ef41Sopenharmony_ci<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> 14081cb0ef41Sopenharmony_ci<div class="api_metadata"> 14091cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 14101cb0ef41Sopenharmony_ci<table> 14111cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 14121cb0ef41Sopenharmony_ci<tr><td>v14.5.0</td> 14131cb0ef41Sopenharmony_ci<td><p>The function returns <code>this</code> for consistency with other Readable streams.</p></td></tr> 14141cb0ef41Sopenharmony_ci<tr><td>v0.3.0</td> 14151cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.0</span></p></td></tr> 14161cb0ef41Sopenharmony_ci</tbody></table> 14171cb0ef41Sopenharmony_ci</details> 14181cb0ef41Sopenharmony_ci</div> 14191cb0ef41Sopenharmony_ci<ul> 14201cb0ef41Sopenharmony_ci<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> 14211cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 14221cb0ef41Sopenharmony_ci</ul> 14231cb0ef41Sopenharmony_ci<p>Destroy the request. Optionally emit an <code>'error'</code> event, 14241cb0ef41Sopenharmony_ciand emit a <code>'close'</code> event. Calling this will cause remaining data 14251cb0ef41Sopenharmony_ciin the response to be dropped and the socket to be destroyed.</p> 14261cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writabledestroyerror"><code>writable.destroy()</code></a> for further details.</p> 14271cb0ef41Sopenharmony_ci<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> 14281cb0ef41Sopenharmony_ci<div class="api_metadata"> 14291cb0ef41Sopenharmony_ci<span>Added in: v14.1.0, v13.14.0</span> 14301cb0ef41Sopenharmony_ci</div> 14311cb0ef41Sopenharmony_ci<ul> 14321cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 14331cb0ef41Sopenharmony_ci</ul> 14341cb0ef41Sopenharmony_ci<p>Is <code>true</code> after <a href="#requestdestroyerror"><code>request.destroy()</code></a> has been called.</p> 14351cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writabledestroyed"><code>writable.destroyed</code></a> for further details.</p> 14361cb0ef41Sopenharmony_ci<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> 14371cb0ef41Sopenharmony_ci<div class="api_metadata"> 14381cb0ef41Sopenharmony_ci<span>Added in: v0.0.1</span><span>Deprecated since: v13.4.0, v12.16.0</span> 14391cb0ef41Sopenharmony_ci</div> 14401cb0ef41Sopenharmony_ci<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> 14411cb0ef41Sopenharmony_ci<ul> 14421cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 14431cb0ef41Sopenharmony_ci</ul> 14441cb0ef41Sopenharmony_ci<p>The <code>request.finished</code> property will be <code>true</code> if <a href="#requestenddata-encoding-callback"><code>request.end()</code></a> 14451cb0ef41Sopenharmony_cihas been called. <code>request.end()</code> will automatically be called if the 14461cb0ef41Sopenharmony_cirequest was initiated via <a href="#httpgetoptions-callback"><code>http.get()</code></a>.</p> 14471cb0ef41Sopenharmony_ci<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> 14481cb0ef41Sopenharmony_ci<div class="api_metadata"> 14491cb0ef41Sopenharmony_ci<span>Added in: v1.6.0</span> 14501cb0ef41Sopenharmony_ci</div> 14511cb0ef41Sopenharmony_ci<p>Flushes the request headers.</p> 14521cb0ef41Sopenharmony_ci<p>For efficiency reasons, Node.js normally buffers the request headers until 14531cb0ef41Sopenharmony_ci<code>request.end()</code> is called or the first chunk of request data is written. It 14541cb0ef41Sopenharmony_cithen tries to pack the request headers and data into a single TCP packet.</p> 14551cb0ef41Sopenharmony_ci<p>That's usually desired (it saves a TCP round-trip), but not when the first 14561cb0ef41Sopenharmony_cidata is not sent until possibly much later. <code>request.flushHeaders()</code> bypasses 14571cb0ef41Sopenharmony_cithe optimization and kickstarts the request.</p> 14581cb0ef41Sopenharmony_ci<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> 14591cb0ef41Sopenharmony_ci<div class="api_metadata"> 14601cb0ef41Sopenharmony_ci<span>Added in: v1.6.0</span> 14611cb0ef41Sopenharmony_ci</div> 14621cb0ef41Sopenharmony_ci<ul> 14631cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 14641cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 14651cb0ef41Sopenharmony_ci</ul> 14661cb0ef41Sopenharmony_ci<p>Reads out a header on the request. The name is case-insensitive. 14671cb0ef41Sopenharmony_ciThe type of the return value depends on the arguments provided to 14681cb0ef41Sopenharmony_ci<a href="#requestsetheadername-value"><code>request.setHeader()</code></a>.</p> 14691cb0ef41Sopenharmony_ci<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>); 14701cb0ef41Sopenharmony_cirequest.<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)); 14711cb0ef41Sopenharmony_cirequest.<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>]); 14721cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contentType = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Type'</span>); 14731cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'contentType' is 'text/html'</span> 14741cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contentLength = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Length'</span>); 14751cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'contentLength' is of type number</span> 14761cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cookie = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Cookie'</span>); 14771cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'cookie' is of type string[]</span></code> <button class="copy-button">copy</button></pre> 14781cb0ef41Sopenharmony_ci<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> 14791cb0ef41Sopenharmony_ci<div class="api_metadata"> 14801cb0ef41Sopenharmony_ci<span>Added in: v7.7.0</span> 14811cb0ef41Sopenharmony_ci</div> 14821cb0ef41Sopenharmony_ci<ul> 14831cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 14841cb0ef41Sopenharmony_ci</ul> 14851cb0ef41Sopenharmony_ci<p>Returns an array containing the unique names of the current outgoing headers. 14861cb0ef41Sopenharmony_ciAll header names are lowercase.</p> 14871cb0ef41Sopenharmony_ci<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>); 14881cb0ef41Sopenharmony_cirequest.<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>]); 14891cb0ef41Sopenharmony_ci 14901cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> headerNames = request.<span class="hljs-title function_">getHeaderNames</span>(); 14911cb0ef41Sopenharmony_ci<span class="hljs-comment">// headerNames === ['foo', 'cookie']</span></code> <button class="copy-button">copy</button></pre> 14921cb0ef41Sopenharmony_ci<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> 14931cb0ef41Sopenharmony_ci<div class="api_metadata"> 14941cb0ef41Sopenharmony_ci<span>Added in: v7.7.0</span> 14951cb0ef41Sopenharmony_ci</div> 14961cb0ef41Sopenharmony_ci<ul> 14971cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 14981cb0ef41Sopenharmony_ci</ul> 14991cb0ef41Sopenharmony_ci<p>Returns a shallow copy of the current outgoing headers. Since a shallow copy 15001cb0ef41Sopenharmony_ciis used, array values may be mutated without additional calls to various 15011cb0ef41Sopenharmony_ciheader-related http module methods. The keys of the returned object are the 15021cb0ef41Sopenharmony_ciheader names and the values are the respective header values. All header names 15031cb0ef41Sopenharmony_ciare lowercase.</p> 15041cb0ef41Sopenharmony_ci<p>The object returned by the <code>request.getHeaders()</code> method <em>does not</em> 15051cb0ef41Sopenharmony_ciprototypically inherit from the JavaScript <code>Object</code>. This means that typical 15061cb0ef41Sopenharmony_ci<code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, and others 15071cb0ef41Sopenharmony_ciare not defined and <em>will not work</em>.</p> 15081cb0ef41Sopenharmony_ci<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>); 15091cb0ef41Sopenharmony_cirequest.<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>]); 15101cb0ef41Sopenharmony_ci 15111cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> headers = request.<span class="hljs-title function_">getHeaders</span>(); 15121cb0ef41Sopenharmony_ci<span class="hljs-comment">// headers === { foo: 'bar', 'cookie': ['foo=bar', 'bar=baz'] }</span></code> <button class="copy-button">copy</button></pre> 15131cb0ef41Sopenharmony_ci<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> 15141cb0ef41Sopenharmony_ci<div class="api_metadata"> 15151cb0ef41Sopenharmony_ci<span>Added in: v15.13.0, v14.17.0</span> 15161cb0ef41Sopenharmony_ci</div> 15171cb0ef41Sopenharmony_ci<ul> 15181cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 15191cb0ef41Sopenharmony_ci</ul> 15201cb0ef41Sopenharmony_ci<p>Returns an array containing the unique names of the current outgoing raw 15211cb0ef41Sopenharmony_ciheaders. Header names are returned with their exact casing being set.</p> 15221cb0ef41Sopenharmony_ci<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>); 15231cb0ef41Sopenharmony_cirequest.<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>]); 15241cb0ef41Sopenharmony_ci 15251cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> headerNames = request.<span class="hljs-title function_">getRawHeaderNames</span>(); 15261cb0ef41Sopenharmony_ci<span class="hljs-comment">// headerNames === ['Foo', 'Set-Cookie']</span></code> <button class="copy-button">copy</button></pre> 15271cb0ef41Sopenharmony_ci<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> 15281cb0ef41Sopenharmony_ci<div class="api_metadata"> 15291cb0ef41Sopenharmony_ci<span>Added in: v7.7.0</span> 15301cb0ef41Sopenharmony_ci</div> 15311cb0ef41Sopenharmony_ci<ul> 15321cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 15331cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 15341cb0ef41Sopenharmony_ci</ul> 15351cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the 15361cb0ef41Sopenharmony_cioutgoing headers. The header name matching is case-insensitive.</p> 15371cb0ef41Sopenharmony_ci<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> 15381cb0ef41Sopenharmony_ci<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> 15391cb0ef41Sopenharmony_ci<ul> 15401cb0ef41Sopenharmony_ci<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> 15411cb0ef41Sopenharmony_ci</ul> 15421cb0ef41Sopenharmony_ci<p>Limits maximum response headers count. If set to 0, no limit will be applied.</p> 15431cb0ef41Sopenharmony_ci<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> 15441cb0ef41Sopenharmony_ci<div class="api_metadata"> 15451cb0ef41Sopenharmony_ci<span>Added in: v0.4.0</span> 15461cb0ef41Sopenharmony_ci</div> 15471cb0ef41Sopenharmony_ci<ul> 15481cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request path.</li> 15491cb0ef41Sopenharmony_ci</ul> 15501cb0ef41Sopenharmony_ci<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> 15511cb0ef41Sopenharmony_ci<div class="api_metadata"> 15521cb0ef41Sopenharmony_ci<span>Added in: v0.1.97</span> 15531cb0ef41Sopenharmony_ci</div> 15541cb0ef41Sopenharmony_ci<ul> 15551cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request method.</li> 15561cb0ef41Sopenharmony_ci</ul> 15571cb0ef41Sopenharmony_ci<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> 15581cb0ef41Sopenharmony_ci<div class="api_metadata"> 15591cb0ef41Sopenharmony_ci<span>Added in: v14.5.0, v12.19.0</span> 15601cb0ef41Sopenharmony_ci</div> 15611cb0ef41Sopenharmony_ci<ul> 15621cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request host.</li> 15631cb0ef41Sopenharmony_ci</ul> 15641cb0ef41Sopenharmony_ci<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> 15651cb0ef41Sopenharmony_ci<div class="api_metadata"> 15661cb0ef41Sopenharmony_ci<span>Added in: v14.5.0, v12.19.0</span> 15671cb0ef41Sopenharmony_ci</div> 15681cb0ef41Sopenharmony_ci<ul> 15691cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request protocol.</li> 15701cb0ef41Sopenharmony_ci</ul> 15711cb0ef41Sopenharmony_ci<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> 15721cb0ef41Sopenharmony_ci<div class="api_metadata"> 15731cb0ef41Sopenharmony_ci<span>Added in: v1.6.0</span> 15741cb0ef41Sopenharmony_ci</div> 15751cb0ef41Sopenharmony_ci<ul> 15761cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 15771cb0ef41Sopenharmony_ci</ul> 15781cb0ef41Sopenharmony_ci<p>Removes a header that's already defined into headers object.</p> 15791cb0ef41Sopenharmony_ci<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> 15801cb0ef41Sopenharmony_ci<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> 15811cb0ef41Sopenharmony_ci<div class="api_metadata"> 15821cb0ef41Sopenharmony_ci<span>Added in: v13.0.0, v12.16.0</span> 15831cb0ef41Sopenharmony_ci</div> 15841cb0ef41Sopenharmony_ci<ul> 15851cb0ef41Sopenharmony_ci<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> 15861cb0ef41Sopenharmony_ci</ul> 15871cb0ef41Sopenharmony_ci<p>When sending request through a keep-alive enabled agent, the underlying socket 15881cb0ef41Sopenharmony_cimight be reused. But if server closes connection at unfortunate time, client 15891cb0ef41Sopenharmony_cimay run into a 'ECONNRESET' error.</p> 15901cb0ef41Sopenharmony_ci 15911cb0ef41Sopenharmony_ci<pre class="with-34-chars"><input class="js-flavor-selector" 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>; 15921cb0ef41Sopenharmony_ci 15931cb0ef41Sopenharmony_ci<span class="hljs-comment">// Server has a 5 seconds keep-alive timeout by default</span> 15941cb0ef41Sopenharmony_cihttp 15951cb0ef41Sopenharmony_ci .<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 15961cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\n'</span>); 15971cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(); 15981cb0ef41Sopenharmony_ci }) 15991cb0ef41Sopenharmony_ci .<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>); 16001cb0ef41Sopenharmony_ci 16011cb0ef41Sopenharmony_ci<span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> { 16021cb0ef41Sopenharmony_ci <span class="hljs-comment">// Adapting a keep-alive agent</span> 16031cb0ef41Sopenharmony_ci 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> { 16041cb0ef41Sopenharmony_ci 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> { 16051cb0ef41Sopenharmony_ci <span class="hljs-comment">// Do nothing</span> 16061cb0ef41Sopenharmony_ci }); 16071cb0ef41Sopenharmony_ci }); 16081cb0ef41Sopenharmony_ci}, <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>); 16091cb0ef41Sopenharmony_ci 16101cb0ef41Sopenharmony_ci<span class="hljs-comment">// Server has a 5 seconds keep-alive timeout by default</span> 16111cb0ef41Sopenharmony_cihttp 16121cb0ef41Sopenharmony_ci .<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 16131cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\n'</span>); 16141cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(); 16151cb0ef41Sopenharmony_ci }) 16161cb0ef41Sopenharmony_ci .<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>); 16171cb0ef41Sopenharmony_ci 16181cb0ef41Sopenharmony_ci<span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> { 16191cb0ef41Sopenharmony_ci <span class="hljs-comment">// Adapting a keep-alive agent</span> 16201cb0ef41Sopenharmony_ci 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> { 16211cb0ef41Sopenharmony_ci 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> { 16221cb0ef41Sopenharmony_ci <span class="hljs-comment">// Do nothing</span> 16231cb0ef41Sopenharmony_ci }); 16241cb0ef41Sopenharmony_ci }); 16251cb0ef41Sopenharmony_ci}, <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> 16261cb0ef41Sopenharmony_ci<p>By marking a request whether it reused socket or not, we can do 16271cb0ef41Sopenharmony_ciautomatic error retry base on it.</p> 16281cb0ef41Sopenharmony_ci 16291cb0ef41Sopenharmony_ci<pre class="with-50-chars"><input class="js-flavor-selector" 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>; 16301cb0ef41Sopenharmony_ci<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> }); 16311cb0ef41Sopenharmony_ci 16321cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">retriableRequest</span>(<span class="hljs-params"></span>) { 16331cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> req = http 16341cb0ef41Sopenharmony_ci .<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> { 16351cb0ef41Sopenharmony_ci <span class="hljs-comment">// ...</span> 16361cb0ef41Sopenharmony_ci }) 16371cb0ef41Sopenharmony_ci .<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 16381cb0ef41Sopenharmony_ci <span class="hljs-comment">// Check if retry is needed</span> 16391cb0ef41Sopenharmony_ci <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>) { 16401cb0ef41Sopenharmony_ci <span class="hljs-title function_">retriableRequest</span>(); 16411cb0ef41Sopenharmony_ci } 16421cb0ef41Sopenharmony_ci }); 16431cb0ef41Sopenharmony_ci} 16441cb0ef41Sopenharmony_ci 16451cb0ef41Sopenharmony_ci<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>); 16461cb0ef41Sopenharmony_ci<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> }); 16471cb0ef41Sopenharmony_ci 16481cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">retriableRequest</span>(<span class="hljs-params"></span>) { 16491cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> req = http 16501cb0ef41Sopenharmony_ci .<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> { 16511cb0ef41Sopenharmony_ci <span class="hljs-comment">// ...</span> 16521cb0ef41Sopenharmony_ci }) 16531cb0ef41Sopenharmony_ci .<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 16541cb0ef41Sopenharmony_ci <span class="hljs-comment">// Check if retry is needed</span> 16551cb0ef41Sopenharmony_ci <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>) { 16561cb0ef41Sopenharmony_ci <span class="hljs-title function_">retriableRequest</span>(); 16571cb0ef41Sopenharmony_ci } 16581cb0ef41Sopenharmony_ci }); 16591cb0ef41Sopenharmony_ci} 16601cb0ef41Sopenharmony_ci 16611cb0ef41Sopenharmony_ci<span class="hljs-title function_">retriableRequest</span>();</code><button class="copy-button">copy</button></pre> 16621cb0ef41Sopenharmony_ci<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> 16631cb0ef41Sopenharmony_ci<div class="api_metadata"> 16641cb0ef41Sopenharmony_ci<span>Added in: v1.6.0</span> 16651cb0ef41Sopenharmony_ci</div> 16661cb0ef41Sopenharmony_ci<ul> 16671cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 16681cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 16691cb0ef41Sopenharmony_ci</ul> 16701cb0ef41Sopenharmony_ci<p>Sets a single header value for headers object. If this header already exists in 16711cb0ef41Sopenharmony_cithe to-be-sent headers, its value will be replaced. Use an array of strings 16721cb0ef41Sopenharmony_cihere to send multiple headers with the same name. Non-string values will be 16731cb0ef41Sopenharmony_cistored without modification. Therefore, <a href="#requestgetheadername"><code>request.getHeader()</code></a> may return 16741cb0ef41Sopenharmony_cinon-string values. However, the non-string values will be converted to strings 16751cb0ef41Sopenharmony_cifor network transmission.</p> 16761cb0ef41Sopenharmony_ci<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> 16771cb0ef41Sopenharmony_ci<p>or</p> 16781cb0ef41Sopenharmony_ci<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> 16791cb0ef41Sopenharmony_ci<p>When the value is a string an exception will be thrown if it contains 16801cb0ef41Sopenharmony_cicharacters outside the <code>latin1</code> encoding.</p> 16811cb0ef41Sopenharmony_ci<p>If you need to pass UTF-8 characters in the value please encode the value 16821cb0ef41Sopenharmony_ciusing the <a href="https://www.rfc-editor.org/rfc/rfc8187.txt">RFC 8187</a> standard.</p> 16831cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> filename = <span class="hljs-string">'Rock .txt'</span>; 16841cb0ef41Sopenharmony_cirequest.<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> 16851cb0ef41Sopenharmony_ci<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> 16861cb0ef41Sopenharmony_ci<div class="api_metadata"> 16871cb0ef41Sopenharmony_ci<span>Added in: v0.5.9</span> 16881cb0ef41Sopenharmony_ci</div> 16891cb0ef41Sopenharmony_ci<ul> 16901cb0ef41Sopenharmony_ci<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 16911cb0ef41Sopenharmony_ci</ul> 16921cb0ef41Sopenharmony_ci<p>Once a socket is assigned to this request and is connected 16931cb0ef41Sopenharmony_ci<a href="net.html#socketsetnodelaynodelay"><code>socket.setNoDelay()</code></a> will be called.</p> 16941cb0ef41Sopenharmony_ci<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> 16951cb0ef41Sopenharmony_ci<div class="api_metadata"> 16961cb0ef41Sopenharmony_ci<span>Added in: v0.5.9</span> 16971cb0ef41Sopenharmony_ci</div> 16981cb0ef41Sopenharmony_ci<ul> 16991cb0ef41Sopenharmony_ci<li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 17001cb0ef41Sopenharmony_ci<li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 17011cb0ef41Sopenharmony_ci</ul> 17021cb0ef41Sopenharmony_ci<p>Once a socket is assigned to this request and is connected 17031cb0ef41Sopenharmony_ci<a href="net.html#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive()</code></a> will be called.</p> 17041cb0ef41Sopenharmony_ci<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> 17051cb0ef41Sopenharmony_ci<div class="api_metadata"> 17061cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 17071cb0ef41Sopenharmony_ci<table> 17081cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 17091cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 17101cb0ef41Sopenharmony_ci<td><p>Consistently set socket timeout only when the socket connects.</p></td></tr> 17111cb0ef41Sopenharmony_ci<tr><td>v0.5.9</td> 17121cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.9</span></p></td></tr> 17131cb0ef41Sopenharmony_ci</tbody></table> 17141cb0ef41Sopenharmony_ci</details> 17151cb0ef41Sopenharmony_ci</div> 17161cb0ef41Sopenharmony_ci<ul> 17171cb0ef41Sopenharmony_ci<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> 17181cb0ef41Sopenharmony_ci<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. 17191cb0ef41Sopenharmony_ciSame as binding to the <code>'timeout'</code> event.</li> 17201cb0ef41Sopenharmony_ci<li>Returns: <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 17211cb0ef41Sopenharmony_ci</ul> 17221cb0ef41Sopenharmony_ci<p>Once a socket is assigned to this request and is connected 17231cb0ef41Sopenharmony_ci<a href="net.html#socketsettimeouttimeout-callback"><code>socket.setTimeout()</code></a> will be called.</p> 17241cb0ef41Sopenharmony_ci<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> 17251cb0ef41Sopenharmony_ci<div class="api_metadata"> 17261cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 17271cb0ef41Sopenharmony_ci</div> 17281cb0ef41Sopenharmony_ci<ul> 17291cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 17301cb0ef41Sopenharmony_ci</ul> 17311cb0ef41Sopenharmony_ci<p>Reference to the underlying socket. Usually users will not want to access 17321cb0ef41Sopenharmony_cithis property. In particular, the socket will not emit <code>'readable'</code> events 17331cb0ef41Sopenharmony_cibecause of how the protocol parser attaches to the socket.</p> 17341cb0ef41Sopenharmony_ci 17351cb0ef41Sopenharmony_ci<pre class="with-34-chars"><input class="js-flavor-selector" 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>; 17361cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = { 17371cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'www.google.com'</span>, 17381cb0ef41Sopenharmony_ci}; 17391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">get</span>(options); 17401cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">end</span>(); 17411cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">once</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 17421cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localAddress</span>; 17431cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localPort</span>; 17441cb0ef41Sopenharmony_ci <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>); 17451cb0ef41Sopenharmony_ci <span class="hljs-comment">// Consume response object</span> 17461cb0ef41Sopenharmony_ci});</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>); 17471cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = { 17481cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'www.google.com'</span>, 17491cb0ef41Sopenharmony_ci}; 17501cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">get</span>(options); 17511cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">end</span>(); 17521cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">once</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 17531cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localAddress</span>; 17541cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localPort</span>; 17551cb0ef41Sopenharmony_ci <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>); 17561cb0ef41Sopenharmony_ci <span class="hljs-comment">// Consume response object</span> 17571cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 17581cb0ef41Sopenharmony_ci<p>This property is guaranteed to be an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 17591cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specified a socket 17601cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 17611cb0ef41Sopenharmony_ci<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> 17621cb0ef41Sopenharmony_ci<div class="api_metadata"> 17631cb0ef41Sopenharmony_ci<span>Added in: v13.2.0, v12.16.0</span> 17641cb0ef41Sopenharmony_ci</div> 17651cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writableuncork"><code>writable.uncork()</code></a>.</p> 17661cb0ef41Sopenharmony_ci<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> 17671cb0ef41Sopenharmony_ci<div class="api_metadata"> 17681cb0ef41Sopenharmony_ci<span>Added in: v12.9.0</span> 17691cb0ef41Sopenharmony_ci</div> 17701cb0ef41Sopenharmony_ci<ul> 17711cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 17721cb0ef41Sopenharmony_ci</ul> 17731cb0ef41Sopenharmony_ci<p>Is <code>true</code> after <a href="#requestenddata-encoding-callback"><code>request.end()</code></a> has been called. This property 17741cb0ef41Sopenharmony_cidoes not indicate whether the data has been flushed, for this use 17751cb0ef41Sopenharmony_ci<a href="#requestwritablefinished"><code>request.writableFinished</code></a> instead.</p> 17761cb0ef41Sopenharmony_ci<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> 17771cb0ef41Sopenharmony_ci<div class="api_metadata"> 17781cb0ef41Sopenharmony_ci<span>Added in: v12.7.0</span> 17791cb0ef41Sopenharmony_ci</div> 17801cb0ef41Sopenharmony_ci<ul> 17811cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 17821cb0ef41Sopenharmony_ci</ul> 17831cb0ef41Sopenharmony_ci<p>Is <code>true</code> if all data has been flushed to the underlying system, immediately 17841cb0ef41Sopenharmony_cibefore the <a href="#event-finish"><code>'finish'</code></a> event is emitted.</p> 17851cb0ef41Sopenharmony_ci<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> 17861cb0ef41Sopenharmony_ci<div class="api_metadata"> 17871cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 17881cb0ef41Sopenharmony_ci<table> 17891cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 17901cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 17911cb0ef41Sopenharmony_ci<td><p>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 17921cb0ef41Sopenharmony_ci<tr><td>v0.1.29</td> 17931cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.29</span></p></td></tr> 17941cb0ef41Sopenharmony_ci</tbody></table> 17951cb0ef41Sopenharmony_ci</details> 17961cb0ef41Sopenharmony_ci</div> 17971cb0ef41Sopenharmony_ci<ul> 17981cb0ef41Sopenharmony_ci<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> 17991cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 18001cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 18011cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 18021cb0ef41Sopenharmony_ci</ul> 18031cb0ef41Sopenharmony_ci<p>Sends a chunk of the body. This method can be called multiple times. If no 18041cb0ef41Sopenharmony_ci<code>Content-Length</code> is set, data will automatically be encoded in HTTP Chunked 18051cb0ef41Sopenharmony_citransfer encoding, so that server knows when the data ends. The 18061cb0ef41Sopenharmony_ci<code>Transfer-Encoding: chunked</code> header is added. Calling <a href="#requestenddata-encoding-callback"><code>request.end()</code></a> 18071cb0ef41Sopenharmony_ciis necessary to finish sending the request.</p> 18081cb0ef41Sopenharmony_ci<p>The <code>encoding</code> argument is optional and only applies when <code>chunk</code> is a string. 18091cb0ef41Sopenharmony_ciDefaults to <code>'utf8'</code>.</p> 18101cb0ef41Sopenharmony_ci<p>The <code>callback</code> argument is optional and will be called when this chunk of data 18111cb0ef41Sopenharmony_ciis flushed, but only if the chunk is non-empty.</p> 18121cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel 18131cb0ef41Sopenharmony_cibuffer. Returns <code>false</code> if all or part of the data was queued in user memory. 18141cb0ef41Sopenharmony_ci<code>'drain'</code> will be emitted when the buffer is free again.</p> 18151cb0ef41Sopenharmony_ci<p>When <code>write</code> function is called with empty string or buffer, it does 18161cb0ef41Sopenharmony_cinothing and waits for more input.</p> 18171cb0ef41Sopenharmony_ci</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> 18181cb0ef41Sopenharmony_ci<div class="api_metadata"> 18191cb0ef41Sopenharmony_ci<span>Added in: v0.1.17</span> 18201cb0ef41Sopenharmony_ci</div> 18211cb0ef41Sopenharmony_ci<ul> 18221cb0ef41Sopenharmony_ci<li>Extends: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 18231cb0ef41Sopenharmony_ci</ul> 18241cb0ef41Sopenharmony_ci<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> 18251cb0ef41Sopenharmony_ci<div class="api_metadata"> 18261cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 18271cb0ef41Sopenharmony_ci</div> 18281cb0ef41Sopenharmony_ci<ul> 18291cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 18301cb0ef41Sopenharmony_ci<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 18311cb0ef41Sopenharmony_ci</ul> 18321cb0ef41Sopenharmony_ci<p>Emitted each time a request with an HTTP <code>Expect: 100-continue</code> is received. 18331cb0ef41Sopenharmony_ciIf this event is not listened for, the server will automatically respond 18341cb0ef41Sopenharmony_ciwith a <code>100 Continue</code> as appropriate.</p> 18351cb0ef41Sopenharmony_ci<p>Handling this event involves calling <a href="#responsewritecontinue"><code>response.writeContinue()</code></a> if the 18361cb0ef41Sopenharmony_ciclient should continue to send the request body, or generating an appropriate 18371cb0ef41Sopenharmony_ciHTTP response (e.g. 400 Bad Request) if the client should not continue to send 18381cb0ef41Sopenharmony_cithe request body.</p> 18391cb0ef41Sopenharmony_ci<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will 18401cb0ef41Sopenharmony_cinot be emitted.</p> 18411cb0ef41Sopenharmony_ci<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> 18421cb0ef41Sopenharmony_ci<div class="api_metadata"> 18431cb0ef41Sopenharmony_ci<span>Added in: v5.5.0</span> 18441cb0ef41Sopenharmony_ci</div> 18451cb0ef41Sopenharmony_ci<ul> 18461cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 18471cb0ef41Sopenharmony_ci<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 18481cb0ef41Sopenharmony_ci</ul> 18491cb0ef41Sopenharmony_ci<p>Emitted each time a request with an HTTP <code>Expect</code> header is received, where the 18501cb0ef41Sopenharmony_civalue is not <code>100-continue</code>. If this event is not listened for, the server will 18511cb0ef41Sopenharmony_ciautomatically respond with a <code>417 Expectation Failed</code> as appropriate.</p> 18521cb0ef41Sopenharmony_ci<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will 18531cb0ef41Sopenharmony_cinot be emitted.</p> 18541cb0ef41Sopenharmony_ci<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> 18551cb0ef41Sopenharmony_ci<div class="api_metadata"> 18561cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 18571cb0ef41Sopenharmony_ci<table> 18581cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 18591cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 18601cb0ef41Sopenharmony_ci<td><p>The default behavior will return a 431 Request Header Fields Too Large if a HPE_HEADER_OVERFLOW error occurs.</p></td></tr> 18611cb0ef41Sopenharmony_ci<tr><td>v9.4.0</td> 18621cb0ef41Sopenharmony_ci<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> 18631cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 18641cb0ef41Sopenharmony_ci<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> 18651cb0ef41Sopenharmony_ci<tr><td>v0.1.94</td> 18661cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.94</span></p></td></tr> 18671cb0ef41Sopenharmony_ci</tbody></table> 18681cb0ef41Sopenharmony_ci</details> 18691cb0ef41Sopenharmony_ci</div> 18701cb0ef41Sopenharmony_ci<ul> 18711cb0ef41Sopenharmony_ci<li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 18721cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 18731cb0ef41Sopenharmony_ci</ul> 18741cb0ef41Sopenharmony_ci<p>If a client connection emits an <code>'error'</code> event, it will be forwarded here. 18751cb0ef41Sopenharmony_ciListener of this event is responsible for closing/destroying the underlying 18761cb0ef41Sopenharmony_cisocket. For example, one may wish to more gracefully close the socket with a 18771cb0ef41Sopenharmony_cicustom HTTP response instead of abruptly severing the connection. The socket 18781cb0ef41Sopenharmony_ci<strong>must be closed or destroyed</strong> before the listener ends.</p> 18791cb0ef41Sopenharmony_ci<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 18801cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 18811cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 18821cb0ef41Sopenharmony_ci<p>Default behavior is to try close the socket with a HTTP '400 Bad Request', 18831cb0ef41Sopenharmony_cior a HTTP '431 Request Header Fields Too Large' in the case of a 18841cb0ef41Sopenharmony_ci<a href="errors.html#hpe_header_overflow"><code>HPE_HEADER_OVERFLOW</code></a> error. If the socket is not writable or headers 18851cb0ef41Sopenharmony_ciof the current attached <a href="#class-httpserverresponse"><code>http.ServerResponse</code></a> has been sent, it is 18861cb0ef41Sopenharmony_ciimmediately destroyed.</p> 18871cb0ef41Sopenharmony_ci<p><code>socket</code> is the <a href="net.html#class-netsocket"><code>net.Socket</code></a> object that the error originated from.</p> 18881cb0ef41Sopenharmony_ci 18891cb0ef41Sopenharmony_ci<pre class="with-34-chars"><input class="js-flavor-selector" 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>; 18901cb0ef41Sopenharmony_ci 18911cb0ef41Sopenharmony_ci<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> { 18921cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(); 18931cb0ef41Sopenharmony_ci}); 18941cb0ef41Sopenharmony_ciserver.<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> { 18951cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</span>); 18961cb0ef41Sopenharmony_ci}); 18971cb0ef41Sopenharmony_ciserver.<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>); 18981cb0ef41Sopenharmony_ci 18991cb0ef41Sopenharmony_ci<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> { 19001cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(); 19011cb0ef41Sopenharmony_ci}); 19021cb0ef41Sopenharmony_ciserver.<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> { 19031cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</span>); 19041cb0ef41Sopenharmony_ci}); 19051cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><button class="copy-button">copy</button></pre> 19061cb0ef41Sopenharmony_ci<p>When the <code>'clientError'</code> event occurs, there is no <code>request</code> or <code>response</code> 19071cb0ef41Sopenharmony_ciobject, so any HTTP response sent, including response headers and payload, 19081cb0ef41Sopenharmony_ci<em>must</em> be written directly to the <code>socket</code> object. Care must be taken to 19091cb0ef41Sopenharmony_ciensure the response is a properly formatted HTTP response message.</p> 19101cb0ef41Sopenharmony_ci<p><code>err</code> is an instance of <code>Error</code> with two extra columns:</p> 19111cb0ef41Sopenharmony_ci<ul> 19121cb0ef41Sopenharmony_ci<li><code>bytesParsed</code>: the bytes count of request packet that Node.js may have parsed 19131cb0ef41Sopenharmony_cicorrectly;</li> 19141cb0ef41Sopenharmony_ci<li><code>rawPacket</code>: the raw packet of current request.</li> 19151cb0ef41Sopenharmony_ci</ul> 19161cb0ef41Sopenharmony_ci<p>In some cases, the client has already received the response and/or the socket 19171cb0ef41Sopenharmony_cihas already been destroyed, like in case of <code>ECONNRESET</code> errors. Before 19181cb0ef41Sopenharmony_citrying to send data to the socket, it is better to check that it is still 19191cb0ef41Sopenharmony_ciwritable.</p> 19201cb0ef41Sopenharmony_ci<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> { 19211cb0ef41Sopenharmony_ci <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>) { 19221cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span>; 19231cb0ef41Sopenharmony_ci } 19241cb0ef41Sopenharmony_ci 19251cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</span>); 19261cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 19271cb0ef41Sopenharmony_ci<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> 19281cb0ef41Sopenharmony_ci<div class="api_metadata"> 19291cb0ef41Sopenharmony_ci<span>Added in: v0.1.4</span> 19301cb0ef41Sopenharmony_ci</div> 19311cb0ef41Sopenharmony_ci<p>Emitted when the server closes.</p> 19321cb0ef41Sopenharmony_ci<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> 19331cb0ef41Sopenharmony_ci<div class="api_metadata"> 19341cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 19351cb0ef41Sopenharmony_ci</div> 19361cb0ef41Sopenharmony_ci<ul> 19371cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Arguments for the HTTP request, as it is in 19381cb0ef41Sopenharmony_cithe <a href="#event-request"><code>'request'</code></a> event</li> 19391cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a> Network socket between the server and client</li> 19401cb0ef41Sopenharmony_ci<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> 19411cb0ef41Sopenharmony_ci</ul> 19421cb0ef41Sopenharmony_ci<p>Emitted each time a client requests an HTTP <code>CONNECT</code> method. If this event is 19431cb0ef41Sopenharmony_cinot listened for, then clients requesting a <code>CONNECT</code> method will have their 19441cb0ef41Sopenharmony_ciconnections closed.</p> 19451cb0ef41Sopenharmony_ci<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 19461cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 19471cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 19481cb0ef41Sopenharmony_ci<p>After this event is emitted, the request's socket will not have a <code>'data'</code> 19491cb0ef41Sopenharmony_cievent listener, meaning it will need to be bound in order to handle data 19501cb0ef41Sopenharmony_cisent to the server on that socket.</p> 19511cb0ef41Sopenharmony_ci<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> 19521cb0ef41Sopenharmony_ci<div class="api_metadata"> 19531cb0ef41Sopenharmony_ci<span>Added in: v0.1.0</span> 19541cb0ef41Sopenharmony_ci</div> 19551cb0ef41Sopenharmony_ci<ul> 19561cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 19571cb0ef41Sopenharmony_ci</ul> 19581cb0ef41Sopenharmony_ci<p>This event is emitted when a new TCP stream is established. <code>socket</code> is 19591cb0ef41Sopenharmony_citypically an object of type <a href="net.html#class-netsocket"><code>net.Socket</code></a>. Usually users will not want to 19601cb0ef41Sopenharmony_ciaccess this event. In particular, the socket will not emit <code>'readable'</code> events 19611cb0ef41Sopenharmony_cibecause of how the protocol parser attaches to the socket. The <code>socket</code> can 19621cb0ef41Sopenharmony_cialso be accessed at <code>request.socket</code>.</p> 19631cb0ef41Sopenharmony_ci<p>This event can also be explicitly emitted by users to inject connections 19641cb0ef41Sopenharmony_ciinto the HTTP server. In that case, any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream can be passed.</p> 19651cb0ef41Sopenharmony_ci<p>If <code>socket.setTimeout()</code> is called here, the timeout will be replaced with 19661cb0ef41Sopenharmony_ci<code>server.keepAliveTimeout</code> when the socket has served a request (if 19671cb0ef41Sopenharmony_ci<code>server.keepAliveTimeout</code> is non-zero).</p> 19681cb0ef41Sopenharmony_ci<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 19691cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 19701cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 19711cb0ef41Sopenharmony_ci<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> 19721cb0ef41Sopenharmony_ci<div class="api_metadata"> 19731cb0ef41Sopenharmony_ci<span>Added in: v18.7.0</span> 19741cb0ef41Sopenharmony_ci</div> 19751cb0ef41Sopenharmony_ci<ul> 19761cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Arguments for the HTTP request, as it is in 19771cb0ef41Sopenharmony_cithe <a href="#event-request"><code>'request'</code></a> event</li> 19781cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a> Network socket between the server and client</li> 19791cb0ef41Sopenharmony_ci</ul> 19801cb0ef41Sopenharmony_ci<p>When the number of requests on a socket reaches the threshold of 19811cb0ef41Sopenharmony_ci<code>server.maxRequestsPerSocket</code>, the server will drop new requests 19821cb0ef41Sopenharmony_ciand emit <code>'dropRequest'</code> event instead, then send <code>503</code> to client.</p> 19831cb0ef41Sopenharmony_ci<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> 19841cb0ef41Sopenharmony_ci<div class="api_metadata"> 19851cb0ef41Sopenharmony_ci<span>Added in: v0.1.0</span> 19861cb0ef41Sopenharmony_ci</div> 19871cb0ef41Sopenharmony_ci<ul> 19881cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 19891cb0ef41Sopenharmony_ci<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 19901cb0ef41Sopenharmony_ci</ul> 19911cb0ef41Sopenharmony_ci<p>Emitted each time there is a request. There may be multiple requests 19921cb0ef41Sopenharmony_ciper connection (in the case of HTTP Keep-Alive connections).</p> 19931cb0ef41Sopenharmony_ci<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> 19941cb0ef41Sopenharmony_ci<div class="api_metadata"> 19951cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 19961cb0ef41Sopenharmony_ci<table> 19971cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 19981cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 19991cb0ef41Sopenharmony_ci<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> 20001cb0ef41Sopenharmony_ci<tr><td>v0.1.94</td> 20011cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.94</span></p></td></tr> 20021cb0ef41Sopenharmony_ci</tbody></table> 20031cb0ef41Sopenharmony_ci</details> 20041cb0ef41Sopenharmony_ci</div> 20051cb0ef41Sopenharmony_ci<ul> 20061cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Arguments for the HTTP request, as it is in 20071cb0ef41Sopenharmony_cithe <a href="#event-request"><code>'request'</code></a> event</li> 20081cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a> Network socket between the server and client</li> 20091cb0ef41Sopenharmony_ci<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> 20101cb0ef41Sopenharmony_ci</ul> 20111cb0ef41Sopenharmony_ci<p>Emitted each time a client requests an HTTP upgrade. Listening to this event 20121cb0ef41Sopenharmony_ciis optional and clients cannot insist on a protocol change.</p> 20131cb0ef41Sopenharmony_ci<p>After this event is emitted, the request's socket will not have a <code>'data'</code> 20141cb0ef41Sopenharmony_cievent listener, meaning it will need to be bound in order to handle data 20151cb0ef41Sopenharmony_cisent to the server on that socket.</p> 20161cb0ef41Sopenharmony_ci<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 20171cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 20181cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 20191cb0ef41Sopenharmony_ci<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> 20201cb0ef41Sopenharmony_ci<div class="api_metadata"> 20211cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 20221cb0ef41Sopenharmony_ci</div> 20231cb0ef41Sopenharmony_ci<ul> 20241cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 20251cb0ef41Sopenharmony_ci</ul> 20261cb0ef41Sopenharmony_ci<p>Stops the server from accepting new connections. See <a href="net.html#serverclosecallback"><code>net.Server.close()</code></a>.</p> 20271cb0ef41Sopenharmony_ci<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> 20281cb0ef41Sopenharmony_ci<div class="api_metadata"> 20291cb0ef41Sopenharmony_ci<span>Added in: v18.2.0</span> 20301cb0ef41Sopenharmony_ci</div> 20311cb0ef41Sopenharmony_ci<p>Closes all connections connected to this server.</p> 20321cb0ef41Sopenharmony_ci<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> 20331cb0ef41Sopenharmony_ci<div class="api_metadata"> 20341cb0ef41Sopenharmony_ci<span>Added in: v18.2.0</span> 20351cb0ef41Sopenharmony_ci</div> 20361cb0ef41Sopenharmony_ci<p>Closes all connections connected to this server which are not sending a request 20371cb0ef41Sopenharmony_cior waiting for a response.</p> 20381cb0ef41Sopenharmony_ci<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> 20391cb0ef41Sopenharmony_ci<div class="api_metadata"> 20401cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 20411cb0ef41Sopenharmony_ci<table> 20421cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 20431cb0ef41Sopenharmony_ci<tr><td>v18.14.0</td> 20441cb0ef41Sopenharmony_ci<td><p>The default is now set to the minimum between 60000 (60 seconds) or <code>requestTimeout</code>.</p></td></tr> 20451cb0ef41Sopenharmony_ci<tr><td>v11.3.0, v10.14.0</td> 20461cb0ef41Sopenharmony_ci<td><p><span>Added in: v11.3.0, v10.14.0</span></p></td></tr> 20471cb0ef41Sopenharmony_ci</tbody></table> 20481cb0ef41Sopenharmony_ci</details> 20491cb0ef41Sopenharmony_ci</div> 20501cb0ef41Sopenharmony_ci<ul> 20511cb0ef41Sopenharmony_ci<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> 20521cb0ef41Sopenharmony_ci</ul> 20531cb0ef41Sopenharmony_ci<p>Limit the amount of time the parser will wait to receive the complete HTTP 20541cb0ef41Sopenharmony_ciheaders.</p> 20551cb0ef41Sopenharmony_ci<p>If the timeout expires, the server responds with status 408 without 20561cb0ef41Sopenharmony_ciforwarding the request to the request listener and then closes the connection.</p> 20571cb0ef41Sopenharmony_ci<p>It must be set to a non-zero value (e.g. 120 seconds) to protect against 20581cb0ef41Sopenharmony_cipotential Denial-of-Service attacks in case the server is deployed without a 20591cb0ef41Sopenharmony_cireverse proxy in front.</p> 20601cb0ef41Sopenharmony_ci<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> 20611cb0ef41Sopenharmony_ci<p>Starts the HTTP server listening for connections. 20621cb0ef41Sopenharmony_ciThis 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> 20631cb0ef41Sopenharmony_ci<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> 20641cb0ef41Sopenharmony_ci<div class="api_metadata"> 20651cb0ef41Sopenharmony_ci<span>Added in: v5.7.0</span> 20661cb0ef41Sopenharmony_ci</div> 20671cb0ef41Sopenharmony_ci<ul> 20681cb0ef41Sopenharmony_ci<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> 20691cb0ef41Sopenharmony_ci</ul> 20701cb0ef41Sopenharmony_ci<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> 20711cb0ef41Sopenharmony_ci<div class="api_metadata"> 20721cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 20731cb0ef41Sopenharmony_ci</div> 20741cb0ef41Sopenharmony_ci<ul> 20751cb0ef41Sopenharmony_ci<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> 20761cb0ef41Sopenharmony_ci</ul> 20771cb0ef41Sopenharmony_ci<p>Limits maximum incoming headers count. If set to 0, no limit will be applied.</p> 20781cb0ef41Sopenharmony_ci<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> 20791cb0ef41Sopenharmony_ci<div class="api_metadata"> 20801cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 20811cb0ef41Sopenharmony_ci<table> 20821cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 20831cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 20841cb0ef41Sopenharmony_ci<td><p>The default request timeout changed from no timeout to 300s (5 minutes).</p></td></tr> 20851cb0ef41Sopenharmony_ci<tr><td>v14.11.0</td> 20861cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.11.0</span></p></td></tr> 20871cb0ef41Sopenharmony_ci</tbody></table> 20881cb0ef41Sopenharmony_ci</details> 20891cb0ef41Sopenharmony_ci</div> 20901cb0ef41Sopenharmony_ci<ul> 20911cb0ef41Sopenharmony_ci<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> 20921cb0ef41Sopenharmony_ci</ul> 20931cb0ef41Sopenharmony_ci<p>Sets the timeout value in milliseconds for receiving the entire request from 20941cb0ef41Sopenharmony_cithe client.</p> 20951cb0ef41Sopenharmony_ci<p>If the timeout expires, the server responds with status 408 without 20961cb0ef41Sopenharmony_ciforwarding the request to the request listener and then closes the connection.</p> 20971cb0ef41Sopenharmony_ci<p>It must be set to a non-zero value (e.g. 120 seconds) to protect against 20981cb0ef41Sopenharmony_cipotential Denial-of-Service attacks in case the server is deployed without a 20991cb0ef41Sopenharmony_cireverse proxy in front.</p> 21001cb0ef41Sopenharmony_ci<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> 21011cb0ef41Sopenharmony_ci<div class="api_metadata"> 21021cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 21031cb0ef41Sopenharmony_ci<table> 21041cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 21051cb0ef41Sopenharmony_ci<tr><td>v13.0.0</td> 21061cb0ef41Sopenharmony_ci<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr> 21071cb0ef41Sopenharmony_ci<tr><td>v0.9.12</td> 21081cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.9.12</span></p></td></tr> 21091cb0ef41Sopenharmony_ci</tbody></table> 21101cb0ef41Sopenharmony_ci</details> 21111cb0ef41Sopenharmony_ci</div> 21121cb0ef41Sopenharmony_ci<ul> 21131cb0ef41Sopenharmony_ci<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> 21141cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 21151cb0ef41Sopenharmony_ci<li>Returns: <a href="http.html#class-httpserver" class="type"><http.Server></a></li> 21161cb0ef41Sopenharmony_ci</ul> 21171cb0ef41Sopenharmony_ci<p>Sets the timeout value for sockets, and emits a <code>'timeout'</code> event on 21181cb0ef41Sopenharmony_cithe Server object, passing the socket as an argument, if a timeout 21191cb0ef41Sopenharmony_cioccurs.</p> 21201cb0ef41Sopenharmony_ci<p>If there is a <code>'timeout'</code> event listener on the Server object, then it 21211cb0ef41Sopenharmony_ciwill be called with the timed-out socket as an argument.</p> 21221cb0ef41Sopenharmony_ci<p>By default, the Server does not timeout sockets. However, if a callback 21231cb0ef41Sopenharmony_ciis assigned to the Server's <code>'timeout'</code> event, timeouts must be handled 21241cb0ef41Sopenharmony_ciexplicitly.</p> 21251cb0ef41Sopenharmony_ci<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> 21261cb0ef41Sopenharmony_ci<div class="api_metadata"> 21271cb0ef41Sopenharmony_ci<span>Added in: v16.10.0</span> 21281cb0ef41Sopenharmony_ci</div> 21291cb0ef41Sopenharmony_ci<ul> 21301cb0ef41Sopenharmony_ci<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> 21311cb0ef41Sopenharmony_ci</ul> 21321cb0ef41Sopenharmony_ci<p>The maximum number of requests socket can handle 21331cb0ef41Sopenharmony_cibefore closing keep alive connection.</p> 21341cb0ef41Sopenharmony_ci<p>A value of <code>0</code> will disable the limit.</p> 21351cb0ef41Sopenharmony_ci<p>When the limit is reached it will set the <code>Connection</code> header value to <code>close</code>, 21361cb0ef41Sopenharmony_cibut will not actually close the connection, subsequent requests sent 21371cb0ef41Sopenharmony_ciafter the limit is reached will get <code>503 Service Unavailable</code> as a response.</p> 21381cb0ef41Sopenharmony_ci<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> 21391cb0ef41Sopenharmony_ci<div class="api_metadata"> 21401cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 21411cb0ef41Sopenharmony_ci<table> 21421cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 21431cb0ef41Sopenharmony_ci<tr><td>v13.0.0</td> 21441cb0ef41Sopenharmony_ci<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr> 21451cb0ef41Sopenharmony_ci<tr><td>v0.9.12</td> 21461cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.9.12</span></p></td></tr> 21471cb0ef41Sopenharmony_ci</tbody></table> 21481cb0ef41Sopenharmony_ci</details> 21491cb0ef41Sopenharmony_ci</div> 21501cb0ef41Sopenharmony_ci<ul> 21511cb0ef41Sopenharmony_ci<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> 21521cb0ef41Sopenharmony_ci</ul> 21531cb0ef41Sopenharmony_ci<p>The number of milliseconds of inactivity before a socket is presumed 21541cb0ef41Sopenharmony_cito have timed out.</p> 21551cb0ef41Sopenharmony_ci<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p> 21561cb0ef41Sopenharmony_ci<p>The socket timeout logic is set up on connection, so changing this 21571cb0ef41Sopenharmony_civalue only affects new connections to the server, not any existing connections.</p> 21581cb0ef41Sopenharmony_ci<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> 21591cb0ef41Sopenharmony_ci<div class="api_metadata"> 21601cb0ef41Sopenharmony_ci<span>Added in: v8.0.0</span> 21611cb0ef41Sopenharmony_ci</div> 21621cb0ef41Sopenharmony_ci<ul> 21631cb0ef41Sopenharmony_ci<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> 21641cb0ef41Sopenharmony_ci</ul> 21651cb0ef41Sopenharmony_ci<p>The number of milliseconds of inactivity a server needs to wait for additional 21661cb0ef41Sopenharmony_ciincoming data, after it has finished writing the last response, before a socket 21671cb0ef41Sopenharmony_ciwill be destroyed. If the server receives new data before the keep-alive 21681cb0ef41Sopenharmony_citimeout has fired, it will reset the regular inactivity timeout, i.e., 21691cb0ef41Sopenharmony_ci<a href="#servertimeout"><code>server.timeout</code></a>.</p> 21701cb0ef41Sopenharmony_ci<p>A value of <code>0</code> will disable the keep-alive timeout behavior on incoming 21711cb0ef41Sopenharmony_ciconnections. 21721cb0ef41Sopenharmony_ciA value of <code>0</code> makes the http server behave similarly to Node.js versions prior 21731cb0ef41Sopenharmony_cito 8.0.0, which did not have a keep-alive timeout.</p> 21741cb0ef41Sopenharmony_ci<p>The socket timeout logic is set up on connection, so changing this value only 21751cb0ef41Sopenharmony_ciaffects new connections to the server, not any existing connections.</p> 21761cb0ef41Sopenharmony_ci</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> 21771cb0ef41Sopenharmony_ci<div class="api_metadata"> 21781cb0ef41Sopenharmony_ci<span>Added in: v0.1.17</span> 21791cb0ef41Sopenharmony_ci</div> 21801cb0ef41Sopenharmony_ci<ul> 21811cb0ef41Sopenharmony_ci<li>Extends: <a href="http.html#class-httpoutgoingmessage" class="type"><http.OutgoingMessage></a></li> 21821cb0ef41Sopenharmony_ci</ul> 21831cb0ef41Sopenharmony_ci<p>This object is created internally by an HTTP server, not by the user. It is 21841cb0ef41Sopenharmony_cipassed as the second parameter to the <a href="#event-request"><code>'request'</code></a> event.</p> 21851cb0ef41Sopenharmony_ci<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> 21861cb0ef41Sopenharmony_ci<div class="api_metadata"> 21871cb0ef41Sopenharmony_ci<span>Added in: v0.6.7</span> 21881cb0ef41Sopenharmony_ci</div> 21891cb0ef41Sopenharmony_ci<p>Indicates that the response is completed, or its underlying connection was 21901cb0ef41Sopenharmony_citerminated prematurely (before the response completion).</p> 21911cb0ef41Sopenharmony_ci<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> 21921cb0ef41Sopenharmony_ci<div class="api_metadata"> 21931cb0ef41Sopenharmony_ci<span>Added in: v0.3.6</span> 21941cb0ef41Sopenharmony_ci</div> 21951cb0ef41Sopenharmony_ci<p>Emitted when the response has been sent. More specifically, this event is 21961cb0ef41Sopenharmony_ciemitted when the last segment of the response headers and body have been 21971cb0ef41Sopenharmony_cihanded off to the operating system for transmission over the network. It 21981cb0ef41Sopenharmony_cidoes not imply that the client has received anything yet.</p> 21991cb0ef41Sopenharmony_ci<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> 22001cb0ef41Sopenharmony_ci<div class="api_metadata"> 22011cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 22021cb0ef41Sopenharmony_ci</div> 22031cb0ef41Sopenharmony_ci<ul> 22041cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 22051cb0ef41Sopenharmony_ci</ul> 22061cb0ef41Sopenharmony_ci<p>This method adds HTTP trailing headers (a header but at the end of the 22071cb0ef41Sopenharmony_cimessage) to the response.</p> 22081cb0ef41Sopenharmony_ci<p>Trailers will <strong>only</strong> be emitted if chunked encoding is used for the 22091cb0ef41Sopenharmony_ciresponse; if it is not (e.g. if the request was HTTP/1.0), they will 22101cb0ef41Sopenharmony_cibe silently discarded.</p> 22111cb0ef41Sopenharmony_ci<p>HTTP requires the <code>Trailer</code> header to be sent in order to 22121cb0ef41Sopenharmony_ciemit trailers, with a list of the header fields in its value. E.g.,</p> 22131cb0ef41Sopenharmony_ci<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>, 22141cb0ef41Sopenharmony_ci <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> }); 22151cb0ef41Sopenharmony_ciresponse.<span class="hljs-title function_">write</span>(fileData); 22161cb0ef41Sopenharmony_ciresponse.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-MD5'</span>: <span class="hljs-string">'7895bf4b8828b55ceaf47747b4bca667'</span> }); 22171cb0ef41Sopenharmony_ciresponse.<span class="hljs-title function_">end</span>();</code> <button class="copy-button">copy</button></pre> 22181cb0ef41Sopenharmony_ci<p>Attempting to set a header field name or value that contains invalid characters 22191cb0ef41Sopenharmony_ciwill result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p> 22201cb0ef41Sopenharmony_ci<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> 22211cb0ef41Sopenharmony_ci<div class="api_metadata"> 22221cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span><span>Deprecated since: v13.0.0</span> 22231cb0ef41Sopenharmony_ci</div> 22241cb0ef41Sopenharmony_ci<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> 22251cb0ef41Sopenharmony_ci<ul> 22261cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 22271cb0ef41Sopenharmony_ci</ul> 22281cb0ef41Sopenharmony_ci<p>See <a href="#responsesocket"><code>response.socket</code></a>.</p> 22291cb0ef41Sopenharmony_ci<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> 22301cb0ef41Sopenharmony_ci<div class="api_metadata"> 22311cb0ef41Sopenharmony_ci<span>Added in: v13.2.0, v12.16.0</span> 22321cb0ef41Sopenharmony_ci</div> 22331cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writablecork"><code>writable.cork()</code></a>.</p> 22341cb0ef41Sopenharmony_ci<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> 22351cb0ef41Sopenharmony_ci<div class="api_metadata"> 22361cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 22371cb0ef41Sopenharmony_ci<table> 22381cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 22391cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 22401cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 22411cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 22421cb0ef41Sopenharmony_ci<td><p>This method now returns a reference to <code>ServerResponse</code>.</p></td></tr> 22431cb0ef41Sopenharmony_ci<tr><td>v0.1.90</td> 22441cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.90</span></p></td></tr> 22451cb0ef41Sopenharmony_ci</tbody></table> 22461cb0ef41Sopenharmony_ci</details> 22471cb0ef41Sopenharmony_ci</div> 22481cb0ef41Sopenharmony_ci<ul> 22491cb0ef41Sopenharmony_ci<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> 22501cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 22511cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 22521cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 22531cb0ef41Sopenharmony_ci</ul> 22541cb0ef41Sopenharmony_ci<p>This method signals to the server that all of the response headers and body 22551cb0ef41Sopenharmony_cihave been sent; that server should consider this message complete. 22561cb0ef41Sopenharmony_ciThe method, <code>response.end()</code>, MUST be called on each response.</p> 22571cb0ef41Sopenharmony_ci<p>If <code>data</code> is specified, it is similar in effect to calling 22581cb0ef41Sopenharmony_ci<a href="#responsewritechunk-encoding-callback"><code>response.write(data, encoding)</code></a> followed by <code>response.end(callback)</code>.</p> 22591cb0ef41Sopenharmony_ci<p>If <code>callback</code> is specified, it will be called when the response stream 22601cb0ef41Sopenharmony_ciis finished.</p> 22611cb0ef41Sopenharmony_ci<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> 22621cb0ef41Sopenharmony_ci<div class="api_metadata"> 22631cb0ef41Sopenharmony_ci<span>Added in: v0.0.2</span><span>Deprecated since: v13.4.0, v12.16.0</span> 22641cb0ef41Sopenharmony_ci</div> 22651cb0ef41Sopenharmony_ci<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> 22661cb0ef41Sopenharmony_ci<ul> 22671cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 22681cb0ef41Sopenharmony_ci</ul> 22691cb0ef41Sopenharmony_ci<p>The <code>response.finished</code> property will be <code>true</code> if <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> 22701cb0ef41Sopenharmony_cihas been called.</p> 22711cb0ef41Sopenharmony_ci<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> 22721cb0ef41Sopenharmony_ci<div class="api_metadata"> 22731cb0ef41Sopenharmony_ci<span>Added in: v1.6.0</span> 22741cb0ef41Sopenharmony_ci</div> 22751cb0ef41Sopenharmony_ci<p>Flushes the response headers. See also: <a href="#requestflushheaders"><code>request.flushHeaders()</code></a>.</p> 22761cb0ef41Sopenharmony_ci<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> 22771cb0ef41Sopenharmony_ci<div class="api_metadata"> 22781cb0ef41Sopenharmony_ci<span>Added in: v0.4.0</span> 22791cb0ef41Sopenharmony_ci</div> 22801cb0ef41Sopenharmony_ci<ul> 22811cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 22821cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 22831cb0ef41Sopenharmony_ci</ul> 22841cb0ef41Sopenharmony_ci<p>Reads out a header that's already been queued but not sent to the client. 22851cb0ef41Sopenharmony_ciThe name is case-insensitive. The type of the return value depends 22861cb0ef41Sopenharmony_cion the arguments provided to <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>.</p> 22871cb0ef41Sopenharmony_ci<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>); 22881cb0ef41Sopenharmony_ciresponse.<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)); 22891cb0ef41Sopenharmony_ciresponse.<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>]); 22901cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contentType = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'content-type'</span>); 22911cb0ef41Sopenharmony_ci<span class="hljs-comment">// contentType is 'text/html'</span> 22921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contentLength = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Length'</span>); 22931cb0ef41Sopenharmony_ci<span class="hljs-comment">// contentLength is of type number</span> 22941cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> setCookie = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'set-cookie'</span>); 22951cb0ef41Sopenharmony_ci<span class="hljs-comment">// setCookie is of type string[]</span></code> <button class="copy-button">copy</button></pre> 22961cb0ef41Sopenharmony_ci<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> 22971cb0ef41Sopenharmony_ci<div class="api_metadata"> 22981cb0ef41Sopenharmony_ci<span>Added in: v7.7.0</span> 22991cb0ef41Sopenharmony_ci</div> 23001cb0ef41Sopenharmony_ci<ul> 23011cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 23021cb0ef41Sopenharmony_ci</ul> 23031cb0ef41Sopenharmony_ci<p>Returns an array containing the unique names of the current outgoing headers. 23041cb0ef41Sopenharmony_ciAll header names are lowercase.</p> 23051cb0ef41Sopenharmony_ci<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>); 23061cb0ef41Sopenharmony_ciresponse.<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>]); 23071cb0ef41Sopenharmony_ci 23081cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">getHeaderNames</span>(); 23091cb0ef41Sopenharmony_ci<span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code> <button class="copy-button">copy</button></pre> 23101cb0ef41Sopenharmony_ci<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> 23111cb0ef41Sopenharmony_ci<div class="api_metadata"> 23121cb0ef41Sopenharmony_ci<span>Added in: v7.7.0</span> 23131cb0ef41Sopenharmony_ci</div> 23141cb0ef41Sopenharmony_ci<ul> 23151cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 23161cb0ef41Sopenharmony_ci</ul> 23171cb0ef41Sopenharmony_ci<p>Returns a shallow copy of the current outgoing headers. Since a shallow copy 23181cb0ef41Sopenharmony_ciis used, array values may be mutated without additional calls to various 23191cb0ef41Sopenharmony_ciheader-related http module methods. The keys of the returned object are the 23201cb0ef41Sopenharmony_ciheader names and the values are the respective header values. All header names 23211cb0ef41Sopenharmony_ciare lowercase.</p> 23221cb0ef41Sopenharmony_ci<p>The object returned by the <code>response.getHeaders()</code> method <em>does not</em> 23231cb0ef41Sopenharmony_ciprototypically inherit from the JavaScript <code>Object</code>. This means that typical 23241cb0ef41Sopenharmony_ci<code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, and others 23251cb0ef41Sopenharmony_ciare not defined and <em>will not work</em>.</p> 23261cb0ef41Sopenharmony_ci<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>); 23271cb0ef41Sopenharmony_ciresponse.<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>]); 23281cb0ef41Sopenharmony_ci 23291cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHeaders</span>(); 23301cb0ef41Sopenharmony_ci<span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</span></code> <button class="copy-button">copy</button></pre> 23311cb0ef41Sopenharmony_ci<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> 23321cb0ef41Sopenharmony_ci<div class="api_metadata"> 23331cb0ef41Sopenharmony_ci<span>Added in: v7.7.0</span> 23341cb0ef41Sopenharmony_ci</div> 23351cb0ef41Sopenharmony_ci<ul> 23361cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 23371cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 23381cb0ef41Sopenharmony_ci</ul> 23391cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the 23401cb0ef41Sopenharmony_cioutgoing headers. The header name matching is case-insensitive.</p> 23411cb0ef41Sopenharmony_ci<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> 23421cb0ef41Sopenharmony_ci<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> 23431cb0ef41Sopenharmony_ci<div class="api_metadata"> 23441cb0ef41Sopenharmony_ci<span>Added in: v0.9.3</span> 23451cb0ef41Sopenharmony_ci</div> 23461cb0ef41Sopenharmony_ci<ul> 23471cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 23481cb0ef41Sopenharmony_ci</ul> 23491cb0ef41Sopenharmony_ci<p>Boolean (read-only). True if headers were sent, false otherwise.</p> 23501cb0ef41Sopenharmony_ci<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> 23511cb0ef41Sopenharmony_ci<div class="api_metadata"> 23521cb0ef41Sopenharmony_ci<span>Added in: v0.4.0</span> 23531cb0ef41Sopenharmony_ci</div> 23541cb0ef41Sopenharmony_ci<ul> 23551cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 23561cb0ef41Sopenharmony_ci</ul> 23571cb0ef41Sopenharmony_ci<p>Removes a header that's queued for implicit sending.</p> 23581cb0ef41Sopenharmony_ci<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> 23591cb0ef41Sopenharmony_ci<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> 23601cb0ef41Sopenharmony_ci<div class="api_metadata"> 23611cb0ef41Sopenharmony_ci<span>Added in: v15.7.0</span> 23621cb0ef41Sopenharmony_ci</div> 23631cb0ef41Sopenharmony_ci<ul> 23641cb0ef41Sopenharmony_ci<li><a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 23651cb0ef41Sopenharmony_ci</ul> 23661cb0ef41Sopenharmony_ci<p>A reference to the original HTTP <code>request</code> object.</p> 23671cb0ef41Sopenharmony_ci<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> 23681cb0ef41Sopenharmony_ci<div class="api_metadata"> 23691cb0ef41Sopenharmony_ci<span>Added in: v0.7.5</span> 23701cb0ef41Sopenharmony_ci</div> 23711cb0ef41Sopenharmony_ci<ul> 23721cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 23731cb0ef41Sopenharmony_ci</ul> 23741cb0ef41Sopenharmony_ci<p>When true, the Date header will be automatically generated and sent in 23751cb0ef41Sopenharmony_cithe response if it is not already present in the headers. Defaults to true.</p> 23761cb0ef41Sopenharmony_ci<p>This should only be disabled for testing; HTTP requires the Date header 23771cb0ef41Sopenharmony_ciin responses.</p> 23781cb0ef41Sopenharmony_ci<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> 23791cb0ef41Sopenharmony_ci<div class="api_metadata"> 23801cb0ef41Sopenharmony_ci<span>Added in: v0.4.0</span> 23811cb0ef41Sopenharmony_ci</div> 23821cb0ef41Sopenharmony_ci<ul> 23831cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 23841cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 23851cb0ef41Sopenharmony_ci<li>Returns: <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 23861cb0ef41Sopenharmony_ci</ul> 23871cb0ef41Sopenharmony_ci<p>Returns the response object.</p> 23881cb0ef41Sopenharmony_ci<p>Sets a single header value for implicit headers. If this header already exists 23891cb0ef41Sopenharmony_ciin the to-be-sent headers, its value will be replaced. Use an array of strings 23901cb0ef41Sopenharmony_cihere to send multiple headers with the same name. Non-string values will be 23911cb0ef41Sopenharmony_cistored without modification. Therefore, <a href="#responsegetheadername"><code>response.getHeader()</code></a> may return 23921cb0ef41Sopenharmony_cinon-string values. However, the non-string values will be converted to strings 23931cb0ef41Sopenharmony_cifor network transmission. The same response object is returned to the caller, 23941cb0ef41Sopenharmony_cito enable call chaining.</p> 23951cb0ef41Sopenharmony_ci<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> 23961cb0ef41Sopenharmony_ci<p>or</p> 23971cb0ef41Sopenharmony_ci<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> 23981cb0ef41Sopenharmony_ci<p>Attempting to set a header field name or value that contains invalid characters 23991cb0ef41Sopenharmony_ciwill result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p> 24001cb0ef41Sopenharmony_ci<p>When headers have been set with <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>, they will be merged 24011cb0ef41Sopenharmony_ciwith any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, with the headers passed 24021cb0ef41Sopenharmony_cito <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p> 24031cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span> 24041cb0ef41Sopenharmony_ci<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> { 24051cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>); 24061cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>); 24071cb0ef41Sopenharmony_ci 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> }); 24081cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 24091cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 24101cb0ef41Sopenharmony_ci<p>If <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> method is called and this method has not been 24111cb0ef41Sopenharmony_cicalled, it will directly write the supplied header values onto the network 24121cb0ef41Sopenharmony_cichannel without caching internally, and the <a href="#responsegetheadername"><code>response.getHeader()</code></a> on the 24131cb0ef41Sopenharmony_ciheader will not yield the expected result. If progressive population of headers 24141cb0ef41Sopenharmony_ciis desired with potential future retrieval and modification, use 24151cb0ef41Sopenharmony_ci<a href="#responsesetheadername-value"><code>response.setHeader()</code></a> instead of <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>.</p> 24161cb0ef41Sopenharmony_ci<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> 24171cb0ef41Sopenharmony_ci<div class="api_metadata"> 24181cb0ef41Sopenharmony_ci<span>Added in: v0.9.12</span> 24191cb0ef41Sopenharmony_ci</div> 24201cb0ef41Sopenharmony_ci<ul> 24211cb0ef41Sopenharmony_ci<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 24221cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 24231cb0ef41Sopenharmony_ci<li>Returns: <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 24241cb0ef41Sopenharmony_ci</ul> 24251cb0ef41Sopenharmony_ci<p>Sets the Socket's timeout value to <code>msecs</code>. If a callback is 24261cb0ef41Sopenharmony_ciprovided, then it is added as a listener on the <code>'timeout'</code> event on 24271cb0ef41Sopenharmony_cithe response object.</p> 24281cb0ef41Sopenharmony_ci<p>If no <code>'timeout'</code> listener is added to the request, the response, or 24291cb0ef41Sopenharmony_cithe server, then sockets are destroyed when they time out. If a handler is 24301cb0ef41Sopenharmony_ciassigned to the request, the response, or the server's <code>'timeout'</code> events, 24311cb0ef41Sopenharmony_citimed out sockets must be handled explicitly.</p> 24321cb0ef41Sopenharmony_ci<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> 24331cb0ef41Sopenharmony_ci<div class="api_metadata"> 24341cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 24351cb0ef41Sopenharmony_ci</div> 24361cb0ef41Sopenharmony_ci<ul> 24371cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 24381cb0ef41Sopenharmony_ci</ul> 24391cb0ef41Sopenharmony_ci<p>Reference to the underlying socket. Usually users will not want to access 24401cb0ef41Sopenharmony_cithis property. In particular, the socket will not emit <code>'readable'</code> events 24411cb0ef41Sopenharmony_cibecause of how the protocol parser attaches to the socket. After 24421cb0ef41Sopenharmony_ci<code>response.end()</code>, the property is nulled.</p> 24431cb0ef41Sopenharmony_ci 24441cb0ef41Sopenharmony_ci<pre class="with-48-chars"><input class="js-flavor-selector" 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>; 24451cb0ef41Sopenharmony_ci<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> { 24461cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ip = res.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>; 24471cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> port = res.<span class="hljs-property">socket</span>.<span class="hljs-property">remotePort</span>; 24481cb0ef41Sopenharmony_ci 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>); 24491cb0ef41Sopenharmony_ci}).<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>); 24501cb0ef41Sopenharmony_ci<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> { 24511cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ip = res.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>; 24521cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> port = res.<span class="hljs-property">socket</span>.<span class="hljs-property">remotePort</span>; 24531cb0ef41Sopenharmony_ci 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>); 24541cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><button class="copy-button">copy</button></pre> 24551cb0ef41Sopenharmony_ci<p>This property is guaranteed to be an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 24561cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specified a socket 24571cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 24581cb0ef41Sopenharmony_ci<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> 24591cb0ef41Sopenharmony_ci<div class="api_metadata"> 24601cb0ef41Sopenharmony_ci<span>Added in: v0.4.0</span> 24611cb0ef41Sopenharmony_ci</div> 24621cb0ef41Sopenharmony_ci<ul> 24631cb0ef41Sopenharmony_ci<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> 24641cb0ef41Sopenharmony_ci</ul> 24651cb0ef41Sopenharmony_ci<p>When using implicit headers (not calling <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> explicitly), 24661cb0ef41Sopenharmony_cithis property controls the status code that will be sent to the client when 24671cb0ef41Sopenharmony_cithe headers get flushed.</p> 24681cb0ef41Sopenharmony_ci<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> 24691cb0ef41Sopenharmony_ci<p>After response header was sent to the client, this property indicates the 24701cb0ef41Sopenharmony_cistatus code which was sent out.</p> 24711cb0ef41Sopenharmony_ci<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> 24721cb0ef41Sopenharmony_ci<div class="api_metadata"> 24731cb0ef41Sopenharmony_ci<span>Added in: v0.11.8</span> 24741cb0ef41Sopenharmony_ci</div> 24751cb0ef41Sopenharmony_ci<ul> 24761cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 24771cb0ef41Sopenharmony_ci</ul> 24781cb0ef41Sopenharmony_ci<p>When using implicit headers (not calling <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> explicitly), 24791cb0ef41Sopenharmony_cithis property controls the status message that will be sent to the client when 24801cb0ef41Sopenharmony_cithe headers get flushed. If this is left as <code>undefined</code> then the standard 24811cb0ef41Sopenharmony_cimessage for the status code will be used.</p> 24821cb0ef41Sopenharmony_ci<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> 24831cb0ef41Sopenharmony_ci<p>After response header was sent to the client, this property indicates the 24841cb0ef41Sopenharmony_cistatus message which was sent out.</p> 24851cb0ef41Sopenharmony_ci<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> 24861cb0ef41Sopenharmony_ci<div class="api_metadata"> 24871cb0ef41Sopenharmony_ci<span>Added in: v18.10.0, v16.18.0</span> 24881cb0ef41Sopenharmony_ci</div> 24891cb0ef41Sopenharmony_ci<ul> 24901cb0ef41Sopenharmony_ci<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> 24911cb0ef41Sopenharmony_ci</ul> 24921cb0ef41Sopenharmony_ci<p>If set to <code>true</code>, Node.js will check whether the <code>Content-Length</code> 24931cb0ef41Sopenharmony_ciheader value and the size of the body, in bytes, are equal. 24941cb0ef41Sopenharmony_ciMismatching the <code>Content-Length</code> header value will result 24951cb0ef41Sopenharmony_ciin 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> 24961cb0ef41Sopenharmony_ci<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> 24971cb0ef41Sopenharmony_ci<div class="api_metadata"> 24981cb0ef41Sopenharmony_ci<span>Added in: v13.2.0, v12.16.0</span> 24991cb0ef41Sopenharmony_ci</div> 25001cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writableuncork"><code>writable.uncork()</code></a>.</p> 25011cb0ef41Sopenharmony_ci<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> 25021cb0ef41Sopenharmony_ci<div class="api_metadata"> 25031cb0ef41Sopenharmony_ci<span>Added in: v12.9.0</span> 25041cb0ef41Sopenharmony_ci</div> 25051cb0ef41Sopenharmony_ci<ul> 25061cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 25071cb0ef41Sopenharmony_ci</ul> 25081cb0ef41Sopenharmony_ci<p>Is <code>true</code> after <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> has been called. This property 25091cb0ef41Sopenharmony_cidoes not indicate whether the data has been flushed, for this use 25101cb0ef41Sopenharmony_ci<a href="#responsewritablefinished"><code>response.writableFinished</code></a> instead.</p> 25111cb0ef41Sopenharmony_ci<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> 25121cb0ef41Sopenharmony_ci<div class="api_metadata"> 25131cb0ef41Sopenharmony_ci<span>Added in: v12.7.0</span> 25141cb0ef41Sopenharmony_ci</div> 25151cb0ef41Sopenharmony_ci<ul> 25161cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 25171cb0ef41Sopenharmony_ci</ul> 25181cb0ef41Sopenharmony_ci<p>Is <code>true</code> if all data has been flushed to the underlying system, immediately 25191cb0ef41Sopenharmony_cibefore the <a href="#event-finish"><code>'finish'</code></a> event is emitted.</p> 25201cb0ef41Sopenharmony_ci<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> 25211cb0ef41Sopenharmony_ci<div class="api_metadata"> 25221cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 25231cb0ef41Sopenharmony_ci<table> 25241cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 25251cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 25261cb0ef41Sopenharmony_ci<td><p>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 25271cb0ef41Sopenharmony_ci<tr><td>v0.1.29</td> 25281cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.29</span></p></td></tr> 25291cb0ef41Sopenharmony_ci</tbody></table> 25301cb0ef41Sopenharmony_ci</details> 25311cb0ef41Sopenharmony_ci</div> 25321cb0ef41Sopenharmony_ci<ul> 25331cb0ef41Sopenharmony_ci<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> 25341cb0ef41Sopenharmony_ci<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> 25351cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 25361cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 25371cb0ef41Sopenharmony_ci</ul> 25381cb0ef41Sopenharmony_ci<p>If this method is called and <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> has not been called, 25391cb0ef41Sopenharmony_ciit will switch to implicit header mode and flush the implicit headers.</p> 25401cb0ef41Sopenharmony_ci<p>This sends a chunk of the response body. This method may 25411cb0ef41Sopenharmony_cibe called multiple times to provide successive parts of the body.</p> 25421cb0ef41Sopenharmony_ci<p>Writing to the body is not allowed when the request method or response status 25431cb0ef41Sopenharmony_cido not support content. If an attempt is made to write to the body for a 25441cb0ef41Sopenharmony_ciHEAD request or as part of a <code>204</code> or <code>304</code>response, a synchronous <code>Error</code> 25451cb0ef41Sopenharmony_ciwith the code <code>ERR_HTTP_BODY_NOT_ALLOWED</code> is thrown.</p> 25461cb0ef41Sopenharmony_ci<p><code>chunk</code> can be a string or a buffer. If <code>chunk</code> is a string, 25471cb0ef41Sopenharmony_cithe second parameter specifies how to encode it into a byte stream. 25481cb0ef41Sopenharmony_ci<code>callback</code> will be called when this chunk of data is flushed.</p> 25491cb0ef41Sopenharmony_ci<p>This is the raw HTTP body and has nothing to do with higher-level multi-part 25501cb0ef41Sopenharmony_cibody encodings that may be used.</p> 25511cb0ef41Sopenharmony_ci<p>The first time <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> is called, it will send the buffered 25521cb0ef41Sopenharmony_ciheader information and the first chunk of the body to the client. The second 25531cb0ef41Sopenharmony_citime <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> is called, Node.js assumes data will be streamed, 25541cb0ef41Sopenharmony_ciand sends the new data separately. That is, the response is buffered up to the 25551cb0ef41Sopenharmony_cifirst chunk of the body.</p> 25561cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel 25571cb0ef41Sopenharmony_cibuffer. Returns <code>false</code> if all or part of the data was queued in user memory. 25581cb0ef41Sopenharmony_ci<code>'drain'</code> will be emitted when the buffer is free again.</p> 25591cb0ef41Sopenharmony_ci<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> 25601cb0ef41Sopenharmony_ci<div class="api_metadata"> 25611cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 25621cb0ef41Sopenharmony_ci</div> 25631cb0ef41Sopenharmony_ci<p>Sends an HTTP/1.1 100 Continue message to the client, indicating that 25641cb0ef41Sopenharmony_cithe request body should be sent. See the <a href="#event-checkcontinue"><code>'checkContinue'</code></a> event on 25651cb0ef41Sopenharmony_ci<code>Server</code>.</p> 25661cb0ef41Sopenharmony_ci<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> 25671cb0ef41Sopenharmony_ci<div class="api_metadata"> 25681cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 25691cb0ef41Sopenharmony_ci<table> 25701cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 25711cb0ef41Sopenharmony_ci<tr><td>v18.11.0</td> 25721cb0ef41Sopenharmony_ci<td><p>Allow passing hints as an object.</p></td></tr> 25731cb0ef41Sopenharmony_ci<tr><td>v18.11.0</td> 25741cb0ef41Sopenharmony_ci<td><p><span>Added in: v18.11.0</span></p></td></tr> 25751cb0ef41Sopenharmony_ci</tbody></table> 25761cb0ef41Sopenharmony_ci</details> 25771cb0ef41Sopenharmony_ci</div> 25781cb0ef41Sopenharmony_ci<ul> 25791cb0ef41Sopenharmony_ci<li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 25801cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 25811cb0ef41Sopenharmony_ci</ul> 25821cb0ef41Sopenharmony_ci<p>Sends an HTTP/1.1 103 Early Hints message to the client with a Link header, 25831cb0ef41Sopenharmony_ciindicating that the user agent can preload/preconnect the linked resources. 25841cb0ef41Sopenharmony_ciThe <code>hints</code> is an object containing the values of headers to be sent with 25851cb0ef41Sopenharmony_ciearly hints message. The optional <code>callback</code> argument will be called when 25861cb0ef41Sopenharmony_cithe response message has been written.</p> 25871cb0ef41Sopenharmony_ci<p><strong>Example</strong></p> 25881cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> earlyHintsLink = <span class="hljs-string">'</styles.css>; rel=preload; as=style'</span>; 25891cb0ef41Sopenharmony_ciresponse.<span class="hljs-title function_">writeEarlyHints</span>({ 25901cb0ef41Sopenharmony_ci <span class="hljs-string">'link'</span>: earlyHintsLink, 25911cb0ef41Sopenharmony_ci}); 25921cb0ef41Sopenharmony_ci 25931cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> earlyHintsLinks = [ 25941cb0ef41Sopenharmony_ci <span class="hljs-string">'</styles.css>; rel=preload; as=style'</span>, 25951cb0ef41Sopenharmony_ci <span class="hljs-string">'</scripts.js>; rel=preload; as=script'</span>, 25961cb0ef41Sopenharmony_ci]; 25971cb0ef41Sopenharmony_ciresponse.<span class="hljs-title function_">writeEarlyHints</span>({ 25981cb0ef41Sopenharmony_ci <span class="hljs-string">'link'</span>: earlyHintsLinks, 25991cb0ef41Sopenharmony_ci <span class="hljs-string">'x-trace-id'</span>: <span class="hljs-string">'id for diagnostics'</span>, 26001cb0ef41Sopenharmony_ci}); 26011cb0ef41Sopenharmony_ci 26021cb0ef41Sopenharmony_ci<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>); 26031cb0ef41Sopenharmony_ciresponse.<span class="hljs-title function_">writeEarlyHints</span>({ 26041cb0ef41Sopenharmony_ci <span class="hljs-string">'link'</span>: earlyHintsLinks, 26051cb0ef41Sopenharmony_ci}, earlyHintsCallback);</code> <button class="copy-button">copy</button></pre> 26061cb0ef41Sopenharmony_ci<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> 26071cb0ef41Sopenharmony_ci<div class="api_metadata"> 26081cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 26091cb0ef41Sopenharmony_ci<table> 26101cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 26111cb0ef41Sopenharmony_ci<tr><td>v14.14.0</td> 26121cb0ef41Sopenharmony_ci<td><p>Allow passing headers as an array.</p></td></tr> 26131cb0ef41Sopenharmony_ci<tr><td>v11.10.0, v10.17.0</td> 26141cb0ef41Sopenharmony_ci<td><p>Return <code>this</code> from <code>writeHead()</code> to allow chaining with <code>end()</code>.</p></td></tr> 26151cb0ef41Sopenharmony_ci<tr><td>v5.11.0, v4.4.5</td> 26161cb0ef41Sopenharmony_ci<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> 26171cb0ef41Sopenharmony_ci<tr><td>v0.1.30</td> 26181cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.30</span></p></td></tr> 26191cb0ef41Sopenharmony_ci</tbody></table> 26201cb0ef41Sopenharmony_ci</details> 26211cb0ef41Sopenharmony_ci</div> 26221cb0ef41Sopenharmony_ci<ul> 26231cb0ef41Sopenharmony_ci<li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 26241cb0ef41Sopenharmony_ci<li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 26251cb0ef41Sopenharmony_ci<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> 26261cb0ef41Sopenharmony_ci<li>Returns: <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 26271cb0ef41Sopenharmony_ci</ul> 26281cb0ef41Sopenharmony_ci<p>Sends a response header to the request. The status code is a 3-digit HTTP 26291cb0ef41Sopenharmony_cistatus code, like <code>404</code>. The last argument, <code>headers</code>, are the response headers. 26301cb0ef41Sopenharmony_ciOptionally one can give a human-readable <code>statusMessage</code> as the second 26311cb0ef41Sopenharmony_ciargument.</p> 26321cb0ef41Sopenharmony_ci<p><code>headers</code> may be an <code>Array</code> where the keys and values are in the same list. 26331cb0ef41Sopenharmony_ciIt is <em>not</em> a list of tuples. So, the even-numbered offsets are key values, 26341cb0ef41Sopenharmony_ciand the odd-numbered offsets are the associated values. The array is in the same 26351cb0ef41Sopenharmony_ciformat as <code>request.rawHeaders</code>.</p> 26361cb0ef41Sopenharmony_ci<p>Returns a reference to the <code>ServerResponse</code>, so that calls can be chained.</p> 26371cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-string">'hello world'</span>; 26381cb0ef41Sopenharmony_ciresponse 26391cb0ef41Sopenharmony_ci .<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { 26401cb0ef41Sopenharmony_ci <span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(body), 26411cb0ef41Sopenharmony_ci <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span>, 26421cb0ef41Sopenharmony_ci }) 26431cb0ef41Sopenharmony_ci .<span class="hljs-title function_">end</span>(body);</code> <button class="copy-button">copy</button></pre> 26441cb0ef41Sopenharmony_ci<p>This method must only be called once on a message and it must 26451cb0ef41Sopenharmony_cibe called before <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> is called.</p> 26461cb0ef41Sopenharmony_ci<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 26471cb0ef41Sopenharmony_cithis, the implicit/mutable headers will be calculated and call this function.</p> 26481cb0ef41Sopenharmony_ci<p>When headers have been set with <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>, they will be merged 26491cb0ef41Sopenharmony_ciwith any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, with the headers passed 26501cb0ef41Sopenharmony_cito <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p> 26511cb0ef41Sopenharmony_ci<p>If this method is called and <a href="#responsesetheadername-value"><code>response.setHeader()</code></a> has not been called, 26521cb0ef41Sopenharmony_ciit will directly write the supplied header values onto the network channel 26531cb0ef41Sopenharmony_ciwithout caching internally, and the <a href="#responsegetheadername"><code>response.getHeader()</code></a> on the header 26541cb0ef41Sopenharmony_ciwill not yield the expected result. If progressive population of headers is 26551cb0ef41Sopenharmony_cidesired with potential future retrieval and modification, use 26561cb0ef41Sopenharmony_ci<a href="#responsesetheadername-value"><code>response.setHeader()</code></a> instead.</p> 26571cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span> 26581cb0ef41Sopenharmony_ci<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> { 26591cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>); 26601cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>); 26611cb0ef41Sopenharmony_ci 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> }); 26621cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 26631cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 26641cb0ef41Sopenharmony_ci<p><code>Content-Length</code> is read in bytes, not characters. Use 26651cb0ef41Sopenharmony_ci<a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength()</code></a> to determine the length of the body in bytes. Node.js 26661cb0ef41Sopenharmony_ciwill check whether <code>Content-Length</code> and the length of the body which has 26671cb0ef41Sopenharmony_cibeen transmitted are equal or not.</p> 26681cb0ef41Sopenharmony_ci<p>Attempting to set a header field name or value that contains invalid characters 26691cb0ef41Sopenharmony_ciwill result in a [<code>Error</code>][] being thrown.</p> 26701cb0ef41Sopenharmony_ci<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> 26711cb0ef41Sopenharmony_ci<div class="api_metadata"> 26721cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span> 26731cb0ef41Sopenharmony_ci</div> 26741cb0ef41Sopenharmony_ci<p>Sends a HTTP/1.1 102 Processing message to the client, indicating that 26751cb0ef41Sopenharmony_cithe request body should be sent.</p> 26761cb0ef41Sopenharmony_ci</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> 26771cb0ef41Sopenharmony_ci<div class="api_metadata"> 26781cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 26791cb0ef41Sopenharmony_ci<table> 26801cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 26811cb0ef41Sopenharmony_ci<tr><td>v15.5.0</td> 26821cb0ef41Sopenharmony_ci<td><p>The <code>destroyed</code> value returns <code>true</code> after the incoming data is consumed.</p></td></tr> 26831cb0ef41Sopenharmony_ci<tr><td>v13.1.0, v12.16.0</td> 26841cb0ef41Sopenharmony_ci<td><p>The <code>readableHighWaterMark</code> value mirrors that of the socket.</p></td></tr> 26851cb0ef41Sopenharmony_ci<tr><td>v0.1.17</td> 26861cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.17</span></p></td></tr> 26871cb0ef41Sopenharmony_ci</tbody></table> 26881cb0ef41Sopenharmony_ci</details> 26891cb0ef41Sopenharmony_ci</div> 26901cb0ef41Sopenharmony_ci<ul> 26911cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li> 26921cb0ef41Sopenharmony_ci</ul> 26931cb0ef41Sopenharmony_ci<p>An <code>IncomingMessage</code> object is created by <a href="#class-httpserver"><code>http.Server</code></a> or 26941cb0ef41Sopenharmony_ci<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> 26951cb0ef41Sopenharmony_ciand <a href="#event-response"><code>'response'</code></a> event respectively. It may be used to access response 26961cb0ef41Sopenharmony_cistatus, headers, and data.</p> 26971cb0ef41Sopenharmony_ci<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 26981cb0ef41Sopenharmony_ci<code>IncomingMessage</code> itself extends <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> and is created separately to 26991cb0ef41Sopenharmony_ciparse and emit the incoming HTTP headers and payload, as the underlying socket 27001cb0ef41Sopenharmony_cimay be reused multiple times in case of keep-alive.</p> 27011cb0ef41Sopenharmony_ci<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> 27021cb0ef41Sopenharmony_ci<div class="api_metadata"> 27031cb0ef41Sopenharmony_ci<span>Added in: v0.3.8</span><span>Deprecated since: v17.0.0, v16.12.0</span> 27041cb0ef41Sopenharmony_ci</div> 27051cb0ef41Sopenharmony_ci<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> 27061cb0ef41Sopenharmony_ci<p>Emitted when the request has been aborted.</p> 27071cb0ef41Sopenharmony_ci<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> 27081cb0ef41Sopenharmony_ci<div class="api_metadata"> 27091cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 27101cb0ef41Sopenharmony_ci<table> 27111cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 27121cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td> 27131cb0ef41Sopenharmony_ci<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> 27141cb0ef41Sopenharmony_ci<tr><td>v0.4.2</td> 27151cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.4.2</span></p></td></tr> 27161cb0ef41Sopenharmony_ci</tbody></table> 27171cb0ef41Sopenharmony_ci</details> 27181cb0ef41Sopenharmony_ci</div> 27191cb0ef41Sopenharmony_ci<p>Emitted when the request has been completed.</p> 27201cb0ef41Sopenharmony_ci<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> 27211cb0ef41Sopenharmony_ci<div class="api_metadata"> 27221cb0ef41Sopenharmony_ci<span>Added in: v10.1.0</span><span>Deprecated since: v17.0.0, v16.12.0</span> 27231cb0ef41Sopenharmony_ci</div> 27241cb0ef41Sopenharmony_ci<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> 27251cb0ef41Sopenharmony_ci<ul> 27261cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 27271cb0ef41Sopenharmony_ci</ul> 27281cb0ef41Sopenharmony_ci<p>The <code>message.aborted</code> property will be <code>true</code> if the request has 27291cb0ef41Sopenharmony_cibeen aborted.</p> 27301cb0ef41Sopenharmony_ci<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> 27311cb0ef41Sopenharmony_ci<div class="api_metadata"> 27321cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 27331cb0ef41Sopenharmony_ci</div> 27341cb0ef41Sopenharmony_ci<ul> 27351cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 27361cb0ef41Sopenharmony_ci</ul> 27371cb0ef41Sopenharmony_ci<p>The <code>message.complete</code> property will be <code>true</code> if a complete HTTP message has 27381cb0ef41Sopenharmony_cibeen received and successfully parsed.</p> 27391cb0ef41Sopenharmony_ci<p>This property is particularly useful as a means of determining if a client or 27401cb0ef41Sopenharmony_ciserver fully transmitted a message before a connection was terminated:</p> 27411cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>({ 27421cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 27431cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>, 27441cb0ef41Sopenharmony_ci <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>, 27451cb0ef41Sopenharmony_ci}, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 27461cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">resume</span>(); 27471cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 27481cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (!res.<span class="hljs-property">complete</span>) 27491cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>( 27501cb0ef41Sopenharmony_ci <span class="hljs-string">'The connection was terminated while the message was still being sent'</span>); 27511cb0ef41Sopenharmony_ci }); 27521cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 27531cb0ef41Sopenharmony_ci<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> 27541cb0ef41Sopenharmony_ci<div class="api_metadata"> 27551cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span><span>Deprecated since: v16.0.0</span> 27561cb0ef41Sopenharmony_ci</div> 27571cb0ef41Sopenharmony_ci<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> 27581cb0ef41Sopenharmony_ci<p>Alias for <a href="#messagesocket"><code>message.socket</code></a>.</p> 27591cb0ef41Sopenharmony_ci<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> 27601cb0ef41Sopenharmony_ci<div class="api_metadata"> 27611cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 27621cb0ef41Sopenharmony_ci<table> 27631cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 27641cb0ef41Sopenharmony_ci<tr><td>v14.5.0, v12.19.0</td> 27651cb0ef41Sopenharmony_ci<td><p>The function returns <code>this</code> for consistency with other Readable streams.</p></td></tr> 27661cb0ef41Sopenharmony_ci<tr><td>v0.3.0</td> 27671cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.0</span></p></td></tr> 27681cb0ef41Sopenharmony_ci</tbody></table> 27691cb0ef41Sopenharmony_ci</details> 27701cb0ef41Sopenharmony_ci</div> 27711cb0ef41Sopenharmony_ci<ul> 27721cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 27731cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 27741cb0ef41Sopenharmony_ci</ul> 27751cb0ef41Sopenharmony_ci<p>Calls <code>destroy()</code> on the socket that received the <code>IncomingMessage</code>. If <code>error</code> 27761cb0ef41Sopenharmony_ciis provided, an <code>'error'</code> event is emitted on the socket and <code>error</code> is passed 27771cb0ef41Sopenharmony_cias an argument to any listeners on the event.</p> 27781cb0ef41Sopenharmony_ci<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> 27791cb0ef41Sopenharmony_ci<div class="api_metadata"> 27801cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 27811cb0ef41Sopenharmony_ci<table> 27821cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 27831cb0ef41Sopenharmony_ci<tr><td>v18.14.0</td> 27841cb0ef41Sopenharmony_ci<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> 27851cb0ef41Sopenharmony_ci<tr><td>v15.1.0</td> 27861cb0ef41Sopenharmony_ci<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> 27871cb0ef41Sopenharmony_ci<tr><td>v0.1.5</td> 27881cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.5</span></p></td></tr> 27891cb0ef41Sopenharmony_ci</tbody></table> 27901cb0ef41Sopenharmony_ci</details> 27911cb0ef41Sopenharmony_ci</div> 27921cb0ef41Sopenharmony_ci<ul> 27931cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 27941cb0ef41Sopenharmony_ci</ul> 27951cb0ef41Sopenharmony_ci<p>The request/response headers object.</p> 27961cb0ef41Sopenharmony_ci<p>Key-value pairs of header names and values. Header names are lower-cased.</p> 27971cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span> 27981cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 27991cb0ef41Sopenharmony_ci<span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span> 28001cb0ef41Sopenharmony_ci<span class="hljs-comment">// host: '127.0.0.1:8000',</span> 28011cb0ef41Sopenharmony_ci<span class="hljs-comment">// accept: '*/*' }</span> 28021cb0ef41Sopenharmony_ci<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> 28031cb0ef41Sopenharmony_ci<p>Duplicates in raw headers are handled in the following ways, depending on the 28041cb0ef41Sopenharmony_ciheader name:</p> 28051cb0ef41Sopenharmony_ci<ul> 28061cb0ef41Sopenharmony_ci<li>Duplicates of <code>age</code>, <code>authorization</code>, <code>content-length</code>, <code>content-type</code>, 28071cb0ef41Sopenharmony_ci<code>etag</code>, <code>expires</code>, <code>from</code>, <code>host</code>, <code>if-modified-since</code>, <code>if-unmodified-since</code>, 28081cb0ef41Sopenharmony_ci<code>last-modified</code>, <code>location</code>, <code>max-forwards</code>, <code>proxy-authorization</code>, <code>referer</code>, 28091cb0ef41Sopenharmony_ci<code>retry-after</code>, <code>server</code>, or <code>user-agent</code> are discarded. 28101cb0ef41Sopenharmony_ciTo allow duplicate values of the headers listed above to be joined, 28111cb0ef41Sopenharmony_ciuse the option <code>joinDuplicateHeaders</code> in <a href="#httprequestoptions-callback"><code>http.request()</code></a> 28121cb0ef41Sopenharmony_ciand <a href="#httpcreateserveroptions-requestlistener"><code>http.createServer()</code></a>. See RFC 9110 Section 5.3 for more 28131cb0ef41Sopenharmony_ciinformation.</li> 28141cb0ef41Sopenharmony_ci<li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li> 28151cb0ef41Sopenharmony_ci<li>For duplicate <code>cookie</code> headers, the values are joined together with <code>; </code>.</li> 28161cb0ef41Sopenharmony_ci<li>For all other headers, the values are joined together with <code>, </code>.</li> 28171cb0ef41Sopenharmony_ci</ul> 28181cb0ef41Sopenharmony_ci<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> 28191cb0ef41Sopenharmony_ci<div class="api_metadata"> 28201cb0ef41Sopenharmony_ci<span>Added in: v18.3.0</span> 28211cb0ef41Sopenharmony_ci</div> 28221cb0ef41Sopenharmony_ci<ul> 28231cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 28241cb0ef41Sopenharmony_ci</ul> 28251cb0ef41Sopenharmony_ci<p>Similar to <a href="#messageheaders"><code>message.headers</code></a>, but there is no join logic and the values are 28261cb0ef41Sopenharmony_cialways arrays of strings, even for headers received just once.</p> 28271cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span> 28281cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 28291cb0ef41Sopenharmony_ci<span class="hljs-comment">// { 'user-agent': ['curl/7.22.0'],</span> 28301cb0ef41Sopenharmony_ci<span class="hljs-comment">// host: ['127.0.0.1:8000'],</span> 28311cb0ef41Sopenharmony_ci<span class="hljs-comment">// accept: ['*/*'] }</span> 28321cb0ef41Sopenharmony_ci<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> 28331cb0ef41Sopenharmony_ci<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> 28341cb0ef41Sopenharmony_ci<div class="api_metadata"> 28351cb0ef41Sopenharmony_ci<span>Added in: v0.1.1</span> 28361cb0ef41Sopenharmony_ci</div> 28371cb0ef41Sopenharmony_ci<ul> 28381cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 28391cb0ef41Sopenharmony_ci</ul> 28401cb0ef41Sopenharmony_ci<p>In case of server request, the HTTP version sent by the client. In the case of 28411cb0ef41Sopenharmony_ciclient response, the HTTP version of the connected-to server. 28421cb0ef41Sopenharmony_ciProbably either <code>'1.1'</code> or <code>'1.0'</code>.</p> 28431cb0ef41Sopenharmony_ci<p>Also <code>message.httpVersionMajor</code> is the first integer and 28441cb0ef41Sopenharmony_ci<code>message.httpVersionMinor</code> is the second.</p> 28451cb0ef41Sopenharmony_ci<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> 28461cb0ef41Sopenharmony_ci<div class="api_metadata"> 28471cb0ef41Sopenharmony_ci<span>Added in: v0.1.1</span> 28481cb0ef41Sopenharmony_ci</div> 28491cb0ef41Sopenharmony_ci<ul> 28501cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 28511cb0ef41Sopenharmony_ci</ul> 28521cb0ef41Sopenharmony_ci<p><strong>Only valid for request obtained from <a href="#class-httpserver"><code>http.Server</code></a>.</strong></p> 28531cb0ef41Sopenharmony_ci<p>The request method as a string. Read only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.</p> 28541cb0ef41Sopenharmony_ci<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> 28551cb0ef41Sopenharmony_ci<div class="api_metadata"> 28561cb0ef41Sopenharmony_ci<span>Added in: v0.11.6</span> 28571cb0ef41Sopenharmony_ci</div> 28581cb0ef41Sopenharmony_ci<ul> 28591cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 28601cb0ef41Sopenharmony_ci</ul> 28611cb0ef41Sopenharmony_ci<p>The raw request/response headers list exactly as they were received.</p> 28621cb0ef41Sopenharmony_ci<p>The keys and values are in the same list. It is <em>not</em> a 28631cb0ef41Sopenharmony_cilist of tuples. So, the even-numbered offsets are key values, and the 28641cb0ef41Sopenharmony_ciodd-numbered offsets are the associated values.</p> 28651cb0ef41Sopenharmony_ci<p>Header names are not lowercased, and duplicates are not merged.</p> 28661cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span> 28671cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span> 28681cb0ef41Sopenharmony_ci<span class="hljs-comment">// [ 'user-agent',</span> 28691cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'this is invalid because there can be only one',</span> 28701cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'User-Agent',</span> 28711cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'curl/7.22.0',</span> 28721cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'Host',</span> 28731cb0ef41Sopenharmony_ci<span class="hljs-comment">// '127.0.0.1:8000',</span> 28741cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'ACCEPT',</span> 28751cb0ef41Sopenharmony_ci<span class="hljs-comment">// '*/*' ]</span> 28761cb0ef41Sopenharmony_ci<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> 28771cb0ef41Sopenharmony_ci<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> 28781cb0ef41Sopenharmony_ci<div class="api_metadata"> 28791cb0ef41Sopenharmony_ci<span>Added in: v0.11.6</span> 28801cb0ef41Sopenharmony_ci</div> 28811cb0ef41Sopenharmony_ci<ul> 28821cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 28831cb0ef41Sopenharmony_ci</ul> 28841cb0ef41Sopenharmony_ci<p>The raw request/response trailer keys and values exactly as they were 28851cb0ef41Sopenharmony_cireceived. Only populated at the <code>'end'</code> event.</p> 28861cb0ef41Sopenharmony_ci<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> 28871cb0ef41Sopenharmony_ci<div class="api_metadata"> 28881cb0ef41Sopenharmony_ci<span>Added in: v0.5.9</span> 28891cb0ef41Sopenharmony_ci</div> 28901cb0ef41Sopenharmony_ci<ul> 28911cb0ef41Sopenharmony_ci<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 28921cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 28931cb0ef41Sopenharmony_ci<li>Returns: <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 28941cb0ef41Sopenharmony_ci</ul> 28951cb0ef41Sopenharmony_ci<p>Calls <code>message.socket.setTimeout(msecs, callback)</code>.</p> 28961cb0ef41Sopenharmony_ci<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> 28971cb0ef41Sopenharmony_ci<div class="api_metadata"> 28981cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 28991cb0ef41Sopenharmony_ci</div> 29001cb0ef41Sopenharmony_ci<ul> 29011cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 29021cb0ef41Sopenharmony_ci</ul> 29031cb0ef41Sopenharmony_ci<p>The <a href="net.html#class-netsocket"><code>net.Socket</code></a> object associated with the connection.</p> 29041cb0ef41Sopenharmony_ci<p>With HTTPS support, use <a href="tls.html#tlssocketgetpeercertificatedetailed"><code>request.socket.getPeerCertificate()</code></a> to obtain the 29051cb0ef41Sopenharmony_ciclient's authentication details.</p> 29061cb0ef41Sopenharmony_ci<p>This property is guaranteed to be an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 29071cb0ef41Sopenharmony_cia subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specified a socket 29081cb0ef41Sopenharmony_citype other than <a href="net.html#class-netsocket" class="type"><net.Socket></a> or internally nulled.</p> 29091cb0ef41Sopenharmony_ci<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> 29101cb0ef41Sopenharmony_ci<div class="api_metadata"> 29111cb0ef41Sopenharmony_ci<span>Added in: v0.1.1</span> 29121cb0ef41Sopenharmony_ci</div> 29131cb0ef41Sopenharmony_ci<ul> 29141cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 29151cb0ef41Sopenharmony_ci</ul> 29161cb0ef41Sopenharmony_ci<p><strong>Only valid for response obtained from <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a>.</strong></p> 29171cb0ef41Sopenharmony_ci<p>The 3-digit HTTP response status code. E.G. <code>404</code>.</p> 29181cb0ef41Sopenharmony_ci<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> 29191cb0ef41Sopenharmony_ci<div class="api_metadata"> 29201cb0ef41Sopenharmony_ci<span>Added in: v0.11.10</span> 29211cb0ef41Sopenharmony_ci</div> 29221cb0ef41Sopenharmony_ci<ul> 29231cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 29241cb0ef41Sopenharmony_ci</ul> 29251cb0ef41Sopenharmony_ci<p><strong>Only valid for response obtained from <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a>.</strong></p> 29261cb0ef41Sopenharmony_ci<p>The HTTP response status message (reason phrase). E.G. <code>OK</code> or <code>Internal Server Error</code>.</p> 29271cb0ef41Sopenharmony_ci<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> 29281cb0ef41Sopenharmony_ci<div class="api_metadata"> 29291cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 29301cb0ef41Sopenharmony_ci</div> 29311cb0ef41Sopenharmony_ci<ul> 29321cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 29331cb0ef41Sopenharmony_ci</ul> 29341cb0ef41Sopenharmony_ci<p>The request/response trailers object. Only populated at the <code>'end'</code> event.</p> 29351cb0ef41Sopenharmony_ci<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> 29361cb0ef41Sopenharmony_ci<div class="api_metadata"> 29371cb0ef41Sopenharmony_ci<span>Added in: v18.3.0</span> 29381cb0ef41Sopenharmony_ci</div> 29391cb0ef41Sopenharmony_ci<ul> 29401cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 29411cb0ef41Sopenharmony_ci</ul> 29421cb0ef41Sopenharmony_ci<p>Similar to <a href="#messagetrailers"><code>message.trailers</code></a>, but there is no join logic and the values are 29431cb0ef41Sopenharmony_cialways arrays of strings, even for headers received just once. 29441cb0ef41Sopenharmony_ciOnly populated at the <code>'end'</code> event.</p> 29451cb0ef41Sopenharmony_ci<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> 29461cb0ef41Sopenharmony_ci<div class="api_metadata"> 29471cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 29481cb0ef41Sopenharmony_ci</div> 29491cb0ef41Sopenharmony_ci<ul> 29501cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 29511cb0ef41Sopenharmony_ci</ul> 29521cb0ef41Sopenharmony_ci<p><strong>Only valid for request obtained from <a href="#class-httpserver"><code>http.Server</code></a>.</strong></p> 29531cb0ef41Sopenharmony_ci<p>Request URL string. This contains only the URL that is present in the actual 29541cb0ef41Sopenharmony_ciHTTP request. Take the following request:</p> 29551cb0ef41Sopenharmony_ci<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> 29561cb0ef41Sopenharmony_ci<span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code> <button class="copy-button">copy</button></pre> 29571cb0ef41Sopenharmony_ci<p>To parse the URL into its parts:</p> 29581cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(request.<span class="hljs-property">url</span>, <span class="hljs-string">`http://<span class="hljs-subst">${request.headers.host}</span>`</span>);</code> <button class="copy-button">copy</button></pre> 29591cb0ef41Sopenharmony_ci<p>When <code>request.url</code> is <code>'/status?name=ryan'</code> and <code>request.headers.host</code> is 29601cb0ef41Sopenharmony_ci<code>'localhost:3000'</code>:</p> 29611cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node</span> 29621cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">new URL(request.url, `http://<span class="hljs-variable">${request.headers.host}</span>`)</span> 29631cb0ef41Sopenharmony_ciURL { 29641cb0ef41Sopenharmony_ci href: 'http://localhost:3000/status?name=ryan', 29651cb0ef41Sopenharmony_ci origin: 'http://localhost:3000', 29661cb0ef41Sopenharmony_ci protocol: 'http:', 29671cb0ef41Sopenharmony_ci username: '', 29681cb0ef41Sopenharmony_ci password: '', 29691cb0ef41Sopenharmony_ci host: 'localhost:3000', 29701cb0ef41Sopenharmony_ci hostname: 'localhost', 29711cb0ef41Sopenharmony_ci port: '3000', 29721cb0ef41Sopenharmony_ci pathname: '/status', 29731cb0ef41Sopenharmony_ci search: '?name=ryan', 29741cb0ef41Sopenharmony_ci searchParams: URLSearchParams { 'name' => 'ryan' }, 29751cb0ef41Sopenharmony_ci hash: '' 29761cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 29771cb0ef41Sopenharmony_ci</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> 29781cb0ef41Sopenharmony_ci<div class="api_metadata"> 29791cb0ef41Sopenharmony_ci<span>Added in: v0.1.17</span> 29801cb0ef41Sopenharmony_ci</div> 29811cb0ef41Sopenharmony_ci<ul> 29821cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#stream" class="type"><Stream></a></li> 29831cb0ef41Sopenharmony_ci</ul> 29841cb0ef41Sopenharmony_ci<p>This class serves as the parent class of <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a> 29851cb0ef41Sopenharmony_ciand <a href="#class-httpserverresponse"><code>http.ServerResponse</code></a>. It is an abstract outgoing message from 29861cb0ef41Sopenharmony_cithe perspective of the participants of an HTTP transaction.</p> 29871cb0ef41Sopenharmony_ci<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> 29881cb0ef41Sopenharmony_ci<div class="api_metadata"> 29891cb0ef41Sopenharmony_ci<span>Added in: v0.3.6</span> 29901cb0ef41Sopenharmony_ci</div> 29911cb0ef41Sopenharmony_ci<p>Emitted when the buffer of the message is free again.</p> 29921cb0ef41Sopenharmony_ci<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> 29931cb0ef41Sopenharmony_ci<div class="api_metadata"> 29941cb0ef41Sopenharmony_ci<span>Added in: v0.1.17</span> 29951cb0ef41Sopenharmony_ci</div> 29961cb0ef41Sopenharmony_ci<p>Emitted when the transmission is finished successfully.</p> 29971cb0ef41Sopenharmony_ci<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> 29981cb0ef41Sopenharmony_ci<div class="api_metadata"> 29991cb0ef41Sopenharmony_ci<span>Added in: v0.11.6</span> 30001cb0ef41Sopenharmony_ci</div> 30011cb0ef41Sopenharmony_ci<p>Emitted after <code>outgoingMessage.end()</code> is called. 30021cb0ef41Sopenharmony_ciWhen the event is emitted, all data has been processed but not necessarily 30031cb0ef41Sopenharmony_cicompletely flushed.</p> 30041cb0ef41Sopenharmony_ci<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> 30051cb0ef41Sopenharmony_ci<div class="api_metadata"> 30061cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 30071cb0ef41Sopenharmony_ci</div> 30081cb0ef41Sopenharmony_ci<ul> 30091cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 30101cb0ef41Sopenharmony_ci</ul> 30111cb0ef41Sopenharmony_ci<p>Adds HTTP trailers (headers but at the end of the message) to the message.</p> 30121cb0ef41Sopenharmony_ci<p>Trailers will <strong>only</strong> be emitted if the message is chunked encoded. If not, 30131cb0ef41Sopenharmony_cithe trailers will be silently discarded.</p> 30141cb0ef41Sopenharmony_ci<p>HTTP requires the <code>Trailer</code> header to be sent to emit trailers, 30151cb0ef41Sopenharmony_ciwith a list of header field names in its value, e.g.</p> 30161cb0ef41Sopenharmony_ci<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>, 30171cb0ef41Sopenharmony_ci <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> }); 30181cb0ef41Sopenharmony_cimessage.<span class="hljs-title function_">write</span>(fileData); 30191cb0ef41Sopenharmony_cimessage.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-MD5'</span>: <span class="hljs-string">'7895bf4b8828b55ceaf47747b4bca667'</span> }); 30201cb0ef41Sopenharmony_cimessage.<span class="hljs-title function_">end</span>();</code> <button class="copy-button">copy</button></pre> 30211cb0ef41Sopenharmony_ci<p>Attempting to set a header field name or value that contains invalid characters 30221cb0ef41Sopenharmony_ciwill result in a <code>TypeError</code> being thrown.</p> 30231cb0ef41Sopenharmony_ci<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> 30241cb0ef41Sopenharmony_ci<div class="api_metadata"> 30251cb0ef41Sopenharmony_ci<span>Added in: v18.3.0</span> 30261cb0ef41Sopenharmony_ci</div> 30271cb0ef41Sopenharmony_ci<ul> 30281cb0ef41Sopenharmony_ci<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> 30291cb0ef41Sopenharmony_ci<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> 30301cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 30311cb0ef41Sopenharmony_ci</ul> 30321cb0ef41Sopenharmony_ci<p>Append a single header value for the header object.</p> 30331cb0ef41Sopenharmony_ci<p>If the value is an array, this is equivalent of calling this method multiple 30341cb0ef41Sopenharmony_citimes.</p> 30351cb0ef41Sopenharmony_ci<p>If there were no previous value for the header, this is equivalent of calling 30361cb0ef41Sopenharmony_ci<a href="#outgoingmessagesetheadername-value"><code>outgoingMessage.setHeader(name, value)</code></a>.</p> 30371cb0ef41Sopenharmony_ci<p>Depending of the value of <code>options.uniqueHeaders</code> when the client request or the 30381cb0ef41Sopenharmony_ciserver were created, this will end up in the header being sent multiple times or 30391cb0ef41Sopenharmony_cia single time with values joined using <code>; </code>.</p> 30401cb0ef41Sopenharmony_ci<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> 30411cb0ef41Sopenharmony_ci<div class="api_metadata"> 30421cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span><span>Deprecated since: v15.12.0, v14.17.1</span> 30431cb0ef41Sopenharmony_ci</div> 30441cb0ef41Sopenharmony_ci<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> 30451cb0ef41Sopenharmony_ci<p>Alias of <a href="#outgoingmessagesocket"><code>outgoingMessage.socket</code></a>.</p> 30461cb0ef41Sopenharmony_ci<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> 30471cb0ef41Sopenharmony_ci<div class="api_metadata"> 30481cb0ef41Sopenharmony_ci<span>Added in: v13.2.0, v12.16.0</span> 30491cb0ef41Sopenharmony_ci</div> 30501cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writablecork"><code>writable.cork()</code></a>.</p> 30511cb0ef41Sopenharmony_ci<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> 30521cb0ef41Sopenharmony_ci<div class="api_metadata"> 30531cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 30541cb0ef41Sopenharmony_ci</div> 30551cb0ef41Sopenharmony_ci<ul> 30561cb0ef41Sopenharmony_ci<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> 30571cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 30581cb0ef41Sopenharmony_ci</ul> 30591cb0ef41Sopenharmony_ci<p>Destroys the message. Once a socket is associated with the message 30601cb0ef41Sopenharmony_ciand is connected, that socket will be destroyed as well.</p> 30611cb0ef41Sopenharmony_ci<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> 30621cb0ef41Sopenharmony_ci<div class="api_metadata"> 30631cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 30641cb0ef41Sopenharmony_ci<table> 30651cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 30661cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 30671cb0ef41Sopenharmony_ci<td><p>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 30681cb0ef41Sopenharmony_ci<tr><td>v0.11.6</td> 30691cb0ef41Sopenharmony_ci<td><p>add <code>callback</code> argument.</p></td></tr> 30701cb0ef41Sopenharmony_ci<tr><td>v0.1.90</td> 30711cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.90</span></p></td></tr> 30721cb0ef41Sopenharmony_ci</tbody></table> 30731cb0ef41Sopenharmony_ci</details> 30741cb0ef41Sopenharmony_ci</div> 30751cb0ef41Sopenharmony_ci<ul> 30761cb0ef41Sopenharmony_ci<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> 30771cb0ef41Sopenharmony_ci<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> 30781cb0ef41Sopenharmony_ci<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> 30791cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 30801cb0ef41Sopenharmony_ci</ul> 30811cb0ef41Sopenharmony_ci<p>Finishes the outgoing message. If any parts of the body are unsent, it will 30821cb0ef41Sopenharmony_ciflush them to the underlying system. If the message is chunked, it will 30831cb0ef41Sopenharmony_cisend the terminating chunk <code>0\r\n\r\n</code>, and send the trailers (if any).</p> 30841cb0ef41Sopenharmony_ci<p>If <code>chunk</code> is specified, it is equivalent to calling 30851cb0ef41Sopenharmony_ci<code>outgoingMessage.write(chunk, encoding)</code>, followed by 30861cb0ef41Sopenharmony_ci<code>outgoingMessage.end(callback)</code>.</p> 30871cb0ef41Sopenharmony_ci<p>If <code>callback</code> is provided, it will be called when the message is finished 30881cb0ef41Sopenharmony_ci(equivalent to a listener of the <code>'finish'</code> event).</p> 30891cb0ef41Sopenharmony_ci<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> 30901cb0ef41Sopenharmony_ci<div class="api_metadata"> 30911cb0ef41Sopenharmony_ci<span>Added in: v1.6.0</span> 30921cb0ef41Sopenharmony_ci</div> 30931cb0ef41Sopenharmony_ci<p>Flushes the message headers.</p> 30941cb0ef41Sopenharmony_ci<p>For efficiency reason, Node.js normally buffers the message headers 30951cb0ef41Sopenharmony_ciuntil <code>outgoingMessage.end()</code> is called or the first chunk of message data 30961cb0ef41Sopenharmony_ciis written. It then tries to pack the headers and data into a single TCP 30971cb0ef41Sopenharmony_cipacket.</p> 30981cb0ef41Sopenharmony_ci<p>It is usually desired (it saves a TCP round-trip), but not when the first 30991cb0ef41Sopenharmony_cidata is not sent until possibly much later. <code>outgoingMessage.flushHeaders()</code> 31001cb0ef41Sopenharmony_cibypasses the optimization and kickstarts the message.</p> 31011cb0ef41Sopenharmony_ci<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> 31021cb0ef41Sopenharmony_ci<div class="api_metadata"> 31031cb0ef41Sopenharmony_ci<span>Added in: v0.4.0</span> 31041cb0ef41Sopenharmony_ci</div> 31051cb0ef41Sopenharmony_ci<ul> 31061cb0ef41Sopenharmony_ci<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> 31071cb0ef41Sopenharmony_ci<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> 31081cb0ef41Sopenharmony_ci</ul> 31091cb0ef41Sopenharmony_ci<p>Gets the value of the HTTP header with the given name. If that header is not 31101cb0ef41Sopenharmony_ciset, the returned value will be <code>undefined</code>.</p> 31111cb0ef41Sopenharmony_ci<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> 31121cb0ef41Sopenharmony_ci<div class="api_metadata"> 31131cb0ef41Sopenharmony_ci<span>Added in: v7.7.0</span> 31141cb0ef41Sopenharmony_ci</div> 31151cb0ef41Sopenharmony_ci<ul> 31161cb0ef41Sopenharmony_ci<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 31171cb0ef41Sopenharmony_ci</ul> 31181cb0ef41Sopenharmony_ci<p>Returns an array containing the unique names of the current outgoing headers. 31191cb0ef41Sopenharmony_ciAll names are lowercase.</p> 31201cb0ef41Sopenharmony_ci<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> 31211cb0ef41Sopenharmony_ci<div class="api_metadata"> 31221cb0ef41Sopenharmony_ci<span>Added in: v7.7.0</span> 31231cb0ef41Sopenharmony_ci</div> 31241cb0ef41Sopenharmony_ci<ul> 31251cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 31261cb0ef41Sopenharmony_ci</ul> 31271cb0ef41Sopenharmony_ci<p>Returns a shallow copy of the current outgoing headers. Since a shallow 31281cb0ef41Sopenharmony_cicopy is used, array values may be mutated without additional calls to 31291cb0ef41Sopenharmony_civarious header-related HTTP module methods. The keys of the returned 31301cb0ef41Sopenharmony_ciobject are the header names and the values are the respective header 31311cb0ef41Sopenharmony_civalues. All header names are lowercase.</p> 31321cb0ef41Sopenharmony_ci<p>The object returned by the <code>outgoingMessage.getHeaders()</code> method does 31331cb0ef41Sopenharmony_cinot prototypically inherit from the JavaScript <code>Object</code>. This means that 31341cb0ef41Sopenharmony_citypical <code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, 31351cb0ef41Sopenharmony_ciand others are not defined and will not work.</p> 31361cb0ef41Sopenharmony_ci<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>); 31371cb0ef41Sopenharmony_cioutgoingMessage.<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>]); 31381cb0ef41Sopenharmony_ci 31391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> headers = outgoingMessage.<span class="hljs-title function_">getHeaders</span>(); 31401cb0ef41Sopenharmony_ci<span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</span></code> <button class="copy-button">copy</button></pre> 31411cb0ef41Sopenharmony_ci<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> 31421cb0ef41Sopenharmony_ci<div class="api_metadata"> 31431cb0ef41Sopenharmony_ci<span>Added in: v7.7.0</span> 31441cb0ef41Sopenharmony_ci</div> 31451cb0ef41Sopenharmony_ci<ul> 31461cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 31471cb0ef41Sopenharmony_ci<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 31481cb0ef41Sopenharmony_ci</ul> 31491cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the 31501cb0ef41Sopenharmony_cioutgoing headers. The header name is case-insensitive.</p> 31511cb0ef41Sopenharmony_ci<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> 31521cb0ef41Sopenharmony_ci<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> 31531cb0ef41Sopenharmony_ci<div class="api_metadata"> 31541cb0ef41Sopenharmony_ci<span>Added in: v0.9.3</span> 31551cb0ef41Sopenharmony_ci</div> 31561cb0ef41Sopenharmony_ci<ul> 31571cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 31581cb0ef41Sopenharmony_ci</ul> 31591cb0ef41Sopenharmony_ci<p>Read-only. <code>true</code> if the headers were sent, otherwise <code>false</code>.</p> 31601cb0ef41Sopenharmony_ci<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> 31611cb0ef41Sopenharmony_ci<div class="api_metadata"> 31621cb0ef41Sopenharmony_ci<span>Added in: v9.0.0</span> 31631cb0ef41Sopenharmony_ci</div> 31641cb0ef41Sopenharmony_ci<p>Overrides the <code>stream.pipe()</code> method inherited from the legacy <code>Stream</code> class 31651cb0ef41Sopenharmony_ciwhich is the parent class of <code>http.OutgoingMessage</code>.</p> 31661cb0ef41Sopenharmony_ci<p>Calling this method will throw an <code>Error</code> because <code>outgoingMessage</code> is a 31671cb0ef41Sopenharmony_ciwrite-only stream.</p> 31681cb0ef41Sopenharmony_ci<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> 31691cb0ef41Sopenharmony_ci<div class="api_metadata"> 31701cb0ef41Sopenharmony_ci<span>Added in: v0.4.0</span> 31711cb0ef41Sopenharmony_ci</div> 31721cb0ef41Sopenharmony_ci<ul> 31731cb0ef41Sopenharmony_ci<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> 31741cb0ef41Sopenharmony_ci</ul> 31751cb0ef41Sopenharmony_ci<p>Removes a header that is queued for implicit sending.</p> 31761cb0ef41Sopenharmony_ci<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> 31771cb0ef41Sopenharmony_ci<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> 31781cb0ef41Sopenharmony_ci<div class="api_metadata"> 31791cb0ef41Sopenharmony_ci<span>Added in: v0.4.0</span> 31801cb0ef41Sopenharmony_ci</div> 31811cb0ef41Sopenharmony_ci<ul> 31821cb0ef41Sopenharmony_ci<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> 31831cb0ef41Sopenharmony_ci<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> 31841cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 31851cb0ef41Sopenharmony_ci</ul> 31861cb0ef41Sopenharmony_ci<p>Sets a single header value. If the header already exists in the to-be-sent 31871cb0ef41Sopenharmony_ciheaders, its value will be replaced. Use an array of strings to send multiple 31881cb0ef41Sopenharmony_ciheaders with the same name.</p> 31891cb0ef41Sopenharmony_ci<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> 31901cb0ef41Sopenharmony_ci<div class="api_metadata"> 31911cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span> 31921cb0ef41Sopenharmony_ci</div> 31931cb0ef41Sopenharmony_ci<ul> 31941cb0ef41Sopenharmony_ci<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> 31951cb0ef41Sopenharmony_ci<li>Returns: <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 31961cb0ef41Sopenharmony_ci</ul> 31971cb0ef41Sopenharmony_ci<p>Returns the response object.</p> 31981cb0ef41Sopenharmony_ci<p>Sets multiple header values for implicit headers. 31991cb0ef41Sopenharmony_ci<code>headers</code> must be an instance of <a href="globals.html#class-headers"><code>Headers</code></a> or <code>Map</code>, 32001cb0ef41Sopenharmony_ciif a header already exists in the to-be-sent headers, 32011cb0ef41Sopenharmony_ciits value will be replaced.</p> 32021cb0ef41Sopenharmony_ci<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> }); 32031cb0ef41Sopenharmony_ciresponse.<span class="hljs-title function_">setHeaders</span>(headers);</code> <button class="copy-button">copy</button></pre> 32041cb0ef41Sopenharmony_ci<p>or</p> 32051cb0ef41Sopenharmony_ci<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>]]); 32061cb0ef41Sopenharmony_cires.<span class="hljs-title function_">setHeaders</span>(headers);</code> <button class="copy-button">copy</button></pre> 32071cb0ef41Sopenharmony_ci<p>When headers have been set with <a href="#outgoingmessagesetheadersheaders"><code>outgoingMessage.setHeaders()</code></a>, 32081cb0ef41Sopenharmony_cithey will be merged with any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, 32091cb0ef41Sopenharmony_ciwith the headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p> 32101cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span> 32111cb0ef41Sopenharmony_ci<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> { 32121cb0ef41Sopenharmony_ci <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> }); 32131cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">setHeaders</span>(headers); 32141cb0ef41Sopenharmony_ci 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> }); 32151cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 32161cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 32171cb0ef41Sopenharmony_ci<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> 32181cb0ef41Sopenharmony_ci<div class="api_metadata"> 32191cb0ef41Sopenharmony_ci<span>Added in: v0.9.12</span> 32201cb0ef41Sopenharmony_ci</div> 32211cb0ef41Sopenharmony_ci<ul> 32221cb0ef41Sopenharmony_ci<li><code>msesc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 32231cb0ef41Sopenharmony_ci<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 32241cb0ef41Sopenharmony_cioccurs. Same as binding to the <code>timeout</code> event.</li> 32251cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 32261cb0ef41Sopenharmony_ci</ul> 32271cb0ef41Sopenharmony_ci<p>Once a socket is associated with the message and is connected, 32281cb0ef41Sopenharmony_ci<a href="net.html#socketsettimeouttimeout-callback"><code>socket.setTimeout()</code></a> will be called with <code>msecs</code> as the first parameter.</p> 32291cb0ef41Sopenharmony_ci<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> 32301cb0ef41Sopenharmony_ci<div class="api_metadata"> 32311cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 32321cb0ef41Sopenharmony_ci</div> 32331cb0ef41Sopenharmony_ci<ul> 32341cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 32351cb0ef41Sopenharmony_ci</ul> 32361cb0ef41Sopenharmony_ci<p>Reference to the underlying socket. Usually, users will not want to access 32371cb0ef41Sopenharmony_cithis property.</p> 32381cb0ef41Sopenharmony_ci<p>After calling <code>outgoingMessage.end()</code>, this property will be nulled.</p> 32391cb0ef41Sopenharmony_ci<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> 32401cb0ef41Sopenharmony_ci<div class="api_metadata"> 32411cb0ef41Sopenharmony_ci<span>Added in: v13.2.0, v12.16.0</span> 32421cb0ef41Sopenharmony_ci</div> 32431cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writableuncork"><code>writable.uncork()</code></a></p> 32441cb0ef41Sopenharmony_ci<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> 32451cb0ef41Sopenharmony_ci<div class="api_metadata"> 32461cb0ef41Sopenharmony_ci<span>Added in: v13.2.0, v12.16.0</span> 32471cb0ef41Sopenharmony_ci</div> 32481cb0ef41Sopenharmony_ci<ul> 32491cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 32501cb0ef41Sopenharmony_ci</ul> 32511cb0ef41Sopenharmony_ci<p>The number of times <code>outgoingMessage.cork()</code> has been called.</p> 32521cb0ef41Sopenharmony_ci<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> 32531cb0ef41Sopenharmony_ci<div class="api_metadata"> 32541cb0ef41Sopenharmony_ci<span>Added in: v12.9.0</span> 32551cb0ef41Sopenharmony_ci</div> 32561cb0ef41Sopenharmony_ci<ul> 32571cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 32581cb0ef41Sopenharmony_ci</ul> 32591cb0ef41Sopenharmony_ci<p>Is <code>true</code> if <code>outgoingMessage.end()</code> has been called. This property does 32601cb0ef41Sopenharmony_cinot indicate whether the data has been flushed. For that purpose, use 32611cb0ef41Sopenharmony_ci<code>message.writableFinished</code> instead.</p> 32621cb0ef41Sopenharmony_ci<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> 32631cb0ef41Sopenharmony_ci<div class="api_metadata"> 32641cb0ef41Sopenharmony_ci<span>Added in: v12.7.0</span> 32651cb0ef41Sopenharmony_ci</div> 32661cb0ef41Sopenharmony_ci<ul> 32671cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 32681cb0ef41Sopenharmony_ci</ul> 32691cb0ef41Sopenharmony_ci<p>Is <code>true</code> if all data has been flushed to the underlying system.</p> 32701cb0ef41Sopenharmony_ci<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> 32711cb0ef41Sopenharmony_ci<div class="api_metadata"> 32721cb0ef41Sopenharmony_ci<span>Added in: v12.9.0</span> 32731cb0ef41Sopenharmony_ci</div> 32741cb0ef41Sopenharmony_ci<ul> 32751cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 32761cb0ef41Sopenharmony_ci</ul> 32771cb0ef41Sopenharmony_ci<p>The <code>highWaterMark</code> of the underlying socket if assigned. Otherwise, the default 32781cb0ef41Sopenharmony_cibuffer level when <a href="stream.html#writablewritechunk-encoding-callback"><code>writable.write()</code></a> starts returning false (<code>16384</code>).</p> 32791cb0ef41Sopenharmony_ci<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> 32801cb0ef41Sopenharmony_ci<div class="api_metadata"> 32811cb0ef41Sopenharmony_ci<span>Added in: v12.9.0</span> 32821cb0ef41Sopenharmony_ci</div> 32831cb0ef41Sopenharmony_ci<ul> 32841cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 32851cb0ef41Sopenharmony_ci</ul> 32861cb0ef41Sopenharmony_ci<p>The number of buffered bytes.</p> 32871cb0ef41Sopenharmony_ci<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> 32881cb0ef41Sopenharmony_ci<div class="api_metadata"> 32891cb0ef41Sopenharmony_ci<span>Added in: v12.9.0</span> 32901cb0ef41Sopenharmony_ci</div> 32911cb0ef41Sopenharmony_ci<ul> 32921cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 32931cb0ef41Sopenharmony_ci</ul> 32941cb0ef41Sopenharmony_ci<p>Always <code>false</code>.</p> 32951cb0ef41Sopenharmony_ci<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> 32961cb0ef41Sopenharmony_ci<div class="api_metadata"> 32971cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 32981cb0ef41Sopenharmony_ci<table> 32991cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 33001cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 33011cb0ef41Sopenharmony_ci<td><p>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 33021cb0ef41Sopenharmony_ci<tr><td>v0.11.6</td> 33031cb0ef41Sopenharmony_ci<td><p>The <code>callback</code> argument was added.</p></td></tr> 33041cb0ef41Sopenharmony_ci<tr><td>v0.1.29</td> 33051cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.29</span></p></td></tr> 33061cb0ef41Sopenharmony_ci</tbody></table> 33071cb0ef41Sopenharmony_ci</details> 33081cb0ef41Sopenharmony_ci</div> 33091cb0ef41Sopenharmony_ci<ul> 33101cb0ef41Sopenharmony_ci<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> 33111cb0ef41Sopenharmony_ci<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> 33121cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 33131cb0ef41Sopenharmony_ci<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 33141cb0ef41Sopenharmony_ci</ul> 33151cb0ef41Sopenharmony_ci<p>Sends a chunk of the body. This method can be called multiple times.</p> 33161cb0ef41Sopenharmony_ci<p>The <code>encoding</code> argument is only relevant when <code>chunk</code> is a string. Defaults to 33171cb0ef41Sopenharmony_ci<code>'utf8'</code>.</p> 33181cb0ef41Sopenharmony_ci<p>The <code>callback</code> argument is optional and will be called when this chunk of data 33191cb0ef41Sopenharmony_ciis flushed.</p> 33201cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel 33211cb0ef41Sopenharmony_cibuffer. Returns <code>false</code> if all or part of the data was queued in the user 33221cb0ef41Sopenharmony_cimemory. The <code>'drain'</code> event will be emitted when the buffer is free again.</p> 33231cb0ef41Sopenharmony_ci</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> 33241cb0ef41Sopenharmony_ci<div class="api_metadata"> 33251cb0ef41Sopenharmony_ci<span>Added in: v0.11.8</span> 33261cb0ef41Sopenharmony_ci</div> 33271cb0ef41Sopenharmony_ci<ul> 33281cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 33291cb0ef41Sopenharmony_ci</ul> 33301cb0ef41Sopenharmony_ci<p>A list of the HTTP methods that are supported by the parser.</p> 33311cb0ef41Sopenharmony_ci</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> 33321cb0ef41Sopenharmony_ci<div class="api_metadata"> 33331cb0ef41Sopenharmony_ci<span>Added in: v0.1.22</span> 33341cb0ef41Sopenharmony_ci</div> 33351cb0ef41Sopenharmony_ci<ul> 33361cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 33371cb0ef41Sopenharmony_ci</ul> 33381cb0ef41Sopenharmony_ci<p>A collection of all the standard HTTP response status codes, and the 33391cb0ef41Sopenharmony_cishort description of each. For example, <code>http.STATUS_CODES[404] === 'Not Found'</code>.</p> 33401cb0ef41Sopenharmony_ci</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> 33411cb0ef41Sopenharmony_ci<div class="api_metadata"> 33421cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 33431cb0ef41Sopenharmony_ci<table> 33441cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 33451cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td> 33461cb0ef41Sopenharmony_ci<td><p>The <code>highWaterMark</code> option is supported now.</p></td></tr> 33471cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 33481cb0ef41Sopenharmony_ci<td><p>The <code>requestTimeout</code>, <code>headersTimeout</code>, <code>keepAliveTimeout</code>, and <code>connectionsCheckingInterval</code> options are supported now.</p></td></tr> 33491cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 33501cb0ef41Sopenharmony_ci<td><p>The <code>noDelay</code> option now defaults to <code>true</code>.</p></td></tr> 33511cb0ef41Sopenharmony_ci<tr><td>v17.7.0</td> 33521cb0ef41Sopenharmony_ci<td><p>The <code>noDelay</code>, <code>keepAlive</code> and <code>keepAliveInitialDelay</code> options are supported now.</p></td></tr> 33531cb0ef41Sopenharmony_ci<tr><td>v13.3.0</td> 33541cb0ef41Sopenharmony_ci<td><p>The <code>maxHeaderSize</code> option is supported now.</p></td></tr> 33551cb0ef41Sopenharmony_ci<tr><td>v13.8.0, v12.15.0, v10.19.0</td> 33561cb0ef41Sopenharmony_ci<td><p>The <code>insecureHTTPParser</code> option is supported now.</p></td></tr> 33571cb0ef41Sopenharmony_ci<tr><td>v9.6.0, v8.12.0</td> 33581cb0ef41Sopenharmony_ci<td><p>The <code>options</code> argument is supported now.</p></td></tr> 33591cb0ef41Sopenharmony_ci<tr><td>v0.1.13</td> 33601cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.13</span></p></td></tr> 33611cb0ef41Sopenharmony_ci</tbody></table> 33621cb0ef41Sopenharmony_ci</details> 33631cb0ef41Sopenharmony_ci</div> 33641cb0ef41Sopenharmony_ci<ul> 33651cb0ef41Sopenharmony_ci<li> 33661cb0ef41Sopenharmony_ci<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 33671cb0ef41Sopenharmony_ci<ul> 33681cb0ef41Sopenharmony_ci<li><code>connectionsCheckingInterval</code>: Sets the interval value in milliseconds to 33691cb0ef41Sopenharmony_cicheck for request and headers timeout in incomplete requests. 33701cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>30000</code>.</li> 33711cb0ef41Sopenharmony_ci<li><code>headersTimeout</code>: Sets the timeout value in milliseconds for receiving 33721cb0ef41Sopenharmony_cithe complete HTTP headers from the client. 33731cb0ef41Sopenharmony_ciSee <a href="#serverheaderstimeout"><code>server.headersTimeout</code></a> for more information. 33741cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>60000</code>.</li> 33751cb0ef41Sopenharmony_ci<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' 33761cb0ef41Sopenharmony_ci<code>readableHighWaterMark</code> and <code>writableHighWaterMark</code>. This affects 33771cb0ef41Sopenharmony_ci<code>highWaterMark</code> property of both <code>IncomingMessage</code> and <code>ServerResponse</code>. 33781cb0ef41Sopenharmony_ci<strong>Default:</strong> See <a href="stream.html#streamgetdefaulthighwatermarkobjectmode"><code>stream.getDefaultHighWaterMark()</code></a>.</li> 33791cb0ef41Sopenharmony_ci<li><code>insecureHTTPParser</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Use an insecure HTTP parser that accepts 33801cb0ef41Sopenharmony_ciinvalid HTTP headers when <code>true</code>. Using the insecure parser should be 33811cb0ef41Sopenharmony_ciavoided. See <a href="cli.html#--insecure-http-parser"><code>--insecure-http-parser</code></a> for more information. 33821cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 33831cb0ef41Sopenharmony_ci<li><code>IncomingMessage</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Specifies the <code>IncomingMessage</code> 33841cb0ef41Sopenharmony_ciclass to be used. Useful for extending the original <code>IncomingMessage</code>. 33851cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>IncomingMessage</code>.</li> 33861cb0ef41Sopenharmony_ci<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 33871cb0ef41Sopenharmony_ciheaders in a request with <code>, </code> instead of discarding the duplicates. 33881cb0ef41Sopenharmony_ciSee <a href="#messageheaders"><code>message.headers</code></a> for more information. 33891cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 33901cb0ef41Sopenharmony_ci<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 33911cb0ef41Sopenharmony_cion the socket immediately after a new incoming connection is received, 33921cb0ef41Sopenharmony_cisimilarly on what is done in [<code>socket.setKeepAlive([enable][, initialDelay])</code>][<code>socket.setKeepAlive(enable, initialDelay)</code>]. 33931cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 33941cb0ef41Sopenharmony_ci<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 33951cb0ef41Sopenharmony_ciinitial delay before the first keepalive probe is sent on an idle socket. 33961cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>0</code>.</li> 33971cb0ef41Sopenharmony_ci<li><code>requestTimeout</code>: Sets the timeout value in milliseconds for receiving 33981cb0ef41Sopenharmony_cithe entire request from the client. 33991cb0ef41Sopenharmony_ciSee <a href="#serverrequesttimeout"><code>server.requestTimeout</code></a> for more information. 34001cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>300000</code>.</li> 34011cb0ef41Sopenharmony_ci<li><code>ServerResponse</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a> Specifies the <code>ServerResponse</code> class 34021cb0ef41Sopenharmony_cito be used. Useful for extending the original <code>ServerResponse</code>. <strong>Default:</strong> 34031cb0ef41Sopenharmony_ci<code>ServerResponse</code>.</li> 34041cb0ef41Sopenharmony_ci<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 34051cb0ef41Sopenharmony_cionce. If the header's value is an array, the items will be joined 34061cb0ef41Sopenharmony_ciusing <code>; </code>.</li> 34071cb0ef41Sopenharmony_ci</ul> 34081cb0ef41Sopenharmony_ci</li> 34091cb0ef41Sopenharmony_ci<li> 34101cb0ef41Sopenharmony_ci<p><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> 34111cb0ef41Sopenharmony_ci</li> 34121cb0ef41Sopenharmony_ci<li> 34131cb0ef41Sopenharmony_ci<p>Returns: <a href="http.html#class-httpserver" class="type"><http.Server></a></p> 34141cb0ef41Sopenharmony_ci</li> 34151cb0ef41Sopenharmony_ci</ul> 34161cb0ef41Sopenharmony_ci<p>Returns a new instance of <a href="#class-httpserver"><code>http.Server</code></a>.</p> 34171cb0ef41Sopenharmony_ci<p>The <code>requestListener</code> is a function which is automatically 34181cb0ef41Sopenharmony_ciadded to the <a href="#event-request"><code>'request'</code></a> event.</p> 34191cb0ef41Sopenharmony_ci 34201cb0ef41Sopenharmony_ci<pre class="with-34-chars"><input class="js-flavor-selector" 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>; 34211cb0ef41Sopenharmony_ci 34221cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a local server to receive data from</span> 34231cb0ef41Sopenharmony_ci<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> { 34241cb0ef41Sopenharmony_ci 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> }); 34251cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 34261cb0ef41Sopenharmony_ci <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>, 34271cb0ef41Sopenharmony_ci })); 34281cb0ef41Sopenharmony_ci}); 34291cb0ef41Sopenharmony_ci 34301cb0ef41Sopenharmony_ciserver.<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>); 34311cb0ef41Sopenharmony_ci 34321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a local server to receive data from</span> 34331cb0ef41Sopenharmony_ci<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> { 34341cb0ef41Sopenharmony_ci 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> }); 34351cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 34361cb0ef41Sopenharmony_ci <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>, 34371cb0ef41Sopenharmony_ci })); 34381cb0ef41Sopenharmony_ci}); 34391cb0ef41Sopenharmony_ci 34401cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><button class="copy-button">copy</button></pre> 34411cb0ef41Sopenharmony_ci 34421cb0ef41Sopenharmony_ci<pre class="with-34-chars"><input class="js-flavor-selector" 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>; 34431cb0ef41Sopenharmony_ci 34441cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a local server to receive data from</span> 34451cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>(); 34461cb0ef41Sopenharmony_ci 34471cb0ef41Sopenharmony_ci<span class="hljs-comment">// Listen to the request event</span> 34481cb0ef41Sopenharmony_ciserver.<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> { 34491cb0ef41Sopenharmony_ci 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> }); 34501cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 34511cb0ef41Sopenharmony_ci <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>, 34521cb0ef41Sopenharmony_ci })); 34531cb0ef41Sopenharmony_ci}); 34541cb0ef41Sopenharmony_ci 34551cb0ef41Sopenharmony_ciserver.<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>); 34561cb0ef41Sopenharmony_ci 34571cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a local server to receive data from</span> 34581cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>(); 34591cb0ef41Sopenharmony_ci 34601cb0ef41Sopenharmony_ci<span class="hljs-comment">// Listen to the request event</span> 34611cb0ef41Sopenharmony_ciserver.<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> { 34621cb0ef41Sopenharmony_ci 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> }); 34631cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 34641cb0ef41Sopenharmony_ci <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>, 34651cb0ef41Sopenharmony_ci })); 34661cb0ef41Sopenharmony_ci}); 34671cb0ef41Sopenharmony_ci 34681cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><button class="copy-button">copy</button></pre> 34691cb0ef41Sopenharmony_ci</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> 34701cb0ef41Sopenharmony_ci</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> 34711cb0ef41Sopenharmony_ci<div class="api_metadata"> 34721cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 34731cb0ef41Sopenharmony_ci<table> 34741cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 34751cb0ef41Sopenharmony_ci<tr><td>v10.9.0</td> 34761cb0ef41Sopenharmony_ci<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr> 34771cb0ef41Sopenharmony_ci<tr><td>v7.5.0</td> 34781cb0ef41Sopenharmony_ci<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr> 34791cb0ef41Sopenharmony_ci<tr><td>v0.3.6</td> 34801cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.6</span></p></td></tr> 34811cb0ef41Sopenharmony_ci</tbody></table> 34821cb0ef41Sopenharmony_ci</details> 34831cb0ef41Sopenharmony_ci</div> 34841cb0ef41Sopenharmony_ci<ul> 34851cb0ef41Sopenharmony_ci<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> 34861cb0ef41Sopenharmony_ci<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 34871cb0ef41Sopenharmony_ci<a href="#httprequestoptions-callback"><code>http.request()</code></a>, with the method set to GET by default.</li> 34881cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 34891cb0ef41Sopenharmony_ci<li>Returns: <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 34901cb0ef41Sopenharmony_ci</ul> 34911cb0ef41Sopenharmony_ci<p>Since most requests are GET requests without bodies, Node.js provides this 34921cb0ef41Sopenharmony_ciconvenience method. The only difference between this method and 34931cb0ef41Sopenharmony_ci<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> 34941cb0ef41Sopenharmony_ciautomatically. The callback must take care to consume the response 34951cb0ef41Sopenharmony_cidata for reasons stated in <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a> section.</p> 34961cb0ef41Sopenharmony_ci<p>The <code>callback</code> is invoked with a single argument that is an instance of 34971cb0ef41Sopenharmony_ci<a href="#class-httpincomingmessage"><code>http.IncomingMessage</code></a>.</p> 34981cb0ef41Sopenharmony_ci<p>JSON fetching example:</p> 34991cb0ef41Sopenharmony_ci<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> { 35001cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> { statusCode } = res; 35011cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> contentType = res.<span class="hljs-property">headers</span>[<span class="hljs-string">'content-type'</span>]; 35021cb0ef41Sopenharmony_ci 35031cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> error; 35041cb0ef41Sopenharmony_ci <span class="hljs-comment">// Any 2xx status code signals a successful response but</span> 35051cb0ef41Sopenharmony_ci <span class="hljs-comment">// here we're only checking for 200.</span> 35061cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (statusCode !== <span class="hljs-number">200</span>) { 35071cb0ef41Sopenharmony_ci error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Request Failed.\n'</span> + 35081cb0ef41Sopenharmony_ci <span class="hljs-string">`Status Code: <span class="hljs-subst">${statusCode}</span>`</span>); 35091cb0ef41Sopenharmony_ci } <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)) { 35101cb0ef41Sopenharmony_ci error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Invalid content-type.\n'</span> + 35111cb0ef41Sopenharmony_ci <span class="hljs-string">`Expected application/json but received <span class="hljs-subst">${contentType}</span>`</span>); 35121cb0ef41Sopenharmony_ci } 35131cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (error) { 35141cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(error.<span class="hljs-property">message</span>); 35151cb0ef41Sopenharmony_ci <span class="hljs-comment">// Consume response data to free up memory</span> 35161cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">resume</span>(); 35171cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span>; 35181cb0ef41Sopenharmony_ci } 35191cb0ef41Sopenharmony_ci 35201cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>); 35211cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> rawData = <span class="hljs-string">''</span>; 35221cb0ef41Sopenharmony_ci 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; }); 35231cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 35241cb0ef41Sopenharmony_ci <span class="hljs-keyword">try</span> { 35251cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> parsedData = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(rawData); 35261cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(parsedData); 35271cb0ef41Sopenharmony_ci } <span class="hljs-keyword">catch</span> (e) { 35281cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e.<span class="hljs-property">message</span>); 35291cb0ef41Sopenharmony_ci } 35301cb0ef41Sopenharmony_ci }); 35311cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 35321cb0ef41Sopenharmony_ci <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>); 35331cb0ef41Sopenharmony_ci}); 35341cb0ef41Sopenharmony_ci 35351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a local server to receive data from</span> 35361cb0ef41Sopenharmony_ci<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> { 35371cb0ef41Sopenharmony_ci 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> }); 35381cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 35391cb0ef41Sopenharmony_ci <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>, 35401cb0ef41Sopenharmony_ci })); 35411cb0ef41Sopenharmony_ci}); 35421cb0ef41Sopenharmony_ci 35431cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre> 35441cb0ef41Sopenharmony_ci</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> 35451cb0ef41Sopenharmony_ci<div class="api_metadata"> 35461cb0ef41Sopenharmony_ci<span>Added in: v0.5.9</span> 35471cb0ef41Sopenharmony_ci</div> 35481cb0ef41Sopenharmony_ci<ul> 35491cb0ef41Sopenharmony_ci<li><a href="http.html#class-httpagent" class="type"><http.Agent></a></li> 35501cb0ef41Sopenharmony_ci</ul> 35511cb0ef41Sopenharmony_ci<p>Global instance of <code>Agent</code> which is used as the default for all HTTP client 35521cb0ef41Sopenharmony_cirequests.</p> 35531cb0ef41Sopenharmony_ci</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> 35541cb0ef41Sopenharmony_ci<div class="api_metadata"> 35551cb0ef41Sopenharmony_ci<span>Added in: v11.6.0, v10.15.0</span> 35561cb0ef41Sopenharmony_ci</div> 35571cb0ef41Sopenharmony_ci<ul> 35581cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 35591cb0ef41Sopenharmony_ci</ul> 35601cb0ef41Sopenharmony_ci<p>Read-only property specifying the maximum allowed size of HTTP headers in bytes. 35611cb0ef41Sopenharmony_ciDefaults to 16 KiB. Configurable using the <a href="cli.html#--max-http-header-sizesize"><code>--max-http-header-size</code></a> CLI 35621cb0ef41Sopenharmony_cioption.</p> 35631cb0ef41Sopenharmony_ci<p>This can be overridden for servers and client requests by passing the 35641cb0ef41Sopenharmony_ci<code>maxHeaderSize</code> option.</p> 35651cb0ef41Sopenharmony_ci</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> 35661cb0ef41Sopenharmony_ci</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> 35671cb0ef41Sopenharmony_ci<div class="api_metadata"> 35681cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 35691cb0ef41Sopenharmony_ci<table> 35701cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 35711cb0ef41Sopenharmony_ci<tr><td>v16.7.0, v14.18.0</td> 35721cb0ef41Sopenharmony_ci<td><p>When using a <code>URL</code> object parsed username and password will now be properly URI decoded.</p></td></tr> 35731cb0ef41Sopenharmony_ci<tr><td>v15.3.0, v14.17.0</td> 35741cb0ef41Sopenharmony_ci<td><p>It is possible to abort a request with an AbortSignal.</p></td></tr> 35751cb0ef41Sopenharmony_ci<tr><td>v13.3.0</td> 35761cb0ef41Sopenharmony_ci<td><p>The <code>maxHeaderSize</code> option is supported now.</p></td></tr> 35771cb0ef41Sopenharmony_ci<tr><td>v13.8.0, v12.15.0, v10.19.0</td> 35781cb0ef41Sopenharmony_ci<td><p>The <code>insecureHTTPParser</code> option is supported now.</p></td></tr> 35791cb0ef41Sopenharmony_ci<tr><td>v10.9.0</td> 35801cb0ef41Sopenharmony_ci<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr> 35811cb0ef41Sopenharmony_ci<tr><td>v7.5.0</td> 35821cb0ef41Sopenharmony_ci<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr> 35831cb0ef41Sopenharmony_ci<tr><td>v0.3.6</td> 35841cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.6</span></p></td></tr> 35851cb0ef41Sopenharmony_ci</tbody></table> 35861cb0ef41Sopenharmony_ci</details> 35871cb0ef41Sopenharmony_ci</div> 35881cb0ef41Sopenharmony_ci<ul> 35891cb0ef41Sopenharmony_ci<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> 35901cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 35911cb0ef41Sopenharmony_ci<ul> 35921cb0ef41Sopenharmony_ci<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 35931cb0ef41Sopenharmony_civalues: 35941cb0ef41Sopenharmony_ci<ul> 35951cb0ef41Sopenharmony_ci<li><code>undefined</code> (default): use <a href="#httpglobalagent"><code>http.globalAgent</code></a> for this host and port.</li> 35961cb0ef41Sopenharmony_ci<li><code>Agent</code> object: explicitly use the passed in <code>Agent</code>.</li> 35971cb0ef41Sopenharmony_ci<li><code>false</code>: causes a new <code>Agent</code> with default values to be used.</li> 35981cb0ef41Sopenharmony_ci</ul> 35991cb0ef41Sopenharmony_ci</li> 36001cb0ef41Sopenharmony_ci<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 36011cb0ef41Sopenharmony_ciAuthorization header.</li> 36021cb0ef41Sopenharmony_ci<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 36031cb0ef41Sopenharmony_ciuse for the request when the <code>agent</code> option is not used. This can be used to 36041cb0ef41Sopenharmony_ciavoid creating a custom <code>Agent</code> class just to override the default 36051cb0ef41Sopenharmony_ci<code>createConnection</code> function. See <a href="#agentcreateconnectionoptions-callback"><code>agent.createConnection()</code></a> for more 36061cb0ef41Sopenharmony_cidetails. Any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream is a valid return value.</li> 36071cb0ef41Sopenharmony_ci<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> 36081cb0ef41Sopenharmony_ci<code>agent.defaultPort</code> if an <code>Agent</code> is used, else <code>undefined</code>.</li> 36091cb0ef41Sopenharmony_ci<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 36101cb0ef41Sopenharmony_ci<code>hostname</code>. Valid values are <code>4</code> or <code>6</code>. When unspecified, both IP v4 and 36111cb0ef41Sopenharmony_civ6 will be used.</li> 36121cb0ef41Sopenharmony_ci<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> 36131cb0ef41Sopenharmony_ci<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> 36141cb0ef41Sopenharmony_ci<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 36151cb0ef41Sopenharmony_cirequest to. <strong>Default:</strong> <code>'localhost'</code>.</li> 36161cb0ef41Sopenharmony_ci<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>, 36171cb0ef41Sopenharmony_ci<code>hostname</code> will be used if both <code>host</code> and <code>hostname</code> are specified.</li> 36181cb0ef41Sopenharmony_ci<li><code>insecureHTTPParser</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Use an insecure HTTP parser that accepts 36191cb0ef41Sopenharmony_ciinvalid HTTP headers when <code>true</code>. Using the insecure parser should be 36201cb0ef41Sopenharmony_ciavoided. See <a href="cli.html#--insecure-http-parser"><code>--insecure-http-parser</code></a> for more information. 36211cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code></li> 36221cb0ef41Sopenharmony_ci<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 36231cb0ef41Sopenharmony_cimultiple headers in a request with <code>, </code> instead of discarding 36241cb0ef41Sopenharmony_cithe duplicates. See <a href="#messageheaders"><code>message.headers</code></a> for more information. 36251cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 36261cb0ef41Sopenharmony_ci<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> 36271cb0ef41Sopenharmony_ci<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> 36281cb0ef41Sopenharmony_ci<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> 36291cb0ef41Sopenharmony_ci<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 36301cb0ef41Sopenharmony_ci<a href="cli.html#--max-http-header-sizesize"><code>--max-http-header-size</code></a> (the maximum length of response headers in 36311cb0ef41Sopenharmony_cibytes) for responses received from the server. 36321cb0ef41Sopenharmony_ci<strong>Default:</strong> 16384 (16 KiB).</li> 36331cb0ef41Sopenharmony_ci<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> 36341cb0ef41Sopenharmony_ci<code>'GET'</code>.</li> 36351cb0ef41Sopenharmony_ci<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. 36361cb0ef41Sopenharmony_ciE.G. <code>'/index.html?page=12'</code>. An exception is thrown when the request path 36371cb0ef41Sopenharmony_cicontains illegal characters. Currently, only spaces are rejected but that 36381cb0ef41Sopenharmony_cimay change in the future. <strong>Default:</strong> <code>'/'</code>.</li> 36391cb0ef41Sopenharmony_ci<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, 36401cb0ef41Sopenharmony_cielse <code>80</code>.</li> 36411cb0ef41Sopenharmony_ci<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> 36421cb0ef41Sopenharmony_ci<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 36431cb0ef41Sopenharmony_ci<code>Host</code> header. Defaults to <code>true</code>.</li> 36441cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a>: An AbortSignal that may be used to abort an ongoing 36451cb0ef41Sopenharmony_cirequest.</li> 36461cb0ef41Sopenharmony_ci<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> 36471cb0ef41Sopenharmony_cior <code>port</code> is specified, as those specify a TCP Socket.</li> 36481cb0ef41Sopenharmony_ci<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. 36491cb0ef41Sopenharmony_ciThis will set the timeout before the socket is connected.</li> 36501cb0ef41Sopenharmony_ci<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 36511cb0ef41Sopenharmony_cionly once. If the header's value is an array, the items will be joined 36521cb0ef41Sopenharmony_ciusing <code>; </code>.</li> 36531cb0ef41Sopenharmony_ci</ul> 36541cb0ef41Sopenharmony_ci</li> 36551cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 36561cb0ef41Sopenharmony_ci<li>Returns: <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 36571cb0ef41Sopenharmony_ci</ul> 36581cb0ef41Sopenharmony_ci<p><code>options</code> in <a href="net.html#socketconnectoptions-connectlistener"><code>socket.connect()</code></a> are also supported.</p> 36591cb0ef41Sopenharmony_ci<p>Node.js maintains several connections per server to make HTTP requests. 36601cb0ef41Sopenharmony_ciThis function allows one to transparently issue requests.</p> 36611cb0ef41Sopenharmony_ci<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 36621cb0ef41Sopenharmony_cistring, 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> 36631cb0ef41Sopenharmony_ciobject, it will be automatically converted to an ordinary <code>options</code> object.</p> 36641cb0ef41Sopenharmony_ci<p>If both <code>url</code> and <code>options</code> are specified, the objects are merged, with the 36651cb0ef41Sopenharmony_ci<code>options</code> properties taking precedence.</p> 36661cb0ef41Sopenharmony_ci<p>The optional <code>callback</code> parameter will be added as a one-time listener for 36671cb0ef41Sopenharmony_cithe <a href="#event-response"><code>'response'</code></a> event.</p> 36681cb0ef41Sopenharmony_ci<p><code>http.request()</code> returns an instance of the <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a> 36691cb0ef41Sopenharmony_ciclass. The <code>ClientRequest</code> instance is a writable stream. If one needs to 36701cb0ef41Sopenharmony_ciupload a file with a POST request, then write to the <code>ClientRequest</code> object.</p> 36711cb0ef41Sopenharmony_ci 36721cb0ef41Sopenharmony_ci<pre class="with-37-chars"><input class="js-flavor-selector" 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>; 36731cb0ef41Sopenharmony_ci<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>; 36741cb0ef41Sopenharmony_ci 36751cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> postData = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 36761cb0ef41Sopenharmony_ci <span class="hljs-string">'msg'</span>: <span class="hljs-string">'Hello World!'</span>, 36771cb0ef41Sopenharmony_ci}); 36781cb0ef41Sopenharmony_ci 36791cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = { 36801cb0ef41Sopenharmony_ci <span class="hljs-attr">hostname</span>: <span class="hljs-string">'www.google.com'</span>, 36811cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>, 36821cb0ef41Sopenharmony_ci <span class="hljs-attr">path</span>: <span class="hljs-string">'/upload'</span>, 36831cb0ef41Sopenharmony_ci <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>, 36841cb0ef41Sopenharmony_ci <span class="hljs-attr">headers</span>: { 36851cb0ef41Sopenharmony_ci <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>, 36861cb0ef41Sopenharmony_ci <span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(postData), 36871cb0ef41Sopenharmony_ci }, 36881cb0ef41Sopenharmony_ci}; 36891cb0ef41Sopenharmony_ci 36901cb0ef41Sopenharmony_ci<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> { 36911cb0ef41Sopenharmony_ci <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>); 36921cb0ef41Sopenharmony_ci <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>); 36931cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>); 36941cb0ef41Sopenharmony_ci 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> { 36951cb0ef41Sopenharmony_ci <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>); 36961cb0ef41Sopenharmony_ci }); 36971cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 36981cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'No more data in response.'</span>); 36991cb0ef41Sopenharmony_ci }); 37001cb0ef41Sopenharmony_ci}); 37011cb0ef41Sopenharmony_ci 37021cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 37031cb0ef41Sopenharmony_ci <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>); 37041cb0ef41Sopenharmony_ci}); 37051cb0ef41Sopenharmony_ci 37061cb0ef41Sopenharmony_ci<span class="hljs-comment">// Write data to request body</span> 37071cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">write</span>(postData); 37081cb0ef41Sopenharmony_cireq.<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>); 37091cb0ef41Sopenharmony_ci 37101cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> postData = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 37111cb0ef41Sopenharmony_ci <span class="hljs-string">'msg'</span>: <span class="hljs-string">'Hello World!'</span>, 37121cb0ef41Sopenharmony_ci}); 37131cb0ef41Sopenharmony_ci 37141cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = { 37151cb0ef41Sopenharmony_ci <span class="hljs-attr">hostname</span>: <span class="hljs-string">'www.google.com'</span>, 37161cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>, 37171cb0ef41Sopenharmony_ci <span class="hljs-attr">path</span>: <span class="hljs-string">'/upload'</span>, 37181cb0ef41Sopenharmony_ci <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>, 37191cb0ef41Sopenharmony_ci <span class="hljs-attr">headers</span>: { 37201cb0ef41Sopenharmony_ci <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>, 37211cb0ef41Sopenharmony_ci <span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(postData), 37221cb0ef41Sopenharmony_ci }, 37231cb0ef41Sopenharmony_ci}; 37241cb0ef41Sopenharmony_ci 37251cb0ef41Sopenharmony_ci<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> { 37261cb0ef41Sopenharmony_ci <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>); 37271cb0ef41Sopenharmony_ci <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>); 37281cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>); 37291cb0ef41Sopenharmony_ci 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> { 37301cb0ef41Sopenharmony_ci <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>); 37311cb0ef41Sopenharmony_ci }); 37321cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 37331cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'No more data in response.'</span>); 37341cb0ef41Sopenharmony_ci }); 37351cb0ef41Sopenharmony_ci}); 37361cb0ef41Sopenharmony_ci 37371cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 37381cb0ef41Sopenharmony_ci <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>); 37391cb0ef41Sopenharmony_ci}); 37401cb0ef41Sopenharmony_ci 37411cb0ef41Sopenharmony_ci<span class="hljs-comment">// Write data to request body</span> 37421cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">write</span>(postData); 37431cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">end</span>();</code><button class="copy-button">copy</button></pre> 37441cb0ef41Sopenharmony_ci<p>In the example <code>req.end()</code> was called. With <code>http.request()</code> one 37451cb0ef41Sopenharmony_cimust always call <code>req.end()</code> to signify the end of the request - 37461cb0ef41Sopenharmony_cieven if there is no data being written to the request body.</p> 37471cb0ef41Sopenharmony_ci<p>If any error is encountered during the request (be that with DNS resolution, 37481cb0ef41Sopenharmony_ciTCP level errors, or actual HTTP parse errors) an <code>'error'</code> event is emitted 37491cb0ef41Sopenharmony_cion the returned request object. As with all <code>'error'</code> events, if no listeners 37501cb0ef41Sopenharmony_ciare registered the error will be thrown.</p> 37511cb0ef41Sopenharmony_ci<p>There are a few special headers that should be noted.</p> 37521cb0ef41Sopenharmony_ci<ul> 37531cb0ef41Sopenharmony_ci<li> 37541cb0ef41Sopenharmony_ci<p>Sending a 'Connection: keep-alive' will notify Node.js that the connection to 37551cb0ef41Sopenharmony_cithe server should be persisted until the next request.</p> 37561cb0ef41Sopenharmony_ci</li> 37571cb0ef41Sopenharmony_ci<li> 37581cb0ef41Sopenharmony_ci<p>Sending a 'Content-Length' header will disable the default chunked encoding.</p> 37591cb0ef41Sopenharmony_ci</li> 37601cb0ef41Sopenharmony_ci<li> 37611cb0ef41Sopenharmony_ci<p>Sending an 'Expect' header will immediately send the request headers. 37621cb0ef41Sopenharmony_ciUsually, when sending 'Expect: 100-continue', both a timeout and a listener 37631cb0ef41Sopenharmony_cifor the <code>'continue'</code> event should be set. See RFC 2616 Section 8.2.3 for more 37641cb0ef41Sopenharmony_ciinformation.</p> 37651cb0ef41Sopenharmony_ci</li> 37661cb0ef41Sopenharmony_ci<li> 37671cb0ef41Sopenharmony_ci<p>Sending an Authorization header will override using the <code>auth</code> option 37681cb0ef41Sopenharmony_cito compute basic authentication.</p> 37691cb0ef41Sopenharmony_ci</li> 37701cb0ef41Sopenharmony_ci</ul> 37711cb0ef41Sopenharmony_ci<p>Example using a <a href="url.html#the-whatwg-url-api"><code>URL</code></a> as <code>options</code>:</p> 37721cb0ef41Sopenharmony_ci<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>); 37731cb0ef41Sopenharmony_ci 37741cb0ef41Sopenharmony_ci<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> { 37751cb0ef41Sopenharmony_ci <span class="hljs-comment">// ...</span> 37761cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 37771cb0ef41Sopenharmony_ci<p>In a successful request, the following events will be emitted in the following 37781cb0ef41Sopenharmony_ciorder:</p> 37791cb0ef41Sopenharmony_ci<ul> 37801cb0ef41Sopenharmony_ci<li><code>'socket'</code></li> 37811cb0ef41Sopenharmony_ci<li><code>'response'</code> 37821cb0ef41Sopenharmony_ci<ul> 37831cb0ef41Sopenharmony_ci<li><code>'data'</code> any number of times, on the <code>res</code> object 37841cb0ef41Sopenharmony_ci(<code>'data'</code> will not be emitted at all if the response body is empty, for 37851cb0ef41Sopenharmony_ciinstance, in most redirects)</li> 37861cb0ef41Sopenharmony_ci<li><code>'end'</code> on the <code>res</code> object</li> 37871cb0ef41Sopenharmony_ci</ul> 37881cb0ef41Sopenharmony_ci</li> 37891cb0ef41Sopenharmony_ci<li><code>'close'</code></li> 37901cb0ef41Sopenharmony_ci</ul> 37911cb0ef41Sopenharmony_ci<p>In the case of a connection error, the following events will be emitted:</p> 37921cb0ef41Sopenharmony_ci<ul> 37931cb0ef41Sopenharmony_ci<li><code>'socket'</code></li> 37941cb0ef41Sopenharmony_ci<li><code>'error'</code></li> 37951cb0ef41Sopenharmony_ci<li><code>'close'</code></li> 37961cb0ef41Sopenharmony_ci</ul> 37971cb0ef41Sopenharmony_ci<p>In the case of a premature connection close before the response is received, 37981cb0ef41Sopenharmony_cithe following events will be emitted in the following order:</p> 37991cb0ef41Sopenharmony_ci<ul> 38001cb0ef41Sopenharmony_ci<li><code>'socket'</code></li> 38011cb0ef41Sopenharmony_ci<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code 38021cb0ef41Sopenharmony_ci<code>'ECONNRESET'</code></li> 38031cb0ef41Sopenharmony_ci<li><code>'close'</code></li> 38041cb0ef41Sopenharmony_ci</ul> 38051cb0ef41Sopenharmony_ci<p>In the case of a premature connection close after the response is received, 38061cb0ef41Sopenharmony_cithe following events will be emitted in the following order:</p> 38071cb0ef41Sopenharmony_ci<ul> 38081cb0ef41Sopenharmony_ci<li><code>'socket'</code></li> 38091cb0ef41Sopenharmony_ci<li><code>'response'</code> 38101cb0ef41Sopenharmony_ci<ul> 38111cb0ef41Sopenharmony_ci<li><code>'data'</code> any number of times, on the <code>res</code> object</li> 38121cb0ef41Sopenharmony_ci</ul> 38131cb0ef41Sopenharmony_ci</li> 38141cb0ef41Sopenharmony_ci<li>(connection closed here)</li> 38151cb0ef41Sopenharmony_ci<li><code>'aborted'</code> on the <code>res</code> object</li> 38161cb0ef41Sopenharmony_ci<li><code>'error'</code> on the <code>res</code> object with an error with message 38171cb0ef41Sopenharmony_ci<code>'Error: aborted'</code> and code <code>'ECONNRESET'</code></li> 38181cb0ef41Sopenharmony_ci<li><code>'close'</code></li> 38191cb0ef41Sopenharmony_ci<li><code>'close'</code> on the <code>res</code> object</li> 38201cb0ef41Sopenharmony_ci</ul> 38211cb0ef41Sopenharmony_ci<p>If <code>req.destroy()</code> is called before a socket is assigned, the following 38221cb0ef41Sopenharmony_cievents will be emitted in the following order:</p> 38231cb0ef41Sopenharmony_ci<ul> 38241cb0ef41Sopenharmony_ci<li>(<code>req.destroy()</code> called here)</li> 38251cb0ef41Sopenharmony_ci<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code 38261cb0ef41Sopenharmony_ci<code>'ECONNRESET'</code>, or the error with which <code>req.destroy()</code> was called</li> 38271cb0ef41Sopenharmony_ci<li><code>'close'</code></li> 38281cb0ef41Sopenharmony_ci</ul> 38291cb0ef41Sopenharmony_ci<p>If <code>req.destroy()</code> is called before the connection succeeds, the following 38301cb0ef41Sopenharmony_cievents will be emitted in the following order:</p> 38311cb0ef41Sopenharmony_ci<ul> 38321cb0ef41Sopenharmony_ci<li><code>'socket'</code></li> 38331cb0ef41Sopenharmony_ci<li>(<code>req.destroy()</code> called here)</li> 38341cb0ef41Sopenharmony_ci<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code 38351cb0ef41Sopenharmony_ci<code>'ECONNRESET'</code>, or the error with which <code>req.destroy()</code> was called</li> 38361cb0ef41Sopenharmony_ci<li><code>'close'</code></li> 38371cb0ef41Sopenharmony_ci</ul> 38381cb0ef41Sopenharmony_ci<p>If <code>req.destroy()</code> is called after the response is received, the following 38391cb0ef41Sopenharmony_cievents will be emitted in the following order:</p> 38401cb0ef41Sopenharmony_ci<ul> 38411cb0ef41Sopenharmony_ci<li><code>'socket'</code></li> 38421cb0ef41Sopenharmony_ci<li><code>'response'</code> 38431cb0ef41Sopenharmony_ci<ul> 38441cb0ef41Sopenharmony_ci<li><code>'data'</code> any number of times, on the <code>res</code> object</li> 38451cb0ef41Sopenharmony_ci</ul> 38461cb0ef41Sopenharmony_ci</li> 38471cb0ef41Sopenharmony_ci<li>(<code>req.destroy()</code> called here)</li> 38481cb0ef41Sopenharmony_ci<li><code>'aborted'</code> on the <code>res</code> object</li> 38491cb0ef41Sopenharmony_ci<li><code>'error'</code> on the <code>res</code> object with an error with message <code>'Error: aborted'</code> 38501cb0ef41Sopenharmony_ciand code <code>'ECONNRESET'</code>, or the error with which <code>req.destroy()</code> was called</li> 38511cb0ef41Sopenharmony_ci<li><code>'close'</code></li> 38521cb0ef41Sopenharmony_ci<li><code>'close'</code> on the <code>res</code> object</li> 38531cb0ef41Sopenharmony_ci</ul> 38541cb0ef41Sopenharmony_ci<p>If <code>req.abort()</code> is called before a socket is assigned, the following 38551cb0ef41Sopenharmony_cievents will be emitted in the following order:</p> 38561cb0ef41Sopenharmony_ci<ul> 38571cb0ef41Sopenharmony_ci<li>(<code>req.abort()</code> called here)</li> 38581cb0ef41Sopenharmony_ci<li><code>'abort'</code></li> 38591cb0ef41Sopenharmony_ci<li><code>'close'</code></li> 38601cb0ef41Sopenharmony_ci</ul> 38611cb0ef41Sopenharmony_ci<p>If <code>req.abort()</code> is called before the connection succeeds, the following 38621cb0ef41Sopenharmony_cievents will be emitted in the following order:</p> 38631cb0ef41Sopenharmony_ci<ul> 38641cb0ef41Sopenharmony_ci<li><code>'socket'</code></li> 38651cb0ef41Sopenharmony_ci<li>(<code>req.abort()</code> called here)</li> 38661cb0ef41Sopenharmony_ci<li><code>'abort'</code></li> 38671cb0ef41Sopenharmony_ci<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code 38681cb0ef41Sopenharmony_ci<code>'ECONNRESET'</code></li> 38691cb0ef41Sopenharmony_ci<li><code>'close'</code></li> 38701cb0ef41Sopenharmony_ci</ul> 38711cb0ef41Sopenharmony_ci<p>If <code>req.abort()</code> is called after the response is received, the following 38721cb0ef41Sopenharmony_cievents will be emitted in the following order:</p> 38731cb0ef41Sopenharmony_ci<ul> 38741cb0ef41Sopenharmony_ci<li><code>'socket'</code></li> 38751cb0ef41Sopenharmony_ci<li><code>'response'</code> 38761cb0ef41Sopenharmony_ci<ul> 38771cb0ef41Sopenharmony_ci<li><code>'data'</code> any number of times, on the <code>res</code> object</li> 38781cb0ef41Sopenharmony_ci</ul> 38791cb0ef41Sopenharmony_ci</li> 38801cb0ef41Sopenharmony_ci<li>(<code>req.abort()</code> called here)</li> 38811cb0ef41Sopenharmony_ci<li><code>'abort'</code></li> 38821cb0ef41Sopenharmony_ci<li><code>'aborted'</code> on the <code>res</code> object</li> 38831cb0ef41Sopenharmony_ci<li><code>'error'</code> on the <code>res</code> object with an error with message 38841cb0ef41Sopenharmony_ci<code>'Error: aborted'</code> and code <code>'ECONNRESET'</code>.</li> 38851cb0ef41Sopenharmony_ci<li><code>'close'</code></li> 38861cb0ef41Sopenharmony_ci<li><code>'close'</code> on the <code>res</code> object</li> 38871cb0ef41Sopenharmony_ci</ul> 38881cb0ef41Sopenharmony_ci<p>Setting the <code>timeout</code> option or using the <code>setTimeout()</code> function will 38891cb0ef41Sopenharmony_cinot abort the request or do anything besides add a <code>'timeout'</code> event.</p> 38901cb0ef41Sopenharmony_ci<p>Passing an <code>AbortSignal</code> and then calling <code>abort()</code> on the corresponding 38911cb0ef41Sopenharmony_ci<code>AbortController</code> will behave the same way as calling <code>.destroy()</code> on the 38921cb0ef41Sopenharmony_cirequest. Specifically, the <code>'error'</code> event will be emitted with an error with 38931cb0ef41Sopenharmony_cithe message <code>'AbortError: The operation was aborted'</code>, the code <code>'ABORT_ERR'</code> 38941cb0ef41Sopenharmony_ciand the <code>cause</code>, if one was provided.</p> 38951cb0ef41Sopenharmony_ci</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> 38961cb0ef41Sopenharmony_ci<div class="api_metadata"> 38971cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 38981cb0ef41Sopenharmony_ci<table> 38991cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 39001cb0ef41Sopenharmony_ci<tr><td>v18.14.0</td> 39011cb0ef41Sopenharmony_ci<td><p>The <code>label</code> parameter is added.</p></td></tr> 39021cb0ef41Sopenharmony_ci<tr><td>v14.3.0</td> 39031cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.3.0</span></p></td></tr> 39041cb0ef41Sopenharmony_ci</tbody></table> 39051cb0ef41Sopenharmony_ci</details> 39061cb0ef41Sopenharmony_ci</div> 39071cb0ef41Sopenharmony_ci<ul> 39081cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 39091cb0ef41Sopenharmony_ci<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> 39101cb0ef41Sopenharmony_ci</ul> 39111cb0ef41Sopenharmony_ci<p>Performs the low-level validations on the provided <code>name</code> that are done when 39121cb0ef41Sopenharmony_ci<code>res.setHeader(name, value)</code> is called.</p> 39131cb0ef41Sopenharmony_ci<p>Passing illegal value as <code>name</code> will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown, 39141cb0ef41Sopenharmony_ciidentified by <code>code: 'ERR_INVALID_HTTP_TOKEN'</code>.</p> 39151cb0ef41Sopenharmony_ci<p>It is not necessary to use this method before passing headers to an HTTP request 39161cb0ef41Sopenharmony_cior response. The HTTP module will automatically validate such headers. 39171cb0ef41Sopenharmony_ciExamples:</p> 39181cb0ef41Sopenharmony_ci<p>Example:</p> 39191cb0ef41Sopenharmony_ci 39201cb0ef41Sopenharmony_ci<pre class="with-52-chars"><input class="js-flavor-selector" 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>; 39211cb0ef41Sopenharmony_ci 39221cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 39231cb0ef41Sopenharmony_ci <span class="hljs-title function_">validateHeaderName</span>(<span class="hljs-string">''</span>); 39241cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 39251cb0ef41Sopenharmony_ci <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> 39261cb0ef41Sopenharmony_ci <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> 39271cb0ef41Sopenharmony_ci <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> 39281cb0ef41Sopenharmony_ci}</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>); 39291cb0ef41Sopenharmony_ci 39301cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 39311cb0ef41Sopenharmony_ci <span class="hljs-title function_">validateHeaderName</span>(<span class="hljs-string">''</span>); 39321cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 39331cb0ef41Sopenharmony_ci <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> 39341cb0ef41Sopenharmony_ci <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> 39351cb0ef41Sopenharmony_ci <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> 39361cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 39371cb0ef41Sopenharmony_ci</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> 39381cb0ef41Sopenharmony_ci<div class="api_metadata"> 39391cb0ef41Sopenharmony_ci<span>Added in: v14.3.0</span> 39401cb0ef41Sopenharmony_ci</div> 39411cb0ef41Sopenharmony_ci<ul> 39421cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 39431cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 39441cb0ef41Sopenharmony_ci</ul> 39451cb0ef41Sopenharmony_ci<p>Performs the low-level validations on the provided <code>value</code> that are done when 39461cb0ef41Sopenharmony_ci<code>res.setHeader(name, value)</code> is called.</p> 39471cb0ef41Sopenharmony_ci<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> 39481cb0ef41Sopenharmony_ci<ul> 39491cb0ef41Sopenharmony_ci<li>Undefined value error is identified by <code>code: 'ERR_HTTP_INVALID_HEADER_VALUE'</code>.</li> 39501cb0ef41Sopenharmony_ci<li>Invalid value character error is identified by <code>code: 'ERR_INVALID_CHAR'</code>.</li> 39511cb0ef41Sopenharmony_ci</ul> 39521cb0ef41Sopenharmony_ci<p>It is not necessary to use this method before passing headers to an HTTP request 39531cb0ef41Sopenharmony_cior response. The HTTP module will automatically validate such headers.</p> 39541cb0ef41Sopenharmony_ci<p>Examples:</p> 39551cb0ef41Sopenharmony_ci 39561cb0ef41Sopenharmony_ci<pre class="with-53-chars"><input class="js-flavor-selector" 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>; 39571cb0ef41Sopenharmony_ci 39581cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 39591cb0ef41Sopenharmony_ci <span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-literal">undefined</span>); 39601cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 39611cb0ef41Sopenharmony_ci <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> 39621cb0ef41Sopenharmony_ci <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> 39631cb0ef41Sopenharmony_ci <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> 39641cb0ef41Sopenharmony_ci} 39651cb0ef41Sopenharmony_ci 39661cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 39671cb0ef41Sopenharmony_ci <span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-string">'oʊmɪɡə'</span>); 39681cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 39691cb0ef41Sopenharmony_ci <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> 39701cb0ef41Sopenharmony_ci <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> 39711cb0ef41Sopenharmony_ci <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> 39721cb0ef41Sopenharmony_ci}</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>); 39731cb0ef41Sopenharmony_ci 39741cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 39751cb0ef41Sopenharmony_ci <span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-literal">undefined</span>); 39761cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 39771cb0ef41Sopenharmony_ci <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> 39781cb0ef41Sopenharmony_ci <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> 39791cb0ef41Sopenharmony_ci <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> 39801cb0ef41Sopenharmony_ci} 39811cb0ef41Sopenharmony_ci 39821cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 39831cb0ef41Sopenharmony_ci <span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-string">'oʊmɪɡə'</span>); 39841cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 39851cb0ef41Sopenharmony_ci <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> 39861cb0ef41Sopenharmony_ci <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> 39871cb0ef41Sopenharmony_ci <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> 39881cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 39891cb0ef41Sopenharmony_ci</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> 39901cb0ef41Sopenharmony_ci<div class="api_metadata"> 39911cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 39921cb0ef41Sopenharmony_ci</div> 39931cb0ef41Sopenharmony_ci<ul> 39941cb0ef41Sopenharmony_ci<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> 39951cb0ef41Sopenharmony_ci</ul> 39961cb0ef41Sopenharmony_ci<p>Set the maximum number of idle HTTP parsers.</p></section> 39971cb0ef41Sopenharmony_ci <!-- API END --> 39981cb0ef41Sopenharmony_ci </div> 39991cb0ef41Sopenharmony_ci </div> 40001cb0ef41Sopenharmony_ci </div> 40011cb0ef41Sopenharmony_ci</body> 40021cb0ef41Sopenharmony_ci</html> 4003