1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v18.18.2"> 7 <title>HTTP | Node.js v18.18.2 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/http.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 14</head> 15<body class="alt apidoc" id="api-section-http"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http active">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="http" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.18.2 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 104 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.18.2</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_2"><a href="#http">HTTP</a></span> 124<ul> 125<li><a href="#class-httpagent">Class: <code>http.Agent</code></a> 126<ul> 127<li><a href="#new-agentoptions"><code>new Agent([options])</code></a></li> 128<li><a href="#agentcreateconnectionoptions-callback"><code>agent.createConnection(options[, callback])</code></a></li> 129<li><a href="#agentkeepsocketalivesocket"><code>agent.keepSocketAlive(socket)</code></a></li> 130<li><a href="#agentreusesocketsocket-request"><code>agent.reuseSocket(socket, request)</code></a></li> 131<li><a href="#agentdestroy"><code>agent.destroy()</code></a></li> 132<li><a href="#agentfreesockets"><code>agent.freeSockets</code></a></li> 133<li><a href="#agentgetnameoptions"><code>agent.getName([options])</code></a></li> 134<li><a href="#agentmaxfreesockets"><code>agent.maxFreeSockets</code></a></li> 135<li><a href="#agentmaxsockets"><code>agent.maxSockets</code></a></li> 136<li><a href="#agentmaxtotalsockets"><code>agent.maxTotalSockets</code></a></li> 137<li><a href="#agentrequests"><code>agent.requests</code></a></li> 138<li><a href="#agentsockets"><code>agent.sockets</code></a></li> 139</ul> 140</li> 141<li><a href="#class-httpclientrequest">Class: <code>http.ClientRequest</code></a> 142<ul> 143<li><span class="stability_0"><a href="#event-abort">Event: <code>'abort'</code></a></span></li> 144<li><a href="#event-close">Event: <code>'close'</code></a></li> 145<li><a href="#event-connect">Event: <code>'connect'</code></a></li> 146<li><a href="#event-continue">Event: <code>'continue'</code></a></li> 147<li><a href="#event-finish">Event: <code>'finish'</code></a></li> 148<li><a href="#event-information">Event: <code>'information'</code></a></li> 149<li><a href="#event-response">Event: <code>'response'</code></a></li> 150<li><a href="#event-socket">Event: <code>'socket'</code></a></li> 151<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li> 152<li><a href="#event-upgrade">Event: <code>'upgrade'</code></a></li> 153<li><span class="stability_0"><a href="#requestabort"><code>request.abort()</code></a></span></li> 154<li><span class="stability_0"><a href="#requestaborted"><code>request.aborted</code></a></span></li> 155<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li> 156<li><a href="#requestcork"><code>request.cork()</code></a></li> 157<li><a href="#requestenddata-encoding-callback"><code>request.end([data[, encoding]][, callback])</code></a></li> 158<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a> 159<ul> 160<li><a href="#requestdestroyed"><code>request.destroyed</code></a></li> 161</ul> 162</li> 163<li><span class="stability_0"><a href="#requestfinished"><code>request.finished</code></a></span></li> 164<li><a href="#requestflushheaders"><code>request.flushHeaders()</code></a></li> 165<li><a href="#requestgetheadername"><code>request.getHeader(name)</code></a></li> 166<li><a href="#requestgetheadernames"><code>request.getHeaderNames()</code></a></li> 167<li><a href="#requestgetheaders"><code>request.getHeaders()</code></a></li> 168<li><a href="#requestgetrawheadernames"><code>request.getRawHeaderNames()</code></a></li> 169<li><a href="#requesthasheadername"><code>request.hasHeader(name)</code></a></li> 170<li><a href="#requestmaxheaderscount"><code>request.maxHeadersCount</code></a></li> 171<li><a href="#requestpath"><code>request.path</code></a></li> 172<li><a href="#requestmethod"><code>request.method</code></a></li> 173<li><a href="#requesthost"><code>request.host</code></a></li> 174<li><a href="#requestprotocol"><code>request.protocol</code></a></li> 175<li><a href="#requestremoveheadername"><code>request.removeHeader(name)</code></a></li> 176<li><a href="#requestreusedsocket"><code>request.reusedSocket</code></a></li> 177<li><a href="#requestsetheadername-value"><code>request.setHeader(name, value)</code></a></li> 178<li><a href="#requestsetnodelaynodelay"><code>request.setNoDelay([noDelay])</code></a></li> 179<li><a href="#requestsetsocketkeepaliveenable-initialdelay"><code>request.setSocketKeepAlive([enable][, initialDelay])</code></a></li> 180<li><a href="#requestsettimeouttimeout-callback"><code>request.setTimeout(timeout[, callback])</code></a></li> 181<li><a href="#requestsocket"><code>request.socket</code></a></li> 182<li><a href="#requestuncork"><code>request.uncork()</code></a></li> 183<li><a href="#requestwritableended"><code>request.writableEnded</code></a></li> 184<li><a href="#requestwritablefinished"><code>request.writableFinished</code></a></li> 185<li><a href="#requestwritechunk-encoding-callback"><code>request.write(chunk[, encoding][, callback])</code></a></li> 186</ul> 187</li> 188<li><a href="#class-httpserver">Class: <code>http.Server</code></a> 189<ul> 190<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li> 191<li><a href="#event-checkexpectation">Event: <code>'checkExpectation'</code></a></li> 192<li><a href="#event-clienterror">Event: <code>'clientError'</code></a></li> 193<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 194<li><a href="#event-connect_1">Event: <code>'connect'</code></a></li> 195<li><a href="#event-connection">Event: <code>'connection'</code></a></li> 196<li><a href="#event-droprequest">Event: <code>'dropRequest'</code></a></li> 197<li><a href="#event-request">Event: <code>'request'</code></a></li> 198<li><a href="#event-upgrade_1">Event: <code>'upgrade'</code></a></li> 199<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li> 200<li><a href="#servercloseallconnections"><code>server.closeAllConnections()</code></a></li> 201<li><a href="#servercloseidleconnections"><code>server.closeIdleConnections()</code></a></li> 202<li><a href="#serverheaderstimeout"><code>server.headersTimeout</code></a></li> 203<li><a href="#serverlisten"><code>server.listen()</code></a></li> 204<li><a href="#serverlistening"><code>server.listening</code></a></li> 205<li><a href="#servermaxheaderscount"><code>server.maxHeadersCount</code></a></li> 206<li><a href="#serverrequesttimeout"><code>server.requestTimeout</code></a></li> 207<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li> 208<li><a href="#servermaxrequestspersocket"><code>server.maxRequestsPerSocket</code></a></li> 209<li><a href="#servertimeout"><code>server.timeout</code></a></li> 210<li><a href="#serverkeepalivetimeout"><code>server.keepAliveTimeout</code></a></li> 211</ul> 212</li> 213<li><a href="#class-httpserverresponse">Class: <code>http.ServerResponse</code></a> 214<ul> 215<li><a href="#event-close_2">Event: <code>'close'</code></a></li> 216<li><a href="#event-finish_1">Event: <code>'finish'</code></a></li> 217<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li> 218<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li> 219<li><a href="#responsecork"><code>response.cork()</code></a></li> 220<li><a href="#responseenddata-encoding-callback"><code>response.end([data[, encoding]][, callback])</code></a></li> 221<li><span class="stability_0"><a href="#responsefinished"><code>response.finished</code></a></span></li> 222<li><a href="#responseflushheaders"><code>response.flushHeaders()</code></a></li> 223<li><a href="#responsegetheadername"><code>response.getHeader(name)</code></a></li> 224<li><a href="#responsegetheadernames"><code>response.getHeaderNames()</code></a></li> 225<li><a href="#responsegetheaders"><code>response.getHeaders()</code></a></li> 226<li><a href="#responsehasheadername"><code>response.hasHeader(name)</code></a></li> 227<li><a href="#responseheaderssent"><code>response.headersSent</code></a></li> 228<li><a href="#responseremoveheadername"><code>response.removeHeader(name)</code></a></li> 229<li><a href="#responsereq"><code>response.req</code></a></li> 230<li><a href="#responsesenddate"><code>response.sendDate</code></a></li> 231<li><a href="#responsesetheadername-value"><code>response.setHeader(name, value)</code></a></li> 232<li><a href="#responsesettimeoutmsecs-callback"><code>response.setTimeout(msecs[, callback])</code></a></li> 233<li><a href="#responsesocket"><code>response.socket</code></a></li> 234<li><a href="#responsestatuscode"><code>response.statusCode</code></a></li> 235<li><a href="#responsestatusmessage"><code>response.statusMessage</code></a></li> 236<li><a href="#responsestrictcontentlength"><code>response.strictContentLength</code></a></li> 237<li><a href="#responseuncork"><code>response.uncork()</code></a></li> 238<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li> 239<li><a href="#responsewritablefinished"><code>response.writableFinished</code></a></li> 240<li><a href="#responsewritechunk-encoding-callback"><code>response.write(chunk[, encoding][, callback])</code></a></li> 241<li><a href="#responsewritecontinue"><code>response.writeContinue()</code></a></li> 242<li><a href="#responsewriteearlyhintshints-callback"><code>response.writeEarlyHints(hints[, callback])</code></a></li> 243<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li> 244<li><a href="#responsewriteprocessing"><code>response.writeProcessing()</code></a></li> 245</ul> 246</li> 247<li><a href="#class-httpincomingmessage">Class: <code>http.IncomingMessage</code></a> 248<ul> 249<li><span class="stability_0"><a href="#event-aborted">Event: <code>'aborted'</code></a></span></li> 250<li><a href="#event-close_3">Event: <code>'close'</code></a></li> 251<li><span class="stability_0"><a href="#messageaborted"><code>message.aborted</code></a></span></li> 252<li><a href="#messagecomplete"><code>message.complete</code></a></li> 253<li><span class="stability_0"><a href="#messageconnection"><code>message.connection</code></a></span></li> 254<li><a href="#messagedestroyerror"><code>message.destroy([error])</code></a></li> 255<li><a href="#messageheaders"><code>message.headers</code></a></li> 256<li><a href="#messageheadersdistinct"><code>message.headersDistinct</code></a></li> 257<li><a href="#messagehttpversion"><code>message.httpVersion</code></a></li> 258<li><a href="#messagemethod"><code>message.method</code></a></li> 259<li><a href="#messagerawheaders"><code>message.rawHeaders</code></a></li> 260<li><a href="#messagerawtrailers"><code>message.rawTrailers</code></a></li> 261<li><a href="#messagesettimeoutmsecs-callback"><code>message.setTimeout(msecs[, callback])</code></a></li> 262<li><a href="#messagesocket"><code>message.socket</code></a></li> 263<li><a href="#messagestatuscode"><code>message.statusCode</code></a></li> 264<li><a href="#messagestatusmessage"><code>message.statusMessage</code></a></li> 265<li><a href="#messagetrailers"><code>message.trailers</code></a></li> 266<li><a href="#messagetrailersdistinct"><code>message.trailersDistinct</code></a></li> 267<li><a href="#messageurl"><code>message.url</code></a></li> 268</ul> 269</li> 270<li><a href="#class-httpoutgoingmessage">Class: <code>http.OutgoingMessage</code></a> 271<ul> 272<li><a href="#event-drain">Event: <code>'drain'</code></a></li> 273<li><a href="#event-finish_2">Event: <code>'finish'</code></a></li> 274<li><a href="#event-prefinish">Event: <code>'prefinish'</code></a></li> 275<li><a href="#outgoingmessageaddtrailersheaders"><code>outgoingMessage.addTrailers(headers)</code></a></li> 276<li><a href="#outgoingmessageappendheadername-value"><code>outgoingMessage.appendHeader(name, value)</code></a></li> 277<li><span class="stability_0"><a href="#outgoingmessageconnection"><code>outgoingMessage.connection</code></a></span></li> 278<li><a href="#outgoingmessagecork"><code>outgoingMessage.cork()</code></a></li> 279<li><a href="#outgoingmessagedestroyerror"><code>outgoingMessage.destroy([error])</code></a></li> 280<li><a href="#outgoingmessageendchunk-encoding-callback"><code>outgoingMessage.end(chunk[, encoding][, callback])</code></a></li> 281<li><a href="#outgoingmessageflushheaders"><code>outgoingMessage.flushHeaders()</code></a></li> 282<li><a href="#outgoingmessagegetheadername"><code>outgoingMessage.getHeader(name)</code></a></li> 283<li><a href="#outgoingmessagegetheadernames"><code>outgoingMessage.getHeaderNames()</code></a></li> 284<li><a href="#outgoingmessagegetheaders"><code>outgoingMessage.getHeaders()</code></a></li> 285<li><a href="#outgoingmessagehasheadername"><code>outgoingMessage.hasHeader(name)</code></a></li> 286<li><a href="#outgoingmessageheaderssent"><code>outgoingMessage.headersSent</code></a></li> 287<li><a href="#outgoingmessagepipe"><code>outgoingMessage.pipe()</code></a></li> 288<li><a href="#outgoingmessageremoveheadername"><code>outgoingMessage.removeHeader(name)</code></a></li> 289<li><a href="#outgoingmessagesetheadername-value"><code>outgoingMessage.setHeader(name, value)</code></a></li> 290<li><a href="#outgoingmessagesetheadersheaders"><code>outgoingMessage.setHeaders(headers)</code></a></li> 291<li><a href="#outgoingmessagesettimeoutmsesc-callback"><code>outgoingMessage.setTimeout(msesc[, callback])</code></a></li> 292<li><a href="#outgoingmessagesocket"><code>outgoingMessage.socket</code></a></li> 293<li><a href="#outgoingmessageuncork"><code>outgoingMessage.uncork()</code></a></li> 294<li><a href="#outgoingmessagewritablecorked"><code>outgoingMessage.writableCorked</code></a></li> 295<li><a href="#outgoingmessagewritableended"><code>outgoingMessage.writableEnded</code></a></li> 296<li><a href="#outgoingmessagewritablefinished"><code>outgoingMessage.writableFinished</code></a></li> 297<li><a href="#outgoingmessagewritablehighwatermark"><code>outgoingMessage.writableHighWaterMark</code></a></li> 298<li><a href="#outgoingmessagewritablelength"><code>outgoingMessage.writableLength</code></a></li> 299<li><a href="#outgoingmessagewritableobjectmode"><code>outgoingMessage.writableObjectMode</code></a></li> 300<li><a href="#outgoingmessagewritechunk-encoding-callback"><code>outgoingMessage.write(chunk[, encoding][, callback])</code></a></li> 301</ul> 302</li> 303<li><a href="#httpmethods"><code>http.METHODS</code></a></li> 304<li><a href="#httpstatus_codes"><code>http.STATUS_CODES</code></a></li> 305<li><a href="#httpcreateserveroptions-requestlistener"><code>http.createServer([options][, requestListener])</code></a></li> 306<li><a href="#httpgetoptions-callback"><code>http.get(options[, callback])</code></a></li> 307<li><a href="#httpgeturl-options-callback"><code>http.get(url[, options][, callback])</code></a></li> 308<li><a href="#httpglobalagent"><code>http.globalAgent</code></a></li> 309<li><a href="#httpmaxheadersize"><code>http.maxHeaderSize</code></a></li> 310<li><a href="#httprequestoptions-callback"><code>http.request(options[, callback])</code></a></li> 311<li><a href="#httprequesturl-options-callback"><code>http.request(url[, options][, callback])</code></a></li> 312<li><a href="#httpvalidateheadernamename-label"><code>http.validateHeaderName(name[, label])</code></a></li> 313<li><a href="#httpvalidateheadervaluename-value"><code>http.validateHeaderValue(name, value)</code></a></li> 314<li><a href="#httpsetmaxidlehttpparsersmax"><code>http.setMaxIdleHTTPParsers(max)</code></a></li> 315</ul> 316</li> 317</ul></div></div> 318 </li> 319 320 321 <li class="picker-header"> 322 <a href="#"> 323 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 324 Index 325 </a> 326 327 <div class="picker"><ul> 328<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 329<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 330 331 <li> 332 <a href="index.html">Index</a> 333 </li> 334 </ul> 335 336<hr class="line"> 337<ul> 338<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 339<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 340<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 341<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 342<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 343<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 344<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 345<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 346<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 347<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 348<li><a href="console.html" class="nav-console">Console</a></li> 349<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 350<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 351<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 352<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 353<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 354<li><a href="dns.html" class="nav-dns">DNS</a></li> 355<li><a href="domain.html" class="nav-domain">Domain</a></li> 356<li><a href="errors.html" class="nav-errors">Errors</a></li> 357<li><a href="events.html" class="nav-events">Events</a></li> 358<li><a href="fs.html" class="nav-fs">File system</a></li> 359<li><a href="globals.html" class="nav-globals">Globals</a></li> 360<li><a href="http.html" class="nav-http active">HTTP</a></li> 361<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 362<li><a href="https.html" class="nav-https">HTTPS</a></li> 363<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 364<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 365<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 366<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 367<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 368<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 369<li><a href="net.html" class="nav-net">Net</a></li> 370<li><a href="os.html" class="nav-os">OS</a></li> 371<li><a href="path.html" class="nav-path">Path</a></li> 372<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 373<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 374<li><a href="process.html" class="nav-process">Process</a></li> 375<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 376<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 377<li><a href="readline.html" class="nav-readline">Readline</a></li> 378<li><a href="repl.html" class="nav-repl">REPL</a></li> 379<li><a href="report.html" class="nav-report">Report</a></li> 380<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 381<li><a href="stream.html" class="nav-stream">Stream</a></li> 382<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 383<li><a href="test.html" class="nav-test">Test runner</a></li> 384<li><a href="timers.html" class="nav-timers">Timers</a></li> 385<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 386<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 387<li><a href="tty.html" class="nav-tty">TTY</a></li> 388<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 389<li><a href="url.html" class="nav-url">URL</a></li> 390<li><a href="util.html" class="nav-util">Utilities</a></li> 391<li><a href="v8.html" class="nav-v8">V8</a></li> 392<li><a href="vm.html" class="nav-vm">VM</a></li> 393<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 394<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 395<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 396<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 397<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 398</ul> 399<hr class="line"> 400<ul> 401<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 402</ul></div> 403 </li> 404 405 406 <li class="picker-header"> 407 <a href="#"> 408 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 409 Other versions 410 </a> 411 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/http.html">20.x</a></li> 412<li><a href="https://nodejs.org/docs/latest-v19.x/api/http.html">19.x</a></li> 413<li><a href="https://nodejs.org/docs/latest-v18.x/api/http.html">18.x <b>LTS</b></a></li> 414<li><a href="https://nodejs.org/docs/latest-v17.x/api/http.html">17.x</a></li> 415<li><a href="https://nodejs.org/docs/latest-v16.x/api/http.html">16.x</a></li> 416<li><a href="https://nodejs.org/docs/latest-v15.x/api/http.html">15.x</a></li> 417<li><a href="https://nodejs.org/docs/latest-v14.x/api/http.html">14.x</a></li> 418<li><a href="https://nodejs.org/docs/latest-v13.x/api/http.html">13.x</a></li> 419<li><a href="https://nodejs.org/docs/latest-v12.x/api/http.html">12.x</a></li> 420<li><a href="https://nodejs.org/docs/latest-v11.x/api/http.html">11.x</a></li> 421<li><a href="https://nodejs.org/docs/latest-v10.x/api/http.html">10.x</a></li> 422<li><a href="https://nodejs.org/docs/latest-v9.x/api/http.html">9.x</a></li> 423<li><a href="https://nodejs.org/docs/latest-v8.x/api/http.html">8.x</a></li> 424<li><a href="https://nodejs.org/docs/latest-v7.x/api/http.html">7.x</a></li> 425<li><a href="https://nodejs.org/docs/latest-v6.x/api/http.html">6.x</a></li> 426<li><a href="https://nodejs.org/docs/latest-v5.x/api/http.html">5.x</a></li> 427<li><a href="https://nodejs.org/docs/latest-v4.x/api/http.html">4.x</a></li> 428<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/http.html">0.12.x</a></li> 429<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/http.html">0.10.x</a></li></ol></div> 430 </li> 431 432 <li class="picker-header"> 433 <a href="#"> 434 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 435 Options 436 </a> 437 438 <div class="picker"> 439 <ul> 440 <li> 441 <a href="all.html">View on single page</a> 442 </li> 443 <li> 444 <a href="http.json">View as JSON</a> 445 </li> 446 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/http.md">Edit on GitHub</a></li> 447 </ul> 448 </div> 449 </li> 450 </ul> 451 </div> 452 <hr> 453 </header> 454 455 <details id="toc" open><summary>Table of contents</summary><ul> 456<li><span class="stability_2"><a href="#http">HTTP</a></span> 457<ul> 458<li><a href="#class-httpagent">Class: <code>http.Agent</code></a> 459<ul> 460<li><a href="#new-agentoptions"><code>new Agent([options])</code></a></li> 461<li><a href="#agentcreateconnectionoptions-callback"><code>agent.createConnection(options[, callback])</code></a></li> 462<li><a href="#agentkeepsocketalivesocket"><code>agent.keepSocketAlive(socket)</code></a></li> 463<li><a href="#agentreusesocketsocket-request"><code>agent.reuseSocket(socket, request)</code></a></li> 464<li><a href="#agentdestroy"><code>agent.destroy()</code></a></li> 465<li><a href="#agentfreesockets"><code>agent.freeSockets</code></a></li> 466<li><a href="#agentgetnameoptions"><code>agent.getName([options])</code></a></li> 467<li><a href="#agentmaxfreesockets"><code>agent.maxFreeSockets</code></a></li> 468<li><a href="#agentmaxsockets"><code>agent.maxSockets</code></a></li> 469<li><a href="#agentmaxtotalsockets"><code>agent.maxTotalSockets</code></a></li> 470<li><a href="#agentrequests"><code>agent.requests</code></a></li> 471<li><a href="#agentsockets"><code>agent.sockets</code></a></li> 472</ul> 473</li> 474<li><a href="#class-httpclientrequest">Class: <code>http.ClientRequest</code></a> 475<ul> 476<li><span class="stability_0"><a href="#event-abort">Event: <code>'abort'</code></a></span></li> 477<li><a href="#event-close">Event: <code>'close'</code></a></li> 478<li><a href="#event-connect">Event: <code>'connect'</code></a></li> 479<li><a href="#event-continue">Event: <code>'continue'</code></a></li> 480<li><a href="#event-finish">Event: <code>'finish'</code></a></li> 481<li><a href="#event-information">Event: <code>'information'</code></a></li> 482<li><a href="#event-response">Event: <code>'response'</code></a></li> 483<li><a href="#event-socket">Event: <code>'socket'</code></a></li> 484<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li> 485<li><a href="#event-upgrade">Event: <code>'upgrade'</code></a></li> 486<li><span class="stability_0"><a href="#requestabort"><code>request.abort()</code></a></span></li> 487<li><span class="stability_0"><a href="#requestaborted"><code>request.aborted</code></a></span></li> 488<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li> 489<li><a href="#requestcork"><code>request.cork()</code></a></li> 490<li><a href="#requestenddata-encoding-callback"><code>request.end([data[, encoding]][, callback])</code></a></li> 491<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a> 492<ul> 493<li><a href="#requestdestroyed"><code>request.destroyed</code></a></li> 494</ul> 495</li> 496<li><span class="stability_0"><a href="#requestfinished"><code>request.finished</code></a></span></li> 497<li><a href="#requestflushheaders"><code>request.flushHeaders()</code></a></li> 498<li><a href="#requestgetheadername"><code>request.getHeader(name)</code></a></li> 499<li><a href="#requestgetheadernames"><code>request.getHeaderNames()</code></a></li> 500<li><a href="#requestgetheaders"><code>request.getHeaders()</code></a></li> 501<li><a href="#requestgetrawheadernames"><code>request.getRawHeaderNames()</code></a></li> 502<li><a href="#requesthasheadername"><code>request.hasHeader(name)</code></a></li> 503<li><a href="#requestmaxheaderscount"><code>request.maxHeadersCount</code></a></li> 504<li><a href="#requestpath"><code>request.path</code></a></li> 505<li><a href="#requestmethod"><code>request.method</code></a></li> 506<li><a href="#requesthost"><code>request.host</code></a></li> 507<li><a href="#requestprotocol"><code>request.protocol</code></a></li> 508<li><a href="#requestremoveheadername"><code>request.removeHeader(name)</code></a></li> 509<li><a href="#requestreusedsocket"><code>request.reusedSocket</code></a></li> 510<li><a href="#requestsetheadername-value"><code>request.setHeader(name, value)</code></a></li> 511<li><a href="#requestsetnodelaynodelay"><code>request.setNoDelay([noDelay])</code></a></li> 512<li><a href="#requestsetsocketkeepaliveenable-initialdelay"><code>request.setSocketKeepAlive([enable][, initialDelay])</code></a></li> 513<li><a href="#requestsettimeouttimeout-callback"><code>request.setTimeout(timeout[, callback])</code></a></li> 514<li><a href="#requestsocket"><code>request.socket</code></a></li> 515<li><a href="#requestuncork"><code>request.uncork()</code></a></li> 516<li><a href="#requestwritableended"><code>request.writableEnded</code></a></li> 517<li><a href="#requestwritablefinished"><code>request.writableFinished</code></a></li> 518<li><a href="#requestwritechunk-encoding-callback"><code>request.write(chunk[, encoding][, callback])</code></a></li> 519</ul> 520</li> 521<li><a href="#class-httpserver">Class: <code>http.Server</code></a> 522<ul> 523<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li> 524<li><a href="#event-checkexpectation">Event: <code>'checkExpectation'</code></a></li> 525<li><a href="#event-clienterror">Event: <code>'clientError'</code></a></li> 526<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 527<li><a href="#event-connect_1">Event: <code>'connect'</code></a></li> 528<li><a href="#event-connection">Event: <code>'connection'</code></a></li> 529<li><a href="#event-droprequest">Event: <code>'dropRequest'</code></a></li> 530<li><a href="#event-request">Event: <code>'request'</code></a></li> 531<li><a href="#event-upgrade_1">Event: <code>'upgrade'</code></a></li> 532<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li> 533<li><a href="#servercloseallconnections"><code>server.closeAllConnections()</code></a></li> 534<li><a href="#servercloseidleconnections"><code>server.closeIdleConnections()</code></a></li> 535<li><a href="#serverheaderstimeout"><code>server.headersTimeout</code></a></li> 536<li><a href="#serverlisten"><code>server.listen()</code></a></li> 537<li><a href="#serverlistening"><code>server.listening</code></a></li> 538<li><a href="#servermaxheaderscount"><code>server.maxHeadersCount</code></a></li> 539<li><a href="#serverrequesttimeout"><code>server.requestTimeout</code></a></li> 540<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li> 541<li><a href="#servermaxrequestspersocket"><code>server.maxRequestsPerSocket</code></a></li> 542<li><a href="#servertimeout"><code>server.timeout</code></a></li> 543<li><a href="#serverkeepalivetimeout"><code>server.keepAliveTimeout</code></a></li> 544</ul> 545</li> 546<li><a href="#class-httpserverresponse">Class: <code>http.ServerResponse</code></a> 547<ul> 548<li><a href="#event-close_2">Event: <code>'close'</code></a></li> 549<li><a href="#event-finish_1">Event: <code>'finish'</code></a></li> 550<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li> 551<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li> 552<li><a href="#responsecork"><code>response.cork()</code></a></li> 553<li><a href="#responseenddata-encoding-callback"><code>response.end([data[, encoding]][, callback])</code></a></li> 554<li><span class="stability_0"><a href="#responsefinished"><code>response.finished</code></a></span></li> 555<li><a href="#responseflushheaders"><code>response.flushHeaders()</code></a></li> 556<li><a href="#responsegetheadername"><code>response.getHeader(name)</code></a></li> 557<li><a href="#responsegetheadernames"><code>response.getHeaderNames()</code></a></li> 558<li><a href="#responsegetheaders"><code>response.getHeaders()</code></a></li> 559<li><a href="#responsehasheadername"><code>response.hasHeader(name)</code></a></li> 560<li><a href="#responseheaderssent"><code>response.headersSent</code></a></li> 561<li><a href="#responseremoveheadername"><code>response.removeHeader(name)</code></a></li> 562<li><a href="#responsereq"><code>response.req</code></a></li> 563<li><a href="#responsesenddate"><code>response.sendDate</code></a></li> 564<li><a href="#responsesetheadername-value"><code>response.setHeader(name, value)</code></a></li> 565<li><a href="#responsesettimeoutmsecs-callback"><code>response.setTimeout(msecs[, callback])</code></a></li> 566<li><a href="#responsesocket"><code>response.socket</code></a></li> 567<li><a href="#responsestatuscode"><code>response.statusCode</code></a></li> 568<li><a href="#responsestatusmessage"><code>response.statusMessage</code></a></li> 569<li><a href="#responsestrictcontentlength"><code>response.strictContentLength</code></a></li> 570<li><a href="#responseuncork"><code>response.uncork()</code></a></li> 571<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li> 572<li><a href="#responsewritablefinished"><code>response.writableFinished</code></a></li> 573<li><a href="#responsewritechunk-encoding-callback"><code>response.write(chunk[, encoding][, callback])</code></a></li> 574<li><a href="#responsewritecontinue"><code>response.writeContinue()</code></a></li> 575<li><a href="#responsewriteearlyhintshints-callback"><code>response.writeEarlyHints(hints[, callback])</code></a></li> 576<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li> 577<li><a href="#responsewriteprocessing"><code>response.writeProcessing()</code></a></li> 578</ul> 579</li> 580<li><a href="#class-httpincomingmessage">Class: <code>http.IncomingMessage</code></a> 581<ul> 582<li><span class="stability_0"><a href="#event-aborted">Event: <code>'aborted'</code></a></span></li> 583<li><a href="#event-close_3">Event: <code>'close'</code></a></li> 584<li><span class="stability_0"><a href="#messageaborted"><code>message.aborted</code></a></span></li> 585<li><a href="#messagecomplete"><code>message.complete</code></a></li> 586<li><span class="stability_0"><a href="#messageconnection"><code>message.connection</code></a></span></li> 587<li><a href="#messagedestroyerror"><code>message.destroy([error])</code></a></li> 588<li><a href="#messageheaders"><code>message.headers</code></a></li> 589<li><a href="#messageheadersdistinct"><code>message.headersDistinct</code></a></li> 590<li><a href="#messagehttpversion"><code>message.httpVersion</code></a></li> 591<li><a href="#messagemethod"><code>message.method</code></a></li> 592<li><a href="#messagerawheaders"><code>message.rawHeaders</code></a></li> 593<li><a href="#messagerawtrailers"><code>message.rawTrailers</code></a></li> 594<li><a href="#messagesettimeoutmsecs-callback"><code>message.setTimeout(msecs[, callback])</code></a></li> 595<li><a href="#messagesocket"><code>message.socket</code></a></li> 596<li><a href="#messagestatuscode"><code>message.statusCode</code></a></li> 597<li><a href="#messagestatusmessage"><code>message.statusMessage</code></a></li> 598<li><a href="#messagetrailers"><code>message.trailers</code></a></li> 599<li><a href="#messagetrailersdistinct"><code>message.trailersDistinct</code></a></li> 600<li><a href="#messageurl"><code>message.url</code></a></li> 601</ul> 602</li> 603<li><a href="#class-httpoutgoingmessage">Class: <code>http.OutgoingMessage</code></a> 604<ul> 605<li><a href="#event-drain">Event: <code>'drain'</code></a></li> 606<li><a href="#event-finish_2">Event: <code>'finish'</code></a></li> 607<li><a href="#event-prefinish">Event: <code>'prefinish'</code></a></li> 608<li><a href="#outgoingmessageaddtrailersheaders"><code>outgoingMessage.addTrailers(headers)</code></a></li> 609<li><a href="#outgoingmessageappendheadername-value"><code>outgoingMessage.appendHeader(name, value)</code></a></li> 610<li><span class="stability_0"><a href="#outgoingmessageconnection"><code>outgoingMessage.connection</code></a></span></li> 611<li><a href="#outgoingmessagecork"><code>outgoingMessage.cork()</code></a></li> 612<li><a href="#outgoingmessagedestroyerror"><code>outgoingMessage.destroy([error])</code></a></li> 613<li><a href="#outgoingmessageendchunk-encoding-callback"><code>outgoingMessage.end(chunk[, encoding][, callback])</code></a></li> 614<li><a href="#outgoingmessageflushheaders"><code>outgoingMessage.flushHeaders()</code></a></li> 615<li><a href="#outgoingmessagegetheadername"><code>outgoingMessage.getHeader(name)</code></a></li> 616<li><a href="#outgoingmessagegetheadernames"><code>outgoingMessage.getHeaderNames()</code></a></li> 617<li><a href="#outgoingmessagegetheaders"><code>outgoingMessage.getHeaders()</code></a></li> 618<li><a href="#outgoingmessagehasheadername"><code>outgoingMessage.hasHeader(name)</code></a></li> 619<li><a href="#outgoingmessageheaderssent"><code>outgoingMessage.headersSent</code></a></li> 620<li><a href="#outgoingmessagepipe"><code>outgoingMessage.pipe()</code></a></li> 621<li><a href="#outgoingmessageremoveheadername"><code>outgoingMessage.removeHeader(name)</code></a></li> 622<li><a href="#outgoingmessagesetheadername-value"><code>outgoingMessage.setHeader(name, value)</code></a></li> 623<li><a href="#outgoingmessagesetheadersheaders"><code>outgoingMessage.setHeaders(headers)</code></a></li> 624<li><a href="#outgoingmessagesettimeoutmsesc-callback"><code>outgoingMessage.setTimeout(msesc[, callback])</code></a></li> 625<li><a href="#outgoingmessagesocket"><code>outgoingMessage.socket</code></a></li> 626<li><a href="#outgoingmessageuncork"><code>outgoingMessage.uncork()</code></a></li> 627<li><a href="#outgoingmessagewritablecorked"><code>outgoingMessage.writableCorked</code></a></li> 628<li><a href="#outgoingmessagewritableended"><code>outgoingMessage.writableEnded</code></a></li> 629<li><a href="#outgoingmessagewritablefinished"><code>outgoingMessage.writableFinished</code></a></li> 630<li><a href="#outgoingmessagewritablehighwatermark"><code>outgoingMessage.writableHighWaterMark</code></a></li> 631<li><a href="#outgoingmessagewritablelength"><code>outgoingMessage.writableLength</code></a></li> 632<li><a href="#outgoingmessagewritableobjectmode"><code>outgoingMessage.writableObjectMode</code></a></li> 633<li><a href="#outgoingmessagewritechunk-encoding-callback"><code>outgoingMessage.write(chunk[, encoding][, callback])</code></a></li> 634</ul> 635</li> 636<li><a href="#httpmethods"><code>http.METHODS</code></a></li> 637<li><a href="#httpstatus_codes"><code>http.STATUS_CODES</code></a></li> 638<li><a href="#httpcreateserveroptions-requestlistener"><code>http.createServer([options][, requestListener])</code></a></li> 639<li><a href="#httpgetoptions-callback"><code>http.get(options[, callback])</code></a></li> 640<li><a href="#httpgeturl-options-callback"><code>http.get(url[, options][, callback])</code></a></li> 641<li><a href="#httpglobalagent"><code>http.globalAgent</code></a></li> 642<li><a href="#httpmaxheadersize"><code>http.maxHeaderSize</code></a></li> 643<li><a href="#httprequestoptions-callback"><code>http.request(options[, callback])</code></a></li> 644<li><a href="#httprequesturl-options-callback"><code>http.request(url[, options][, callback])</code></a></li> 645<li><a href="#httpvalidateheadernamename-label"><code>http.validateHeaderName(name[, label])</code></a></li> 646<li><a href="#httpvalidateheadervaluename-value"><code>http.validateHeaderValue(name, value)</code></a></li> 647<li><a href="#httpsetmaxidlehttpparsersmax"><code>http.setMaxIdleHTTPParsers(max)</code></a></li> 648</ul> 649</li> 650</ul></details> 651 652 <div id="apicontent"> 653 <h2>HTTP<span><a class="mark" href="#http" id="http">#</a></span><a aria-hidden="true" class="legacy" id="http_http"></a></h2> 654 655<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 656<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/http.js">lib/http.js</a></p> 657<p>To use the HTTP server and client one must <code>require('node:http')</code>.</p> 658<p>The HTTP interfaces in Node.js are designed to support many features 659of the protocol which have been traditionally difficult to use. 660In particular, large, possibly chunk-encoded, messages. The interface is 661careful to never buffer entire requests or responses, so the 662user is able to stream data.</p> 663<p>HTTP message headers are represented by an object like this:</p> 664<!-- eslint-skip --> 665<pre><code class="language-js">{ <span class="hljs-string">'content-length'</span>: <span class="hljs-string">'123'</span>, 666 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain'</span>, 667 <span class="hljs-string">'connection'</span>: <span class="hljs-string">'keep-alive'</span>, 668 <span class="hljs-string">'host'</span>: <span class="hljs-string">'example.com'</span>, 669 <span class="hljs-string">'accept'</span>: <span class="hljs-string">'*/*'</span> }</code> <button class="copy-button">copy</button></pre> 670<p>Keys are lowercased. Values are not modified.</p> 671<p>In order to support the full spectrum of possible HTTP applications, the Node.js 672HTTP API is very low-level. It deals with stream handling and message 673parsing only. It parses a message into headers and body but it does not 674parse the actual headers or the body.</p> 675<p>See <a href="#messageheaders"><code>message.headers</code></a> for details on how duplicate headers are handled.</p> 676<p>The raw headers as they were received are retained in the <code>rawHeaders</code> 677property, which is an array of <code>[key, value, key2, value2, ...]</code>. For 678example, the previous message header object might have a <code>rawHeaders</code> 679list like the following:</p> 680<!-- eslint-disable semi --> 681<pre><code class="language-js">[ <span class="hljs-string">'ConTent-Length'</span>, <span class="hljs-string">'123456'</span>, 682 <span class="hljs-string">'content-LENGTH'</span>, <span class="hljs-string">'123'</span>, 683 <span class="hljs-string">'content-type'</span>, <span class="hljs-string">'text/plain'</span>, 684 <span class="hljs-string">'CONNECTION'</span>, <span class="hljs-string">'keep-alive'</span>, 685 <span class="hljs-string">'Host'</span>, <span class="hljs-string">'example.com'</span>, 686 <span class="hljs-string">'accepT'</span>, <span class="hljs-string">'*/*'</span> ]</code> <button class="copy-button">copy</button></pre> 687<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> 688<div class="api_metadata"> 689<span>Added in: v0.3.4</span> 690</div> 691<p>An <code>Agent</code> is responsible for managing connection persistence 692and reuse for HTTP clients. It maintains a queue of pending requests 693for a given host and port, reusing a single socket connection for each 694until the queue is empty, at which time the socket is either destroyed 695or put into a pool where it is kept to be used again for requests to the 696same host and port. Whether it is destroyed or pooled depends on the 697<code>keepAlive</code> <a href="#new-agentoptions">option</a>.</p> 698<p>Pooled connections have TCP Keep-Alive enabled for them, but servers may 699still close idle connections, in which case they will be removed from the 700pool and a new connection will be made when a new HTTP request is made for 701that host and port. Servers may also refuse to allow multiple requests 702over the same connection, in which case the connection will have to be 703remade for every request and cannot be pooled. The <code>Agent</code> will still make 704the requests to that server, but each one will occur over a new connection.</p> 705<p>When a connection is closed by the client or the server, it is removed 706from the pool. Any unused sockets in the pool will be unrefed so as not 707to keep the Node.js process running when there are no outstanding requests. 708(see <a href="net.html#socketunref"><code>socket.unref()</code></a>).</p> 709<p>It is good practice, to <a href="#agentdestroy"><code>destroy()</code></a> an <code>Agent</code> instance when it is no 710longer in use, because unused sockets consume OS resources.</p> 711<p>Sockets are removed from an agent when the socket emits either 712a <code>'close'</code> event or an <code>'agentRemove'</code> event. When intending to keep one 713HTTP request open for a long time without keeping it in the agent, something 714like the following may be done:</p> 715<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> { 716 <span class="hljs-comment">// Do stuff</span> 717}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'socket'</span>, <span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { 718 socket.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'agentRemove'</span>); 719});</code> <button class="copy-button">copy</button></pre> 720<p>An agent may also be used for an individual request. By providing 721<code>{agent: false}</code> as an option to the <code>http.get()</code> or <code>http.request()</code> 722functions, a one-time use <code>Agent</code> with default options will be used 723for the client connection.</p> 724<p><code>agent:false</code>:</p> 725<pre><code class="language-js">http.<span class="hljs-title function_">get</span>({ 726 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'localhost'</span>, 727 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>, 728 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>, 729 <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> 730}, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 731 <span class="hljs-comment">// Do stuff with response</span> 732});</code> <button class="copy-button">copy</button></pre> 733<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> 734<div class="api_metadata"> 735<details class="changelog"><summary>History</summary> 736<table> 737<tbody><tr><th>Version</th><th>Changes</th></tr> 738<tr><td>v15.6.0, v14.17.0</td> 739<td><p>Change the default scheduling from 'fifo' to 'lifo'.</p></td></tr> 740<tr><td>v14.5.0, v12.20.0</td> 741<td><p>Add <code>scheduling</code> option to specify the free socket scheduling strategy.</p></td></tr> 742<tr><td>v14.5.0, v12.19.0</td> 743<td><p>Add <code>maxTotalSockets</code> option to agent constructor.</p></td></tr> 744<tr><td>v0.3.4</td> 745<td><p><span>Added in: v0.3.4</span></p></td></tr> 746</tbody></table> 747</details> 748</div> 749<ul> 750<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. 751Can have the following fields: 752<ul> 753<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 754outstanding requests, so they can be used for future requests without 755having to reestablish a TCP connection. Not to be confused with the 756<code>keep-alive</code> value of the <code>Connection</code> header. The <code>Connection: keep-alive</code> 757header is always sent when using an agent except when the <code>Connection</code> 758header is explicitly specified or when the <code>keepAlive</code> and <code>maxSockets</code> 759options are respectively set to <code>false</code> and <code>Infinity</code>, in which case 760<code>Connection: close</code> will be used. <strong>Default:</strong> <code>false</code>.</li> 761<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 762the <a href="net.html#socketsetkeepaliveenable-initialdelay">initial delay</a> 763for TCP Keep-Alive packets. Ignored when the 764<code>keepAlive</code> option is <code>false</code> or <code>undefined</code>. <strong>Default:</strong> <code>1000</code>.</li> 765<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. 766If the same host opens multiple concurrent connections, each request 767will use new socket until the <code>maxSockets</code> value is reached. 768If the host attempts to open more connections than <code>maxSockets</code>, 769the additional requests will enter into a pending request queue, and 770will enter active connection state when an existing connection terminates. 771This makes sure there are at most <code>maxSockets</code> active connections at 772any point in time, from a given host. 773<strong>Default:</strong> <code>Infinity</code>.</li> 774<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 775all hosts in total. Each request will use a new socket 776until the maximum is reached. 777<strong>Default:</strong> <code>Infinity</code>.</li> 778<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 779in a free state. Only relevant if <code>keepAlive</code> is set to <code>true</code>. 780<strong>Default:</strong> <code>256</code>.</li> 781<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 782the next free socket to use. It can be <code>'fifo'</code> or <code>'lifo'</code>. 783The main difference between the two scheduling strategies is that <code>'lifo'</code> 784selects the most recently used socket, while <code>'fifo'</code> selects 785the least recently used socket. 786In case of a low rate of request per second, the <code>'lifo'</code> scheduling 787will lower the risk of picking a socket that might have been closed 788by the server due to inactivity. 789In case of a high rate of request per second, 790the <code>'fifo'</code> scheduling will maximize the number of open sockets, 791while the <code>'lifo'</code> scheduling will keep it as low as possible. 792<strong>Default:</strong> <code>'lifo'</code>.</li> 793<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. 794This will set the timeout when the socket is created.</li> 795</ul> 796</li> 797</ul> 798<p><code>options</code> in <a href="net.html#socketconnectoptions-connectlistener"><code>socket.connect()</code></a> are also supported.</p> 799<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 800of these values set to their respective defaults.</p> 801<p>To configure any of them, a custom <a href="#class-httpagent"><code>http.Agent</code></a> instance must be created.</p> 802<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 803<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> }); 804options.<span class="hljs-property">agent</span> = keepAliveAgent; 805http.<span class="hljs-title function_">request</span>(options, onResponseCallback);</code> <button class="copy-button">copy</button></pre> 806<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> 807<div class="api_metadata"> 808<span>Added in: v0.11.4</span> 809</div> 810<ul> 811<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 812<a href="net.html#netcreateconnectionoptions-connectlistener"><code>net.createConnection()</code></a> for the format of the options</li> 813<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> 814<li>Returns: <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 815</ul> 816<p>Produces a socket/stream to be used for HTTP requests.</p> 817<p>By default, this function is the same as <a href="net.html#netcreateconnectionoptions-connectlistener"><code>net.createConnection()</code></a>. However, 818custom agents may override this method in case greater flexibility is desired.</p> 819<p>A socket/stream can be supplied in one of two ways: by returning the 820socket/stream from this function, or by passing the socket/stream to <code>callback</code>.</p> 821<p>This method is guaranteed to return an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 822a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 823type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 824<p><code>callback</code> has a signature of <code>(err, stream)</code>.</p> 825<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> 826<div class="api_metadata"> 827<span>Added in: v8.1.0</span> 828</div> 829<ul> 830<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 831</ul> 832<p>Called when <code>socket</code> is detached from a request and could be persisted by the 833<code>Agent</code>. Default behavior is to:</p> 834<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>); 835socket.<span class="hljs-title function_">unref</span>(); 836<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;</code> <button class="copy-button">copy</button></pre> 837<p>This method can be overridden by a particular <code>Agent</code> subclass. If this 838method returns a falsy value, the socket will be destroyed instead of persisting 839it for use with the next request.</p> 840<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 841<a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>.</p> 842<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> 843<div class="api_metadata"> 844<span>Added in: v8.1.0</span> 845</div> 846<ul> 847<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 848<li><code>request</code> <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 849</ul> 850<p>Called when <code>socket</code> is attached to <code>request</code> after being persisted because of 851the keep-alive options. Default behavior is to:</p> 852<pre><code class="language-js">socket.<span class="hljs-title function_">ref</span>();</code> <button class="copy-button">copy</button></pre> 853<p>This method can be overridden by a particular <code>Agent</code> subclass.</p> 854<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 855<a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>.</p> 856<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> 857<div class="api_metadata"> 858<span>Added in: v0.11.4</span> 859</div> 860<p>Destroy any sockets that are currently in use by the agent.</p> 861<p>It is usually not necessary to do this. However, if using an 862agent with <code>keepAlive</code> enabled, then it is best to explicitly shut down 863the agent when it is no longer needed. Otherwise, 864sockets might stay open for quite a long time before the server 865terminates them.</p> 866<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> 867<div class="api_metadata"> 868<details class="changelog"><summary>History</summary> 869<table> 870<tbody><tr><th>Version</th><th>Changes</th></tr> 871<tr><td>v16.0.0</td> 872<td><p>The property now has a <code>null</code> prototype.</p></td></tr> 873<tr><td>v0.11.4</td> 874<td><p><span>Added in: v0.11.4</span></p></td></tr> 875</tbody></table> 876</details> 877</div> 878<ul> 879<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 880</ul> 881<p>An object which contains arrays of sockets currently awaiting use by 882the agent when <code>keepAlive</code> is enabled. Do not modify.</p> 883<p>Sockets in the <code>freeSockets</code> list will be automatically destroyed and 884removed from the array on <code>'timeout'</code>.</p> 885<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> 886<div class="api_metadata"> 887<details class="changelog"><summary>History</summary> 888<table> 889<tbody><tr><th>Version</th><th>Changes</th></tr> 890<tr><td>v17.7.0</td> 891<td><p>The <code>options</code> parameter is now optional.</p></td></tr> 892<tr><td>v0.11.4</td> 893<td><p><span>Added in: v0.11.4</span></p></td></tr> 894</tbody></table> 895</details> 896</div> 897<ul> 898<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 899<ul> 900<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 901request to</li> 902<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> 903<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 904when issuing the request</li> 905<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> 906</ul> 907</li> 908<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 909</ul> 910<p>Get a unique name for a set of request options, to determine whether a 911connection can be reused. For an HTTP agent, this returns 912<code>host:port:localAddress</code> or <code>host:port:localAddress:family</code>. For an HTTPS agent, 913the name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options 914that determine socket reusability.</p> 915<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> 916<div class="api_metadata"> 917<span>Added in: v0.11.7</span> 918</div> 919<ul> 920<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 921</ul> 922<p>By default set to 256. For agents with <code>keepAlive</code> enabled, this 923sets the maximum number of sockets that will be left open in the free 924state.</p> 925<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> 926<div class="api_metadata"> 927<span>Added in: v0.3.6</span> 928</div> 929<ul> 930<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 931</ul> 932<p>By default set to <code>Infinity</code>. Determines how many concurrent sockets the agent 933can have open per origin. Origin is the returned value of <a href="#agentgetnameoptions"><code>agent.getName()</code></a>.</p> 934<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> 935<div class="api_metadata"> 936<span>Added in: v14.5.0, v12.19.0</span> 937</div> 938<ul> 939<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 940</ul> 941<p>By default set to <code>Infinity</code>. Determines how many concurrent sockets the agent 942can have open. Unlike <code>maxSockets</code>, this parameter applies across all origins.</p> 943<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> 944<div class="api_metadata"> 945<details class="changelog"><summary>History</summary> 946<table> 947<tbody><tr><th>Version</th><th>Changes</th></tr> 948<tr><td>v16.0.0</td> 949<td><p>The property now has a <code>null</code> prototype.</p></td></tr> 950<tr><td>v0.5.9</td> 951<td><p><span>Added in: v0.5.9</span></p></td></tr> 952</tbody></table> 953</details> 954</div> 955<ul> 956<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 957</ul> 958<p>An object which contains queues of requests that have not yet been assigned to 959sockets. Do not modify.</p> 960<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> 961<div class="api_metadata"> 962<details class="changelog"><summary>History</summary> 963<table> 964<tbody><tr><th>Version</th><th>Changes</th></tr> 965<tr><td>v16.0.0</td> 966<td><p>The property now has a <code>null</code> prototype.</p></td></tr> 967<tr><td>v0.3.6</td> 968<td><p><span>Added in: v0.3.6</span></p></td></tr> 969</tbody></table> 970</details> 971</div> 972<ul> 973<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 974</ul> 975<p>An object which contains arrays of sockets currently in use by the 976agent. Do not modify.</p> 977</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> 978<div class="api_metadata"> 979<span>Added in: v0.1.17</span> 980</div> 981<ul> 982<li>Extends: <a href="http.html#class-httpoutgoingmessage" class="type"><http.OutgoingMessage></a></li> 983</ul> 984<p>This object is created internally and returned from <a href="#httprequestoptions-callback"><code>http.request()</code></a>. It 985represents an <em>in-progress</em> request whose header has already been queued. The 986header is still mutable using the <a href="#requestsetheadername-value"><code>setHeader(name, value)</code></a>, 987<a href="#requestgetheadername"><code>getHeader(name)</code></a>, <a href="#requestremoveheadername"><code>removeHeader(name)</code></a> API. The actual header will 988be sent along with the first data chunk or when calling <a href="#requestenddata-encoding-callback"><code>request.end()</code></a>.</p> 989<p>To get the response, add a listener for <a href="#event-response"><code>'response'</code></a> to the request object. 990<a href="#event-response"><code>'response'</code></a> will be emitted from the request object when the response 991headers have been received. The <a href="#event-response"><code>'response'</code></a> event is executed with one 992argument which is an instance of <a href="#class-httpincomingmessage"><code>http.IncomingMessage</code></a>.</p> 993<p>During the <a href="#event-response"><code>'response'</code></a> event, one can add listeners to the 994response object; particularly to listen for the <code>'data'</code> event.</p> 995<p>If no <a href="#event-response"><code>'response'</code></a> handler is added, then the response will be 996entirely discarded. However, if a <a href="#event-response"><code>'response'</code></a> event handler is added, 997then the data from the response object <strong>must</strong> be consumed, either by 998calling <code>response.read()</code> whenever there is a <code>'readable'</code> event, or 999by adding a <code>'data'</code> handler, or by calling the <code>.resume()</code> method. 1000Until the data is consumed, the <code>'end'</code> event will not fire. Also, until 1001the data is read it will consume memory that can eventually lead to a 1002'process out of memory' error.</p> 1003<p>For backward compatibility, <code>res</code> will only emit <code>'error'</code> if there is an 1004<code>'error'</code> listener registered.</p> 1005<p>Set <code>Content-Length</code> header to limit the response body size. 1006If <a href="#responsestrictcontentlength"><code>response.strictContentLength</code></a> is set to <code>true</code>, mismatching the 1007<code>Content-Length</code> header value will result in an <code>Error</code> being thrown, 1008identified by <code>code:</code> <a href="errors.html#err_http_content_length_mismatch"><code>'ERR_HTTP_CONTENT_LENGTH_MISMATCH'</code></a>.</p> 1009<p><code>Content-Length</code> value should be in bytes, not characters. Use 1010<a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength()</code></a> to determine the length of the body in bytes.</p> 1011<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> 1012<div class="api_metadata"> 1013<span>Added in: v1.4.1</span><span>Deprecated since: v17.0.0, v16.12.0</span> 1014</div> 1015<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> 1016<p>Emitted when the request has been aborted by the client. This event is only 1017emitted on the first call to <code>abort()</code>.</p> 1018<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> 1019<div class="api_metadata"> 1020<span>Added in: v0.5.4</span> 1021</div> 1022<p>Indicates that the request is completed, or its underlying connection was 1023terminated prematurely (before the response completion).</p> 1024<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> 1025<div class="api_metadata"> 1026<span>Added in: v0.7.0</span> 1027</div> 1028<ul> 1029<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 1030<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 1031<li><code>head</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 1032</ul> 1033<p>Emitted each time a server responds to a request with a <code>CONNECT</code> method. If 1034this event is not being listened for, clients receiving a <code>CONNECT</code> method will 1035have their connections closed.</p> 1036<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 1037a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 1038type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 1039<p>A client and server pair demonstrating how to listen for the <code>'connect'</code> event:</p> 1040<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 1041<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>); 1042<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>); 1043 1044<span class="hljs-comment">// Create an HTTP tunneling proxy</span> 1045<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> { 1046 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> }); 1047 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>); 1048}); 1049proxy.<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> { 1050 <span class="hljs-comment">// Connect to an origin server</span> 1051 <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>); 1052 <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> { 1053 clientSocket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 200 Connection Established\r\n'</span> + 1054 <span class="hljs-string">'Proxy-agent: Node.js-Proxy\r\n'</span> + 1055 <span class="hljs-string">'\r\n'</span>); 1056 serverSocket.<span class="hljs-title function_">write</span>(head); 1057 serverSocket.<span class="hljs-title function_">pipe</span>(clientSocket); 1058 clientSocket.<span class="hljs-title function_">pipe</span>(serverSocket); 1059 }); 1060}); 1061 1062<span class="hljs-comment">// Now that proxy is running</span> 1063proxy.<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> { 1064 1065 <span class="hljs-comment">// Make a request to a tunneling proxy</span> 1066 <span class="hljs-keyword">const</span> options = { 1067 <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>, 1068 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 1069 <span class="hljs-attr">method</span>: <span class="hljs-string">'CONNECT'</span>, 1070 <span class="hljs-attr">path</span>: <span class="hljs-string">'www.google.com:80'</span>, 1071 }; 1072 1073 <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options); 1074 req.<span class="hljs-title function_">end</span>(); 1075 1076 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> { 1077 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got connected!'</span>); 1078 1079 <span class="hljs-comment">// Make a request over an HTTP tunnel</span> 1080 socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'GET / HTTP/1.1\r\n'</span> + 1081 <span class="hljs-string">'Host: www.google.com:80\r\n'</span> + 1082 <span class="hljs-string">'Connection: close\r\n'</span> + 1083 <span class="hljs-string">'\r\n'</span>); 1084 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> { 1085 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk.<span class="hljs-title function_">toString</span>()); 1086 }); 1087 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 1088 proxy.<span class="hljs-title function_">close</span>(); 1089 }); 1090 }); 1091});</code> <button class="copy-button">copy</button></pre> 1092<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> 1093<div class="api_metadata"> 1094<span>Added in: v0.3.2</span> 1095</div> 1096<p>Emitted when the server sends a '100 Continue' HTTP response, usually because 1097the request contained 'Expect: 100-continue'. This is an instruction that 1098the client should send the request body.</p> 1099<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> 1100<div class="api_metadata"> 1101<span>Added in: v0.3.6</span> 1102</div> 1103<p>Emitted when the request has been sent. More specifically, this event is emitted 1104when the last segment of the response headers and body have been handed off to 1105the operating system for transmission over the network. It does not imply that 1106the server has received anything yet.</p> 1107<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> 1108<div class="api_metadata"> 1109<span>Added in: v10.0.0</span> 1110</div> 1111<ul> 1112<li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1113<ul> 1114<li><code>httpVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1115<li><code>httpVersionMajor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1116<li><code>httpVersionMinor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1117<li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1118<li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1119<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1120<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 1121</ul> 1122</li> 1123</ul> 1124<p>Emitted when the server sends a 1xx intermediate response (excluding 101 1125Upgrade). The listeners of this event will receive an object containing the 1126HTTP version, status code, status message, key-value headers object, 1127and array with the raw header names followed by their respective values.</p> 1128<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 1129 1130<span class="hljs-keyword">const</span> options = { 1131 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 1132 <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>, 1133 <span class="hljs-attr">path</span>: <span class="hljs-string">'/length_request'</span>, 1134}; 1135 1136<span class="hljs-comment">// Make a request</span> 1137<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options); 1138req.<span class="hljs-title function_">end</span>(); 1139 1140req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'information'</span>, <span class="hljs-function">(<span class="hljs-params">info</span>) =></span> { 1141 <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>); 1142});</code> <button class="copy-button">copy</button></pre> 1143<p>101 Upgrade statuses do not fire this event due to their break from the 1144traditional HTTP request/response chain, such as web sockets, in-place TLS 1145upgrades, or HTTP 2.0. To be notified of 101 Upgrade notices, listen for the 1146<a href="#event-upgrade"><code>'upgrade'</code></a> event instead.</p> 1147<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> 1148<div class="api_metadata"> 1149<span>Added in: v0.1.0</span> 1150</div> 1151<ul> 1152<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 1153</ul> 1154<p>Emitted when a response is received to this request. This event is emitted only 1155once.</p> 1156<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> 1157<div class="api_metadata"> 1158<span>Added in: v0.5.3</span> 1159</div> 1160<ul> 1161<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 1162</ul> 1163<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 1164a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 1165type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 1166<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> 1167<div class="api_metadata"> 1168<span>Added in: v0.7.8</span> 1169</div> 1170<p>Emitted when the underlying socket times out from inactivity. This only notifies 1171that the socket has been idle. The request must be destroyed manually.</p> 1172<p>See also: <a href="#requestsettimeouttimeout-callback"><code>request.setTimeout()</code></a>.</p> 1173<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> 1174<div class="api_metadata"> 1175<span>Added in: v0.1.94</span> 1176</div> 1177<ul> 1178<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 1179<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 1180<li><code>head</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 1181</ul> 1182<p>Emitted each time a server responds to a request with an upgrade. If this 1183event is not being listened for and the response status code is 101 Switching 1184Protocols, clients receiving an upgrade header will have their connections 1185closed.</p> 1186<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 1187a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 1188type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 1189<p>A client server pair demonstrating how to listen for the <code>'upgrade'</code> event.</p> 1190<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 1191 1192<span class="hljs-comment">// Create an HTTP server</span> 1193<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> { 1194 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> }); 1195 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>); 1196}); 1197server.<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> { 1198 socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'</span> + 1199 <span class="hljs-string">'Upgrade: WebSocket\r\n'</span> + 1200 <span class="hljs-string">'Connection: Upgrade\r\n'</span> + 1201 <span class="hljs-string">'\r\n'</span>); 1202 1203 socket.<span class="hljs-title function_">pipe</span>(socket); <span class="hljs-comment">// echo back</span> 1204}); 1205 1206<span class="hljs-comment">// Now that server is running</span> 1207server.<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> { 1208 1209 <span class="hljs-comment">// make a request</span> 1210 <span class="hljs-keyword">const</span> options = { 1211 <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>, 1212 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 1213 <span class="hljs-attr">headers</span>: { 1214 <span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Upgrade'</span>, 1215 <span class="hljs-string">'Upgrade'</span>: <span class="hljs-string">'websocket'</span>, 1216 }, 1217 }; 1218 1219 <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options); 1220 req.<span class="hljs-title function_">end</span>(); 1221 1222 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> { 1223 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got upgraded!'</span>); 1224 socket.<span class="hljs-title function_">end</span>(); 1225 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>); 1226 }); 1227});</code> <button class="copy-button">copy</button></pre> 1228<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> 1229<div class="api_metadata"> 1230<span>Added in: v0.3.8</span><span>Deprecated since: v14.1.0, v13.14.0</span> 1231</div> 1232<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> 1233<p>Marks the request as aborting. Calling this will cause remaining data 1234in the response to be dropped and the socket to be destroyed.</p> 1235<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> 1236<div class="api_metadata"> 1237<details class="changelog"><summary>History</summary> 1238<table> 1239<tbody><tr><th>Version</th><th>Changes</th></tr> 1240<tr><td>v17.0.0, v16.12.0</td> 1241<td><p><span>Deprecated since: v17.0.0, v16.12.0</span></p></td></tr> 1242<tr><td>v11.0.0</td> 1243<td><p>The <code>aborted</code> property is no longer a timestamp number.</p></td></tr> 1244<tr><td>v0.11.14</td> 1245<td><p><span>Added in: v0.11.14</span></p></td></tr> 1246</tbody></table> 1247</details> 1248</div> 1249<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> 1250<ul> 1251<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1252</ul> 1253<p>The <code>request.aborted</code> property will be <code>true</code> if the request has 1254been aborted.</p> 1255<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> 1256<div class="api_metadata"> 1257<span>Added in: v0.3.0</span><span>Deprecated since: v13.0.0</span> 1258</div> 1259<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> 1260<ul> 1261<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 1262</ul> 1263<p>See <a href="#requestsocket"><code>request.socket</code></a>.</p> 1264<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> 1265<div class="api_metadata"> 1266<span>Added in: v13.2.0, v12.16.0</span> 1267</div> 1268<p>See <a href="stream.html#writablecork"><code>writable.cork()</code></a>.</p> 1269<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> 1270<div class="api_metadata"> 1271<details class="changelog"><summary>History</summary> 1272<table> 1273<tbody><tr><th>Version</th><th>Changes</th></tr> 1274<tr><td>v15.0.0</td> 1275<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 1276<tr><td>v10.0.0</td> 1277<td><p>This method now returns a reference to <code>ClientRequest</code>.</p></td></tr> 1278<tr><td>v0.1.90</td> 1279<td><p><span>Added in: v0.1.90</span></p></td></tr> 1280</tbody></table> 1281</details> 1282</div> 1283<ul> 1284<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> 1285<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1286<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1287<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 1288</ul> 1289<p>Finishes sending the request. If any parts of the body are 1290unsent, it will flush them to the stream. If the request is 1291chunked, this will send the terminating <code>'0\r\n\r\n'</code>.</p> 1292<p>If <code>data</code> is specified, it is equivalent to calling 1293<a href="#requestwritechunk-encoding-callback"><code>request.write(data, encoding)</code></a> followed by <code>request.end(callback)</code>.</p> 1294<p>If <code>callback</code> is specified, it will be called when the request stream 1295is finished.</p> 1296<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> 1297<div class="api_metadata"> 1298<details class="changelog"><summary>History</summary> 1299<table> 1300<tbody><tr><th>Version</th><th>Changes</th></tr> 1301<tr><td>v14.5.0</td> 1302<td><p>The function returns <code>this</code> for consistency with other Readable streams.</p></td></tr> 1303<tr><td>v0.3.0</td> 1304<td><p><span>Added in: v0.3.0</span></p></td></tr> 1305</tbody></table> 1306</details> 1307</div> 1308<ul> 1309<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> 1310<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 1311</ul> 1312<p>Destroy the request. Optionally emit an <code>'error'</code> event, 1313and emit a <code>'close'</code> event. Calling this will cause remaining data 1314in the response to be dropped and the socket to be destroyed.</p> 1315<p>See <a href="stream.html#writabledestroyerror"><code>writable.destroy()</code></a> for further details.</p> 1316<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> 1317<div class="api_metadata"> 1318<span>Added in: v14.1.0, v13.14.0</span> 1319</div> 1320<ul> 1321<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1322</ul> 1323<p>Is <code>true</code> after <a href="#requestdestroyerror"><code>request.destroy()</code></a> has been called.</p> 1324<p>See <a href="stream.html#writabledestroyed"><code>writable.destroyed</code></a> for further details.</p> 1325<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> 1326<div class="api_metadata"> 1327<span>Added in: v0.0.1</span><span>Deprecated since: v13.4.0, v12.16.0</span> 1328</div> 1329<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> 1330<ul> 1331<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1332</ul> 1333<p>The <code>request.finished</code> property will be <code>true</code> if <a href="#requestenddata-encoding-callback"><code>request.end()</code></a> 1334has been called. <code>request.end()</code> will automatically be called if the 1335request was initiated via <a href="#httpgetoptions-callback"><code>http.get()</code></a>.</p> 1336<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> 1337<div class="api_metadata"> 1338<span>Added in: v1.6.0</span> 1339</div> 1340<p>Flushes the request headers.</p> 1341<p>For efficiency reasons, Node.js normally buffers the request headers until 1342<code>request.end()</code> is called or the first chunk of request data is written. It 1343then tries to pack the request headers and data into a single TCP packet.</p> 1344<p>That's usually desired (it saves a TCP round-trip), but not when the first 1345data is not sent until possibly much later. <code>request.flushHeaders()</code> bypasses 1346the optimization and kickstarts the request.</p> 1347<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> 1348<div class="api_metadata"> 1349<span>Added in: v1.6.0</span> 1350</div> 1351<ul> 1352<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1353<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1354</ul> 1355<p>Reads out a header on the request. The name is case-insensitive. 1356The type of the return value depends on the arguments provided to 1357<a href="#requestsetheadername-value"><code>request.setHeader()</code></a>.</p> 1358<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>); 1359request.<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)); 1360request.<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>]); 1361<span class="hljs-keyword">const</span> contentType = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Type'</span>); 1362<span class="hljs-comment">// 'contentType' is 'text/html'</span> 1363<span class="hljs-keyword">const</span> contentLength = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Length'</span>); 1364<span class="hljs-comment">// 'contentLength' is of type number</span> 1365<span class="hljs-keyword">const</span> cookie = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Cookie'</span>); 1366<span class="hljs-comment">// 'cookie' is of type string[]</span></code> <button class="copy-button">copy</button></pre> 1367<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> 1368<div class="api_metadata"> 1369<span>Added in: v7.7.0</span> 1370</div> 1371<ul> 1372<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 1373</ul> 1374<p>Returns an array containing the unique names of the current outgoing headers. 1375All header names are lowercase.</p> 1376<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>); 1377request.<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>]); 1378 1379<span class="hljs-keyword">const</span> headerNames = request.<span class="hljs-title function_">getHeaderNames</span>(); 1380<span class="hljs-comment">// headerNames === ['foo', 'cookie']</span></code> <button class="copy-button">copy</button></pre> 1381<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> 1382<div class="api_metadata"> 1383<span>Added in: v7.7.0</span> 1384</div> 1385<ul> 1386<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1387</ul> 1388<p>Returns a shallow copy of the current outgoing headers. Since a shallow copy 1389is used, array values may be mutated without additional calls to various 1390header-related http module methods. The keys of the returned object are the 1391header names and the values are the respective header values. All header names 1392are lowercase.</p> 1393<p>The object returned by the <code>request.getHeaders()</code> method <em>does not</em> 1394prototypically inherit from the JavaScript <code>Object</code>. This means that typical 1395<code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, and others 1396are not defined and <em>will not work</em>.</p> 1397<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>); 1398request.<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>]); 1399 1400<span class="hljs-keyword">const</span> headers = request.<span class="hljs-title function_">getHeaders</span>(); 1401<span class="hljs-comment">// headers === { foo: 'bar', 'cookie': ['foo=bar', 'bar=baz'] }</span></code> <button class="copy-button">copy</button></pre> 1402<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> 1403<div class="api_metadata"> 1404<span>Added in: v15.13.0, v14.17.0</span> 1405</div> 1406<ul> 1407<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 1408</ul> 1409<p>Returns an array containing the unique names of the current outgoing raw 1410headers. Header names are returned with their exact casing being set.</p> 1411<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>); 1412request.<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>]); 1413 1414<span class="hljs-keyword">const</span> headerNames = request.<span class="hljs-title function_">getRawHeaderNames</span>(); 1415<span class="hljs-comment">// headerNames === ['Foo', 'Set-Cookie']</span></code> <button class="copy-button">copy</button></pre> 1416<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> 1417<div class="api_metadata"> 1418<span>Added in: v7.7.0</span> 1419</div> 1420<ul> 1421<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1422<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1423</ul> 1424<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the 1425outgoing headers. The header name matching is case-insensitive.</p> 1426<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> 1427<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> 1428<ul> 1429<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> 1430</ul> 1431<p>Limits maximum response headers count. If set to 0, no limit will be applied.</p> 1432<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> 1433<div class="api_metadata"> 1434<span>Added in: v0.4.0</span> 1435</div> 1436<ul> 1437<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request path.</li> 1438</ul> 1439<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> 1440<div class="api_metadata"> 1441<span>Added in: v0.1.97</span> 1442</div> 1443<ul> 1444<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request method.</li> 1445</ul> 1446<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> 1447<div class="api_metadata"> 1448<span>Added in: v14.5.0, v12.19.0</span> 1449</div> 1450<ul> 1451<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request host.</li> 1452</ul> 1453<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> 1454<div class="api_metadata"> 1455<span>Added in: v14.5.0, v12.19.0</span> 1456</div> 1457<ul> 1458<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The request protocol.</li> 1459</ul> 1460<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> 1461<div class="api_metadata"> 1462<span>Added in: v1.6.0</span> 1463</div> 1464<ul> 1465<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1466</ul> 1467<p>Removes a header that's already defined into headers object.</p> 1468<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> 1469<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> 1470<div class="api_metadata"> 1471<span>Added in: v13.0.0, v12.16.0</span> 1472</div> 1473<ul> 1474<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> 1475</ul> 1476<p>When sending request through a keep-alive enabled agent, the underlying socket 1477might be reused. But if server closes connection at unfortunate time, client 1478may run into a 'ECONNRESET' error.</p> 1479<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 1480 1481<span class="hljs-comment">// Server has a 5 seconds keep-alive timeout by default</span> 1482http 1483 .<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 1484 res.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\n'</span>); 1485 res.<span class="hljs-title function_">end</span>(); 1486 }) 1487 .<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>); 1488 1489<span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> { 1490 <span class="hljs-comment">// Adapting a keep-alive agent</span> 1491 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> { 1492 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> { 1493 <span class="hljs-comment">// Do nothing</span> 1494 }); 1495 }); 1496}, <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> 1497<p>By marking a request whether it reused socket or not, we can do 1498automatic error retry base on it.</p> 1499<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 1500<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> }); 1501 1502<span class="hljs-keyword">function</span> <span class="hljs-title function_">retriableRequest</span>(<span class="hljs-params"></span>) { 1503 <span class="hljs-keyword">const</span> req = http 1504 .<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> { 1505 <span class="hljs-comment">// ...</span> 1506 }) 1507 .<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 1508 <span class="hljs-comment">// Check if retry is needed</span> 1509 <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>) { 1510 <span class="hljs-title function_">retriableRequest</span>(); 1511 } 1512 }); 1513} 1514 1515<span class="hljs-title function_">retriableRequest</span>();</code> <button class="copy-button">copy</button></pre> 1516<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> 1517<div class="api_metadata"> 1518<span>Added in: v1.6.0</span> 1519</div> 1520<ul> 1521<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1522<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1523</ul> 1524<p>Sets a single header value for headers object. If this header already exists in 1525the to-be-sent headers, its value will be replaced. Use an array of strings 1526here to send multiple headers with the same name. Non-string values will be 1527stored without modification. Therefore, <a href="#requestgetheadername"><code>request.getHeader()</code></a> may return 1528non-string values. However, the non-string values will be converted to strings 1529for network transmission.</p> 1530<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> 1531<p>or</p> 1532<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> 1533<p>When the value is a string an exception will be thrown if it contains 1534characters outside the <code>latin1</code> encoding.</p> 1535<p>If you need to pass UTF-8 characters in the value please encode the value 1536using the <a href="https://www.rfc-editor.org/rfc/rfc8187.txt">RFC 8187</a> standard.</p> 1537<pre><code class="language-js"><span class="hljs-keyword">const</span> filename = <span class="hljs-string">'Rock .txt'</span>; 1538request.<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> 1539<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> 1540<div class="api_metadata"> 1541<span>Added in: v0.5.9</span> 1542</div> 1543<ul> 1544<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1545</ul> 1546<p>Once a socket is assigned to this request and is connected 1547<a href="net.html#socketsetnodelaynodelay"><code>socket.setNoDelay()</code></a> will be called.</p> 1548<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> 1549<div class="api_metadata"> 1550<span>Added in: v0.5.9</span> 1551</div> 1552<ul> 1553<li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1554<li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1555</ul> 1556<p>Once a socket is assigned to this request and is connected 1557<a href="net.html#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive()</code></a> will be called.</p> 1558<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> 1559<div class="api_metadata"> 1560<details class="changelog"><summary>History</summary> 1561<table> 1562<tbody><tr><th>Version</th><th>Changes</th></tr> 1563<tr><td>v9.0.0</td> 1564<td><p>Consistently set socket timeout only when the socket connects.</p></td></tr> 1565<tr><td>v0.5.9</td> 1566<td><p><span>Added in: v0.5.9</span></p></td></tr> 1567</tbody></table> 1568</details> 1569</div> 1570<ul> 1571<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> 1572<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. 1573Same as binding to the <code>'timeout'</code> event.</li> 1574<li>Returns: <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 1575</ul> 1576<p>Once a socket is assigned to this request and is connected 1577<a href="net.html#socketsettimeouttimeout-callback"><code>socket.setTimeout()</code></a> will be called.</p> 1578<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> 1579<div class="api_metadata"> 1580<span>Added in: v0.3.0</span> 1581</div> 1582<ul> 1583<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 1584</ul> 1585<p>Reference to the underlying socket. Usually users will not want to access 1586this property. In particular, the socket will not emit <code>'readable'</code> events 1587because of how the protocol parser attaches to the socket.</p> 1588<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 1589<span class="hljs-keyword">const</span> options = { 1590 <span class="hljs-attr">host</span>: <span class="hljs-string">'www.google.com'</span>, 1591}; 1592<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">get</span>(options); 1593req.<span class="hljs-title function_">end</span>(); 1594req.<span class="hljs-title function_">once</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 1595 <span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localAddress</span>; 1596 <span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localPort</span>; 1597 <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>); 1598 <span class="hljs-comment">// Consume response object</span> 1599});</code> <button class="copy-button">copy</button></pre> 1600<p>This property is guaranteed to be an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 1601a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specified a socket 1602type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 1603<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> 1604<div class="api_metadata"> 1605<span>Added in: v13.2.0, v12.16.0</span> 1606</div> 1607<p>See <a href="stream.html#writableuncork"><code>writable.uncork()</code></a>.</p> 1608<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> 1609<div class="api_metadata"> 1610<span>Added in: v12.9.0</span> 1611</div> 1612<ul> 1613<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1614</ul> 1615<p>Is <code>true</code> after <a href="#requestenddata-encoding-callback"><code>request.end()</code></a> has been called. This property 1616does not indicate whether the data has been flushed, for this use 1617<a href="#requestwritablefinished"><code>request.writableFinished</code></a> instead.</p> 1618<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> 1619<div class="api_metadata"> 1620<span>Added in: v12.7.0</span> 1621</div> 1622<ul> 1623<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1624</ul> 1625<p>Is <code>true</code> if all data has been flushed to the underlying system, immediately 1626before the <a href="#event-finish"><code>'finish'</code></a> event is emitted.</p> 1627<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> 1628<div class="api_metadata"> 1629<details class="changelog"><summary>History</summary> 1630<table> 1631<tbody><tr><th>Version</th><th>Changes</th></tr> 1632<tr><td>v15.0.0</td> 1633<td><p>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 1634<tr><td>v0.1.29</td> 1635<td><p><span>Added in: v0.1.29</span></p></td></tr> 1636</tbody></table> 1637</details> 1638</div> 1639<ul> 1640<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> 1641<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1642<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1643<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1644</ul> 1645<p>Sends a chunk of the body. This method can be called multiple times. If no 1646<code>Content-Length</code> is set, data will automatically be encoded in HTTP Chunked 1647transfer encoding, so that server knows when the data ends. The 1648<code>Transfer-Encoding: chunked</code> header is added. Calling <a href="#requestenddata-encoding-callback"><code>request.end()</code></a> 1649is necessary to finish sending the request.</p> 1650<p>The <code>encoding</code> argument is optional and only applies when <code>chunk</code> is a string. 1651Defaults to <code>'utf8'</code>.</p> 1652<p>The <code>callback</code> argument is optional and will be called when this chunk of data 1653is flushed, but only if the chunk is non-empty.</p> 1654<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel 1655buffer. Returns <code>false</code> if all or part of the data was queued in user memory. 1656<code>'drain'</code> will be emitted when the buffer is free again.</p> 1657<p>When <code>write</code> function is called with empty string or buffer, it does 1658nothing and waits for more input.</p> 1659</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> 1660<div class="api_metadata"> 1661<span>Added in: v0.1.17</span> 1662</div> 1663<ul> 1664<li>Extends: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 1665</ul> 1666<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> 1667<div class="api_metadata"> 1668<span>Added in: v0.3.0</span> 1669</div> 1670<ul> 1671<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 1672<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 1673</ul> 1674<p>Emitted each time a request with an HTTP <code>Expect: 100-continue</code> is received. 1675If this event is not listened for, the server will automatically respond 1676with a <code>100 Continue</code> as appropriate.</p> 1677<p>Handling this event involves calling <a href="#responsewritecontinue"><code>response.writeContinue()</code></a> if the 1678client should continue to send the request body, or generating an appropriate 1679HTTP response (e.g. 400 Bad Request) if the client should not continue to send 1680the request body.</p> 1681<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will 1682not be emitted.</p> 1683<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> 1684<div class="api_metadata"> 1685<span>Added in: v5.5.0</span> 1686</div> 1687<ul> 1688<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 1689<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 1690</ul> 1691<p>Emitted each time a request with an HTTP <code>Expect</code> header is received, where the 1692value is not <code>100-continue</code>. If this event is not listened for, the server will 1693automatically respond with a <code>417 Expectation Failed</code> as appropriate.</p> 1694<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will 1695not be emitted.</p> 1696<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> 1697<div class="api_metadata"> 1698<details class="changelog"><summary>History</summary> 1699<table> 1700<tbody><tr><th>Version</th><th>Changes</th></tr> 1701<tr><td>v12.0.0</td> 1702<td><p>The default behavior will return a 431 Request Header Fields Too Large if a HPE_HEADER_OVERFLOW error occurs.</p></td></tr> 1703<tr><td>v9.4.0</td> 1704<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> 1705<tr><td>v6.0.0</td> 1706<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> 1707<tr><td>v0.1.94</td> 1708<td><p><span>Added in: v0.1.94</span></p></td></tr> 1709</tbody></table> 1710</details> 1711</div> 1712<ul> 1713<li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 1714<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 1715</ul> 1716<p>If a client connection emits an <code>'error'</code> event, it will be forwarded here. 1717Listener of this event is responsible for closing/destroying the underlying 1718socket. For example, one may wish to more gracefully close the socket with a 1719custom HTTP response instead of abruptly severing the connection. The socket 1720<strong>must be closed or destroyed</strong> before the listener ends.</p> 1721<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 1722a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 1723type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 1724<p>Default behavior is to try close the socket with a HTTP '400 Bad Request', 1725or a HTTP '431 Request Header Fields Too Large' in the case of a 1726<a href="errors.html#hpe_header_overflow"><code>HPE_HEADER_OVERFLOW</code></a> error. If the socket is not writable or headers 1727of the current attached <a href="#class-httpserverresponse"><code>http.ServerResponse</code></a> has been sent, it is 1728immediately destroyed.</p> 1729<p><code>socket</code> is the <a href="net.html#class-netsocket"><code>net.Socket</code></a> object that the error originated from.</p> 1730<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 1731 1732<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> { 1733 res.<span class="hljs-title function_">end</span>(); 1734}); 1735server.<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> { 1736 socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</span>); 1737}); 1738server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre> 1739<p>When the <code>'clientError'</code> event occurs, there is no <code>request</code> or <code>response</code> 1740object, so any HTTP response sent, including response headers and payload, 1741<em>must</em> be written directly to the <code>socket</code> object. Care must be taken to 1742ensure the response is a properly formatted HTTP response message.</p> 1743<p><code>err</code> is an instance of <code>Error</code> with two extra columns:</p> 1744<ul> 1745<li><code>bytesParsed</code>: the bytes count of request packet that Node.js may have parsed 1746correctly;</li> 1747<li><code>rawPacket</code>: the raw packet of current request.</li> 1748</ul> 1749<p>In some cases, the client has already received the response and/or the socket 1750has already been destroyed, like in case of <code>ECONNRESET</code> errors. Before 1751trying to send data to the socket, it is better to check that it is still 1752writable.</p> 1753<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> { 1754 <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>) { 1755 <span class="hljs-keyword">return</span>; 1756 } 1757 1758 socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</span>); 1759});</code> <button class="copy-button">copy</button></pre> 1760<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> 1761<div class="api_metadata"> 1762<span>Added in: v0.1.4</span> 1763</div> 1764<p>Emitted when the server closes.</p> 1765<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> 1766<div class="api_metadata"> 1767<span>Added in: v0.7.0</span> 1768</div> 1769<ul> 1770<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Arguments for the HTTP request, as it is in 1771the <a href="#event-request"><code>'request'</code></a> event</li> 1772<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a> Network socket between the server and client</li> 1773<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> 1774</ul> 1775<p>Emitted each time a client requests an HTTP <code>CONNECT</code> method. If this event is 1776not listened for, then clients requesting a <code>CONNECT</code> method will have their 1777connections closed.</p> 1778<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 1779a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 1780type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 1781<p>After this event is emitted, the request's socket will not have a <code>'data'</code> 1782event listener, meaning it will need to be bound in order to handle data 1783sent to the server on that socket.</p> 1784<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> 1785<div class="api_metadata"> 1786<span>Added in: v0.1.0</span> 1787</div> 1788<ul> 1789<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 1790</ul> 1791<p>This event is emitted when a new TCP stream is established. <code>socket</code> is 1792typically an object of type <a href="net.html#class-netsocket"><code>net.Socket</code></a>. Usually users will not want to 1793access this event. In particular, the socket will not emit <code>'readable'</code> events 1794because of how the protocol parser attaches to the socket. The <code>socket</code> can 1795also be accessed at <code>request.socket</code>.</p> 1796<p>This event can also be explicitly emitted by users to inject connections 1797into the HTTP server. In that case, any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream can be passed.</p> 1798<p>If <code>socket.setTimeout()</code> is called here, the timeout will be replaced with 1799<code>server.keepAliveTimeout</code> when the socket has served a request (if 1800<code>server.keepAliveTimeout</code> is non-zero).</p> 1801<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 1802a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 1803type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 1804<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> 1805<div class="api_metadata"> 1806<span>Added in: v18.7.0</span> 1807</div> 1808<ul> 1809<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Arguments for the HTTP request, as it is in 1810the <a href="#event-request"><code>'request'</code></a> event</li> 1811<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a> Network socket between the server and client</li> 1812</ul> 1813<p>When the number of requests on a socket reaches the threshold of 1814<code>server.maxRequestsPerSocket</code>, the server will drop new requests 1815and emit <code>'dropRequest'</code> event instead, then send <code>503</code> to client.</p> 1816<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> 1817<div class="api_metadata"> 1818<span>Added in: v0.1.0</span> 1819</div> 1820<ul> 1821<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 1822<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 1823</ul> 1824<p>Emitted each time there is a request. There may be multiple requests 1825per connection (in the case of HTTP Keep-Alive connections).</p> 1826<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> 1827<div class="api_metadata"> 1828<details class="changelog"><summary>History</summary> 1829<table> 1830<tbody><tr><th>Version</th><th>Changes</th></tr> 1831<tr><td>v10.0.0</td> 1832<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> 1833<tr><td>v0.1.94</td> 1834<td><p><span>Added in: v0.1.94</span></p></td></tr> 1835</tbody></table> 1836</details> 1837</div> 1838<ul> 1839<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Arguments for the HTTP request, as it is in 1840the <a href="#event-request"><code>'request'</code></a> event</li> 1841<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a> Network socket between the server and client</li> 1842<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> 1843</ul> 1844<p>Emitted each time a client requests an HTTP upgrade. Listening to this event 1845is optional and clients cannot insist on a protocol change.</p> 1846<p>After this event is emitted, the request's socket will not have a <code>'data'</code> 1847event listener, meaning it will need to be bound in order to handle data 1848sent to the server on that socket.</p> 1849<p>This event is guaranteed to be passed an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 1850a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specifies a socket 1851type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 1852<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> 1853<div class="api_metadata"> 1854<span>Added in: v0.1.90</span> 1855</div> 1856<ul> 1857<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1858</ul> 1859<p>Stops the server from accepting new connections. See <a href="net.html#serverclosecallback"><code>net.Server.close()</code></a>.</p> 1860<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> 1861<div class="api_metadata"> 1862<span>Added in: v18.2.0</span> 1863</div> 1864<p>Closes all connections connected to this server.</p> 1865<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> 1866<div class="api_metadata"> 1867<span>Added in: v18.2.0</span> 1868</div> 1869<p>Closes all connections connected to this server which are not sending a request 1870or waiting for a response.</p> 1871<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> 1872<div class="api_metadata"> 1873<details class="changelog"><summary>History</summary> 1874<table> 1875<tbody><tr><th>Version</th><th>Changes</th></tr> 1876<tr><td>v18.14.0</td> 1877<td><p>The default is now set to the minimum between 60000 (60 seconds) or <code>requestTimeout</code>.</p></td></tr> 1878<tr><td>v11.3.0, v10.14.0</td> 1879<td><p><span>Added in: v11.3.0, v10.14.0</span></p></td></tr> 1880</tbody></table> 1881</details> 1882</div> 1883<ul> 1884<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> 1885</ul> 1886<p>Limit the amount of time the parser will wait to receive the complete HTTP 1887headers.</p> 1888<p>If the timeout expires, the server responds with status 408 without 1889forwarding the request to the request listener and then closes the connection.</p> 1890<p>It must be set to a non-zero value (e.g. 120 seconds) to protect against 1891potential Denial-of-Service attacks in case the server is deployed without a 1892reverse proxy in front.</p> 1893<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> 1894<p>Starts the HTTP server listening for connections. 1895This 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> 1896<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> 1897<div class="api_metadata"> 1898<span>Added in: v5.7.0</span> 1899</div> 1900<ul> 1901<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> 1902</ul> 1903<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> 1904<div class="api_metadata"> 1905<span>Added in: v0.7.0</span> 1906</div> 1907<ul> 1908<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> 1909</ul> 1910<p>Limits maximum incoming headers count. If set to 0, no limit will be applied.</p> 1911<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> 1912<div class="api_metadata"> 1913<details class="changelog"><summary>History</summary> 1914<table> 1915<tbody><tr><th>Version</th><th>Changes</th></tr> 1916<tr><td>v18.0.0</td> 1917<td><p>The default request timeout changed from no timeout to 300s (5 minutes).</p></td></tr> 1918<tr><td>v14.11.0</td> 1919<td><p><span>Added in: v14.11.0</span></p></td></tr> 1920</tbody></table> 1921</details> 1922</div> 1923<ul> 1924<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> 1925</ul> 1926<p>Sets the timeout value in milliseconds for receiving the entire request from 1927the client.</p> 1928<p>If the timeout expires, the server responds with status 408 without 1929forwarding the request to the request listener and then closes the connection.</p> 1930<p>It must be set to a non-zero value (e.g. 120 seconds) to protect against 1931potential Denial-of-Service attacks in case the server is deployed without a 1932reverse proxy in front.</p> 1933<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> 1934<div class="api_metadata"> 1935<details class="changelog"><summary>History</summary> 1936<table> 1937<tbody><tr><th>Version</th><th>Changes</th></tr> 1938<tr><td>v13.0.0</td> 1939<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr> 1940<tr><td>v0.9.12</td> 1941<td><p><span>Added in: v0.9.12</span></p></td></tr> 1942</tbody></table> 1943</details> 1944</div> 1945<ul> 1946<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> 1947<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1948<li>Returns: <a href="http.html#class-httpserver" class="type"><http.Server></a></li> 1949</ul> 1950<p>Sets the timeout value for sockets, and emits a <code>'timeout'</code> event on 1951the Server object, passing the socket as an argument, if a timeout 1952occurs.</p> 1953<p>If there is a <code>'timeout'</code> event listener on the Server object, then it 1954will be called with the timed-out socket as an argument.</p> 1955<p>By default, the Server does not timeout sockets. However, if a callback 1956is assigned to the Server's <code>'timeout'</code> event, timeouts must be handled 1957explicitly.</p> 1958<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> 1959<div class="api_metadata"> 1960<span>Added in: v16.10.0</span> 1961</div> 1962<ul> 1963<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> 1964</ul> 1965<p>The maximum number of requests socket can handle 1966before closing keep alive connection.</p> 1967<p>A value of <code>0</code> will disable the limit.</p> 1968<p>When the limit is reached it will set the <code>Connection</code> header value to <code>close</code>, 1969but will not actually close the connection, subsequent requests sent 1970after the limit is reached will get <code>503 Service Unavailable</code> as a response.</p> 1971<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> 1972<div class="api_metadata"> 1973<details class="changelog"><summary>History</summary> 1974<table> 1975<tbody><tr><th>Version</th><th>Changes</th></tr> 1976<tr><td>v13.0.0</td> 1977<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr> 1978<tr><td>v0.9.12</td> 1979<td><p><span>Added in: v0.9.12</span></p></td></tr> 1980</tbody></table> 1981</details> 1982</div> 1983<ul> 1984<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> 1985</ul> 1986<p>The number of milliseconds of inactivity before a socket is presumed 1987to have timed out.</p> 1988<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p> 1989<p>The socket timeout logic is set up on connection, so changing this 1990value only affects new connections to the server, not any existing connections.</p> 1991<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> 1992<div class="api_metadata"> 1993<span>Added in: v8.0.0</span> 1994</div> 1995<ul> 1996<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> 1997</ul> 1998<p>The number of milliseconds of inactivity a server needs to wait for additional 1999incoming data, after it has finished writing the last response, before a socket 2000will be destroyed. If the server receives new data before the keep-alive 2001timeout has fired, it will reset the regular inactivity timeout, i.e., 2002<a href="#servertimeout"><code>server.timeout</code></a>.</p> 2003<p>A value of <code>0</code> will disable the keep-alive timeout behavior on incoming 2004connections. 2005A value of <code>0</code> makes the http server behave similarly to Node.js versions prior 2006to 8.0.0, which did not have a keep-alive timeout.</p> 2007<p>The socket timeout logic is set up on connection, so changing this value only 2008affects new connections to the server, not any existing connections.</p> 2009</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> 2010<div class="api_metadata"> 2011<span>Added in: v0.1.17</span> 2012</div> 2013<ul> 2014<li>Extends: <a href="http.html#class-httpoutgoingmessage" class="type"><http.OutgoingMessage></a></li> 2015</ul> 2016<p>This object is created internally by an HTTP server, not by the user. It is 2017passed as the second parameter to the <a href="#event-request"><code>'request'</code></a> event.</p> 2018<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> 2019<div class="api_metadata"> 2020<span>Added in: v0.6.7</span> 2021</div> 2022<p>Indicates that the response is completed, or its underlying connection was 2023terminated prematurely (before the response completion).</p> 2024<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> 2025<div class="api_metadata"> 2026<span>Added in: v0.3.6</span> 2027</div> 2028<p>Emitted when the response has been sent. More specifically, this event is 2029emitted when the last segment of the response headers and body have been 2030handed off to the operating system for transmission over the network. It 2031does not imply that the client has received anything yet.</p> 2032<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> 2033<div class="api_metadata"> 2034<span>Added in: v0.3.0</span> 2035</div> 2036<ul> 2037<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2038</ul> 2039<p>This method adds HTTP trailing headers (a header but at the end of the 2040message) to the response.</p> 2041<p>Trailers will <strong>only</strong> be emitted if chunked encoding is used for the 2042response; if it is not (e.g. if the request was HTTP/1.0), they will 2043be silently discarded.</p> 2044<p>HTTP requires the <code>Trailer</code> header to be sent in order to 2045emit trailers, with a list of the header fields in its value. E.g.,</p> 2046<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>, 2047 <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> }); 2048response.<span class="hljs-title function_">write</span>(fileData); 2049response.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-MD5'</span>: <span class="hljs-string">'7895bf4b8828b55ceaf47747b4bca667'</span> }); 2050response.<span class="hljs-title function_">end</span>();</code> <button class="copy-button">copy</button></pre> 2051<p>Attempting to set a header field name or value that contains invalid characters 2052will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p> 2053<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> 2054<div class="api_metadata"> 2055<span>Added in: v0.3.0</span><span>Deprecated since: v13.0.0</span> 2056</div> 2057<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> 2058<ul> 2059<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 2060</ul> 2061<p>See <a href="#responsesocket"><code>response.socket</code></a>.</p> 2062<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> 2063<div class="api_metadata"> 2064<span>Added in: v13.2.0, v12.16.0</span> 2065</div> 2066<p>See <a href="stream.html#writablecork"><code>writable.cork()</code></a>.</p> 2067<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> 2068<div class="api_metadata"> 2069<details class="changelog"><summary>History</summary> 2070<table> 2071<tbody><tr><th>Version</th><th>Changes</th></tr> 2072<tr><td>v15.0.0</td> 2073<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 2074<tr><td>v10.0.0</td> 2075<td><p>This method now returns a reference to <code>ServerResponse</code>.</p></td></tr> 2076<tr><td>v0.1.90</td> 2077<td><p><span>Added in: v0.1.90</span></p></td></tr> 2078</tbody></table> 2079</details> 2080</div> 2081<ul> 2082<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> 2083<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2084<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2085<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 2086</ul> 2087<p>This method signals to the server that all of the response headers and body 2088have been sent; that server should consider this message complete. 2089The method, <code>response.end()</code>, MUST be called on each response.</p> 2090<p>If <code>data</code> is specified, it is similar in effect to calling 2091<a href="#responsewritechunk-encoding-callback"><code>response.write(data, encoding)</code></a> followed by <code>response.end(callback)</code>.</p> 2092<p>If <code>callback</code> is specified, it will be called when the response stream 2093is finished.</p> 2094<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> 2095<div class="api_metadata"> 2096<span>Added in: v0.0.2</span><span>Deprecated since: v13.4.0, v12.16.0</span> 2097</div> 2098<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> 2099<ul> 2100<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2101</ul> 2102<p>The <code>response.finished</code> property will be <code>true</code> if <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> 2103has been called.</p> 2104<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> 2105<div class="api_metadata"> 2106<span>Added in: v1.6.0</span> 2107</div> 2108<p>Flushes the response headers. See also: <a href="#requestflushheaders"><code>request.flushHeaders()</code></a>.</p> 2109<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> 2110<div class="api_metadata"> 2111<span>Added in: v0.4.0</span> 2112</div> 2113<ul> 2114<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2115<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2116</ul> 2117<p>Reads out a header that's already been queued but not sent to the client. 2118The name is case-insensitive. The type of the return value depends 2119on the arguments provided to <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>.</p> 2120<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>); 2121response.<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)); 2122response.<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>]); 2123<span class="hljs-keyword">const</span> contentType = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'content-type'</span>); 2124<span class="hljs-comment">// contentType is 'text/html'</span> 2125<span class="hljs-keyword">const</span> contentLength = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Length'</span>); 2126<span class="hljs-comment">// contentLength is of type number</span> 2127<span class="hljs-keyword">const</span> setCookie = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'set-cookie'</span>); 2128<span class="hljs-comment">// setCookie is of type string[]</span></code> <button class="copy-button">copy</button></pre> 2129<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> 2130<div class="api_metadata"> 2131<span>Added in: v7.7.0</span> 2132</div> 2133<ul> 2134<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 2135</ul> 2136<p>Returns an array containing the unique names of the current outgoing headers. 2137All header names are lowercase.</p> 2138<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>); 2139response.<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>]); 2140 2141<span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">getHeaderNames</span>(); 2142<span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code> <button class="copy-button">copy</button></pre> 2143<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> 2144<div class="api_metadata"> 2145<span>Added in: v7.7.0</span> 2146</div> 2147<ul> 2148<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2149</ul> 2150<p>Returns a shallow copy of the current outgoing headers. Since a shallow copy 2151is used, array values may be mutated without additional calls to various 2152header-related http module methods. The keys of the returned object are the 2153header names and the values are the respective header values. All header names 2154are lowercase.</p> 2155<p>The object returned by the <code>response.getHeaders()</code> method <em>does not</em> 2156prototypically inherit from the JavaScript <code>Object</code>. This means that typical 2157<code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, and others 2158are not defined and <em>will not work</em>.</p> 2159<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>); 2160response.<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>]); 2161 2162<span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHeaders</span>(); 2163<span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</span></code> <button class="copy-button">copy</button></pre> 2164<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> 2165<div class="api_metadata"> 2166<span>Added in: v7.7.0</span> 2167</div> 2168<ul> 2169<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2170<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2171</ul> 2172<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the 2173outgoing headers. The header name matching is case-insensitive.</p> 2174<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> 2175<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> 2176<div class="api_metadata"> 2177<span>Added in: v0.9.3</span> 2178</div> 2179<ul> 2180<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2181</ul> 2182<p>Boolean (read-only). True if headers were sent, false otherwise.</p> 2183<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> 2184<div class="api_metadata"> 2185<span>Added in: v0.4.0</span> 2186</div> 2187<ul> 2188<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2189</ul> 2190<p>Removes a header that's queued for implicit sending.</p> 2191<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> 2192<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> 2193<div class="api_metadata"> 2194<span>Added in: v15.7.0</span> 2195</div> 2196<ul> 2197<li><a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 2198</ul> 2199<p>A reference to the original HTTP <code>request</code> object.</p> 2200<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> 2201<div class="api_metadata"> 2202<span>Added in: v0.7.5</span> 2203</div> 2204<ul> 2205<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2206</ul> 2207<p>When true, the Date header will be automatically generated and sent in 2208the response if it is not already present in the headers. Defaults to true.</p> 2209<p>This should only be disabled for testing; HTTP requires the Date header 2210in responses.</p> 2211<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> 2212<div class="api_metadata"> 2213<span>Added in: v0.4.0</span> 2214</div> 2215<ul> 2216<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2217<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2218<li>Returns: <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 2219</ul> 2220<p>Returns the response object.</p> 2221<p>Sets a single header value for implicit headers. If this header already exists 2222in the to-be-sent headers, its value will be replaced. Use an array of strings 2223here to send multiple headers with the same name. Non-string values will be 2224stored without modification. Therefore, <a href="#responsegetheadername"><code>response.getHeader()</code></a> may return 2225non-string values. However, the non-string values will be converted to strings 2226for network transmission. The same response object is returned to the caller, 2227to enable call chaining.</p> 2228<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> 2229<p>or</p> 2230<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> 2231<p>Attempting to set a header field name or value that contains invalid characters 2232will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p> 2233<p>When headers have been set with <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>, they will be merged 2234with any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, with the headers passed 2235to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p> 2236<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span> 2237<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> { 2238 res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>); 2239 res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>); 2240 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> }); 2241 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 2242});</code> <button class="copy-button">copy</button></pre> 2243<p>If <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> method is called and this method has not been 2244called, it will directly write the supplied header values onto the network 2245channel without caching internally, and the <a href="#responsegetheadername"><code>response.getHeader()</code></a> on the 2246header will not yield the expected result. If progressive population of headers 2247is desired with potential future retrieval and modification, use 2248<a href="#responsesetheadername-value"><code>response.setHeader()</code></a> instead of <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>.</p> 2249<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> 2250<div class="api_metadata"> 2251<span>Added in: v0.9.12</span> 2252</div> 2253<ul> 2254<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2255<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2256<li>Returns: <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 2257</ul> 2258<p>Sets the Socket's timeout value to <code>msecs</code>. If a callback is 2259provided, then it is added as a listener on the <code>'timeout'</code> event on 2260the response object.</p> 2261<p>If no <code>'timeout'</code> listener is added to the request, the response, or 2262the server, then sockets are destroyed when they time out. If a handler is 2263assigned to the request, the response, or the server's <code>'timeout'</code> events, 2264timed out sockets must be handled explicitly.</p> 2265<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> 2266<div class="api_metadata"> 2267<span>Added in: v0.3.0</span> 2268</div> 2269<ul> 2270<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 2271</ul> 2272<p>Reference to the underlying socket. Usually users will not want to access 2273this property. In particular, the socket will not emit <code>'readable'</code> events 2274because of how the protocol parser attaches to the socket. After 2275<code>response.end()</code>, the property is nulled.</p> 2276<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 2277<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> { 2278 <span class="hljs-keyword">const</span> ip = res.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>; 2279 <span class="hljs-keyword">const</span> port = res.<span class="hljs-property">socket</span>.<span class="hljs-property">remotePort</span>; 2280 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>); 2281}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code> <button class="copy-button">copy</button></pre> 2282<p>This property is guaranteed to be an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 2283a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specified a socket 2284type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 2285<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> 2286<div class="api_metadata"> 2287<span>Added in: v0.4.0</span> 2288</div> 2289<ul> 2290<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> 2291</ul> 2292<p>When using implicit headers (not calling <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> explicitly), 2293this property controls the status code that will be sent to the client when 2294the headers get flushed.</p> 2295<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> 2296<p>After response header was sent to the client, this property indicates the 2297status code which was sent out.</p> 2298<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> 2299<div class="api_metadata"> 2300<span>Added in: v0.11.8</span> 2301</div> 2302<ul> 2303<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2304</ul> 2305<p>When using implicit headers (not calling <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> explicitly), 2306this property controls the status message that will be sent to the client when 2307the headers get flushed. If this is left as <code>undefined</code> then the standard 2308message for the status code will be used.</p> 2309<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> 2310<p>After response header was sent to the client, this property indicates the 2311status message which was sent out.</p> 2312<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> 2313<div class="api_metadata"> 2314<span>Added in: v18.10.0, v16.18.0</span> 2315</div> 2316<ul> 2317<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> 2318</ul> 2319<p>If set to <code>true</code>, Node.js will check whether the <code>Content-Length</code> 2320header value and the size of the body, in bytes, are equal. 2321Mismatching the <code>Content-Length</code> header value will result 2322in 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> 2323<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> 2324<div class="api_metadata"> 2325<span>Added in: v13.2.0, v12.16.0</span> 2326</div> 2327<p>See <a href="stream.html#writableuncork"><code>writable.uncork()</code></a>.</p> 2328<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> 2329<div class="api_metadata"> 2330<span>Added in: v12.9.0</span> 2331</div> 2332<ul> 2333<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2334</ul> 2335<p>Is <code>true</code> after <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> has been called. This property 2336does not indicate whether the data has been flushed, for this use 2337<a href="#responsewritablefinished"><code>response.writableFinished</code></a> instead.</p> 2338<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> 2339<div class="api_metadata"> 2340<span>Added in: v12.7.0</span> 2341</div> 2342<ul> 2343<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2344</ul> 2345<p>Is <code>true</code> if all data has been flushed to the underlying system, immediately 2346before the <a href="#event-finish"><code>'finish'</code></a> event is emitted.</p> 2347<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> 2348<div class="api_metadata"> 2349<details class="changelog"><summary>History</summary> 2350<table> 2351<tbody><tr><th>Version</th><th>Changes</th></tr> 2352<tr><td>v15.0.0</td> 2353<td><p>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 2354<tr><td>v0.1.29</td> 2355<td><p><span>Added in: v0.1.29</span></p></td></tr> 2356</tbody></table> 2357</details> 2358</div> 2359<ul> 2360<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> 2361<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> 2362<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2363<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2364</ul> 2365<p>If this method is called and <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> has not been called, 2366it will switch to implicit header mode and flush the implicit headers.</p> 2367<p>This sends a chunk of the response body. This method may 2368be called multiple times to provide successive parts of the body.</p> 2369<p>Writing to the body is not allowed when the request method or response status 2370do not support content. If an attempt is made to write to the body for a 2371HEAD request or as part of a <code>204</code> or <code>304</code>response, a synchronous <code>Error</code> 2372with the code <code>ERR_HTTP_BODY_NOT_ALLOWED</code> is thrown.</p> 2373<p><code>chunk</code> can be a string or a buffer. If <code>chunk</code> is a string, 2374the second parameter specifies how to encode it into a byte stream. 2375<code>callback</code> will be called when this chunk of data is flushed.</p> 2376<p>This is the raw HTTP body and has nothing to do with higher-level multi-part 2377body encodings that may be used.</p> 2378<p>The first time <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> is called, it will send the buffered 2379header information and the first chunk of the body to the client. The second 2380time <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> is called, Node.js assumes data will be streamed, 2381and sends the new data separately. That is, the response is buffered up to the 2382first chunk of the body.</p> 2383<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel 2384buffer. Returns <code>false</code> if all or part of the data was queued in user memory. 2385<code>'drain'</code> will be emitted when the buffer is free again.</p> 2386<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> 2387<div class="api_metadata"> 2388<span>Added in: v0.3.0</span> 2389</div> 2390<p>Sends an HTTP/1.1 100 Continue message to the client, indicating that 2391the request body should be sent. See the <a href="#event-checkcontinue"><code>'checkContinue'</code></a> event on 2392<code>Server</code>.</p> 2393<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> 2394<div class="api_metadata"> 2395<details class="changelog"><summary>History</summary> 2396<table> 2397<tbody><tr><th>Version</th><th>Changes</th></tr> 2398<tr><td>v18.11.0</td> 2399<td><p>Allow passing hints as an object.</p></td></tr> 2400<tr><td>v18.11.0</td> 2401<td><p><span>Added in: v18.11.0</span></p></td></tr> 2402</tbody></table> 2403</details> 2404</div> 2405<ul> 2406<li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2407<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2408</ul> 2409<p>Sends an HTTP/1.1 103 Early Hints message to the client with a Link header, 2410indicating that the user agent can preload/preconnect the linked resources. 2411The <code>hints</code> is an object containing the values of headers to be sent with 2412early hints message. The optional <code>callback</code> argument will be called when 2413the response message has been written.</p> 2414<p><strong>Example</strong></p> 2415<pre><code class="language-js"><span class="hljs-keyword">const</span> earlyHintsLink = <span class="hljs-string">'</styles.css>; rel=preload; as=style'</span>; 2416response.<span class="hljs-title function_">writeEarlyHints</span>({ 2417 <span class="hljs-string">'link'</span>: earlyHintsLink, 2418}); 2419 2420<span class="hljs-keyword">const</span> earlyHintsLinks = [ 2421 <span class="hljs-string">'</styles.css>; rel=preload; as=style'</span>, 2422 <span class="hljs-string">'</scripts.js>; rel=preload; as=script'</span>, 2423]; 2424response.<span class="hljs-title function_">writeEarlyHints</span>({ 2425 <span class="hljs-string">'link'</span>: earlyHintsLinks, 2426 <span class="hljs-string">'x-trace-id'</span>: <span class="hljs-string">'id for diagnostics'</span>, 2427}); 2428 2429<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>); 2430response.<span class="hljs-title function_">writeEarlyHints</span>({ 2431 <span class="hljs-string">'link'</span>: earlyHintsLinks, 2432}, earlyHintsCallback);</code> <button class="copy-button">copy</button></pre> 2433<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> 2434<div class="api_metadata"> 2435<details class="changelog"><summary>History</summary> 2436<table> 2437<tbody><tr><th>Version</th><th>Changes</th></tr> 2438<tr><td>v14.14.0</td> 2439<td><p>Allow passing headers as an array.</p></td></tr> 2440<tr><td>v11.10.0, v10.17.0</td> 2441<td><p>Return <code>this</code> from <code>writeHead()</code> to allow chaining with <code>end()</code>.</p></td></tr> 2442<tr><td>v5.11.0, v4.4.5</td> 2443<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> 2444<tr><td>v0.1.30</td> 2445<td><p><span>Added in: v0.1.30</span></p></td></tr> 2446</tbody></table> 2447</details> 2448</div> 2449<ul> 2450<li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2451<li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2452<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> 2453<li>Returns: <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 2454</ul> 2455<p>Sends a response header to the request. The status code is a 3-digit HTTP 2456status code, like <code>404</code>. The last argument, <code>headers</code>, are the response headers. 2457Optionally one can give a human-readable <code>statusMessage</code> as the second 2458argument.</p> 2459<p><code>headers</code> may be an <code>Array</code> where the keys and values are in the same list. 2460It is <em>not</em> a list of tuples. So, the even-numbered offsets are key values, 2461and the odd-numbered offsets are the associated values. The array is in the same 2462format as <code>request.rawHeaders</code>.</p> 2463<p>Returns a reference to the <code>ServerResponse</code>, so that calls can be chained.</p> 2464<pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-string">'hello world'</span>; 2465response 2466 .<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { 2467 <span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(body), 2468 <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span>, 2469 }) 2470 .<span class="hljs-title function_">end</span>(body);</code> <button class="copy-button">copy</button></pre> 2471<p>This method must only be called once on a message and it must 2472be called before <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> is called.</p> 2473<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 2474this, the implicit/mutable headers will be calculated and call this function.</p> 2475<p>When headers have been set with <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>, they will be merged 2476with any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, with the headers passed 2477to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p> 2478<p>If this method is called and <a href="#responsesetheadername-value"><code>response.setHeader()</code></a> has not been called, 2479it will directly write the supplied header values onto the network channel 2480without caching internally, and the <a href="#responsegetheadername"><code>response.getHeader()</code></a> on the header 2481will not yield the expected result. If progressive population of headers is 2482desired with potential future retrieval and modification, use 2483<a href="#responsesetheadername-value"><code>response.setHeader()</code></a> instead.</p> 2484<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span> 2485<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> { 2486 res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>); 2487 res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>); 2488 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> }); 2489 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 2490});</code> <button class="copy-button">copy</button></pre> 2491<p><code>Content-Length</code> is read in bytes, not characters. Use 2492<a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength()</code></a> to determine the length of the body in bytes. Node.js 2493will check whether <code>Content-Length</code> and the length of the body which has 2494been transmitted are equal or not.</p> 2495<p>Attempting to set a header field name or value that contains invalid characters 2496will result in a [<code>Error</code>][] being thrown.</p> 2497<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> 2498<div class="api_metadata"> 2499<span>Added in: v10.0.0</span> 2500</div> 2501<p>Sends a HTTP/1.1 102 Processing message to the client, indicating that 2502the request body should be sent.</p> 2503</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> 2504<div class="api_metadata"> 2505<details class="changelog"><summary>History</summary> 2506<table> 2507<tbody><tr><th>Version</th><th>Changes</th></tr> 2508<tr><td>v15.5.0</td> 2509<td><p>The <code>destroyed</code> value returns <code>true</code> after the incoming data is consumed.</p></td></tr> 2510<tr><td>v13.1.0, v12.16.0</td> 2511<td><p>The <code>readableHighWaterMark</code> value mirrors that of the socket.</p></td></tr> 2512<tr><td>v0.1.17</td> 2513<td><p><span>Added in: v0.1.17</span></p></td></tr> 2514</tbody></table> 2515</details> 2516</div> 2517<ul> 2518<li>Extends: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li> 2519</ul> 2520<p>An <code>IncomingMessage</code> object is created by <a href="#class-httpserver"><code>http.Server</code></a> or 2521<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> 2522and <a href="#event-response"><code>'response'</code></a> event respectively. It may be used to access response 2523status, headers, and data.</p> 2524<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 2525<code>IncomingMessage</code> itself extends <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> and is created separately to 2526parse and emit the incoming HTTP headers and payload, as the underlying socket 2527may be reused multiple times in case of keep-alive.</p> 2528<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> 2529<div class="api_metadata"> 2530<span>Added in: v0.3.8</span><span>Deprecated since: v17.0.0, v16.12.0</span> 2531</div> 2532<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> 2533<p>Emitted when the request has been aborted.</p> 2534<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> 2535<div class="api_metadata"> 2536<details class="changelog"><summary>History</summary> 2537<table> 2538<tbody><tr><th>Version</th><th>Changes</th></tr> 2539<tr><td>v16.0.0</td> 2540<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> 2541<tr><td>v0.4.2</td> 2542<td><p><span>Added in: v0.4.2</span></p></td></tr> 2543</tbody></table> 2544</details> 2545</div> 2546<p>Emitted when the request has been completed.</p> 2547<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> 2548<div class="api_metadata"> 2549<span>Added in: v10.1.0</span><span>Deprecated since: v17.0.0, v16.12.0</span> 2550</div> 2551<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> 2552<ul> 2553<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2554</ul> 2555<p>The <code>message.aborted</code> property will be <code>true</code> if the request has 2556been aborted.</p> 2557<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> 2558<div class="api_metadata"> 2559<span>Added in: v0.3.0</span> 2560</div> 2561<ul> 2562<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2563</ul> 2564<p>The <code>message.complete</code> property will be <code>true</code> if a complete HTTP message has 2565been received and successfully parsed.</p> 2566<p>This property is particularly useful as a means of determining if a client or 2567server fully transmitted a message before a connection was terminated:</p> 2568<pre><code class="language-js"><span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>({ 2569 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>, 2570 <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>, 2571 <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>, 2572}, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 2573 res.<span class="hljs-title function_">resume</span>(); 2574 res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 2575 <span class="hljs-keyword">if</span> (!res.<span class="hljs-property">complete</span>) 2576 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>( 2577 <span class="hljs-string">'The connection was terminated while the message was still being sent'</span>); 2578 }); 2579});</code> <button class="copy-button">copy</button></pre> 2580<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> 2581<div class="api_metadata"> 2582<span>Added in: v0.1.90</span><span>Deprecated since: v16.0.0</span> 2583</div> 2584<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> 2585<p>Alias for <a href="#messagesocket"><code>message.socket</code></a>.</p> 2586<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> 2587<div class="api_metadata"> 2588<details class="changelog"><summary>History</summary> 2589<table> 2590<tbody><tr><th>Version</th><th>Changes</th></tr> 2591<tr><td>v14.5.0, v12.19.0</td> 2592<td><p>The function returns <code>this</code> for consistency with other Readable streams.</p></td></tr> 2593<tr><td>v0.3.0</td> 2594<td><p><span>Added in: v0.3.0</span></p></td></tr> 2595</tbody></table> 2596</details> 2597</div> 2598<ul> 2599<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2600<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 2601</ul> 2602<p>Calls <code>destroy()</code> on the socket that received the <code>IncomingMessage</code>. If <code>error</code> 2603is provided, an <code>'error'</code> event is emitted on the socket and <code>error</code> is passed 2604as an argument to any listeners on the event.</p> 2605<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> 2606<div class="api_metadata"> 2607<details class="changelog"><summary>History</summary> 2608<table> 2609<tbody><tr><th>Version</th><th>Changes</th></tr> 2610<tr><td>v18.14.0</td> 2611<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> 2612<tr><td>v15.1.0</td> 2613<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> 2614<tr><td>v0.1.5</td> 2615<td><p><span>Added in: v0.1.5</span></p></td></tr> 2616</tbody></table> 2617</details> 2618</div> 2619<ul> 2620<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2621</ul> 2622<p>The request/response headers object.</p> 2623<p>Key-value pairs of header names and values. Header names are lower-cased.</p> 2624<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span> 2625<span class="hljs-comment">//</span> 2626<span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span> 2627<span class="hljs-comment">// host: '127.0.0.1:8000',</span> 2628<span class="hljs-comment">// accept: '*/*' }</span> 2629<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> 2630<p>Duplicates in raw headers are handled in the following ways, depending on the 2631header name:</p> 2632<ul> 2633<li>Duplicates of <code>age</code>, <code>authorization</code>, <code>content-length</code>, <code>content-type</code>, 2634<code>etag</code>, <code>expires</code>, <code>from</code>, <code>host</code>, <code>if-modified-since</code>, <code>if-unmodified-since</code>, 2635<code>last-modified</code>, <code>location</code>, <code>max-forwards</code>, <code>proxy-authorization</code>, <code>referer</code>, 2636<code>retry-after</code>, <code>server</code>, or <code>user-agent</code> are discarded. 2637To allow duplicate values of the headers listed above to be joined, 2638use the option <code>joinDuplicateHeaders</code> in <a href="#httprequestoptions-callback"><code>http.request()</code></a> 2639and <a href="#httpcreateserveroptions-requestlistener"><code>http.createServer()</code></a>. See RFC 9110 Section 5.3 for more 2640information.</li> 2641<li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li> 2642<li>For duplicate <code>cookie</code> headers, the values are joined together with <code>; </code>.</li> 2643<li>For all other headers, the values are joined together with <code>, </code>.</li> 2644</ul> 2645<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> 2646<div class="api_metadata"> 2647<span>Added in: v18.3.0</span> 2648</div> 2649<ul> 2650<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2651</ul> 2652<p>Similar to <a href="#messageheaders"><code>message.headers</code></a>, but there is no join logic and the values are 2653always arrays of strings, even for headers received just once.</p> 2654<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span> 2655<span class="hljs-comment">//</span> 2656<span class="hljs-comment">// { 'user-agent': ['curl/7.22.0'],</span> 2657<span class="hljs-comment">// host: ['127.0.0.1:8000'],</span> 2658<span class="hljs-comment">// accept: ['*/*'] }</span> 2659<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> 2660<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> 2661<div class="api_metadata"> 2662<span>Added in: v0.1.1</span> 2663</div> 2664<ul> 2665<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2666</ul> 2667<p>In case of server request, the HTTP version sent by the client. In the case of 2668client response, the HTTP version of the connected-to server. 2669Probably either <code>'1.1'</code> or <code>'1.0'</code>.</p> 2670<p>Also <code>message.httpVersionMajor</code> is the first integer and 2671<code>message.httpVersionMinor</code> is the second.</p> 2672<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> 2673<div class="api_metadata"> 2674<span>Added in: v0.1.1</span> 2675</div> 2676<ul> 2677<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2678</ul> 2679<p><strong>Only valid for request obtained from <a href="#class-httpserver"><code>http.Server</code></a>.</strong></p> 2680<p>The request method as a string. Read only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.</p> 2681<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> 2682<div class="api_metadata"> 2683<span>Added in: v0.11.6</span> 2684</div> 2685<ul> 2686<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 2687</ul> 2688<p>The raw request/response headers list exactly as they were received.</p> 2689<p>The keys and values are in the same list. It is <em>not</em> a 2690list of tuples. So, the even-numbered offsets are key values, and the 2691odd-numbered offsets are the associated values.</p> 2692<p>Header names are not lowercased, and duplicates are not merged.</p> 2693<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span> 2694<span class="hljs-comment">//</span> 2695<span class="hljs-comment">// [ 'user-agent',</span> 2696<span class="hljs-comment">// 'this is invalid because there can be only one',</span> 2697<span class="hljs-comment">// 'User-Agent',</span> 2698<span class="hljs-comment">// 'curl/7.22.0',</span> 2699<span class="hljs-comment">// 'Host',</span> 2700<span class="hljs-comment">// '127.0.0.1:8000',</span> 2701<span class="hljs-comment">// 'ACCEPT',</span> 2702<span class="hljs-comment">// '*/*' ]</span> 2703<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> 2704<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> 2705<div class="api_metadata"> 2706<span>Added in: v0.11.6</span> 2707</div> 2708<ul> 2709<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 2710</ul> 2711<p>The raw request/response trailer keys and values exactly as they were 2712received. Only populated at the <code>'end'</code> event.</p> 2713<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> 2714<div class="api_metadata"> 2715<span>Added in: v0.5.9</span> 2716</div> 2717<ul> 2718<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2719<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2720<li>Returns: <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 2721</ul> 2722<p>Calls <code>message.socket.setTimeout(msecs, callback)</code>.</p> 2723<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> 2724<div class="api_metadata"> 2725<span>Added in: v0.3.0</span> 2726</div> 2727<ul> 2728<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 2729</ul> 2730<p>The <a href="net.html#class-netsocket"><code>net.Socket</code></a> object associated with the connection.</p> 2731<p>With HTTPS support, use <a href="tls.html#tlssocketgetpeercertificatedetailed"><code>request.socket.getPeerCertificate()</code></a> to obtain the 2732client's authentication details.</p> 2733<p>This property is guaranteed to be an instance of the <a href="net.html#class-netsocket" class="type"><net.Socket></a> class, 2734a subclass of <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a>, unless the user specified a socket 2735type other than <a href="net.html#class-netsocket" class="type"><net.Socket></a> or internally nulled.</p> 2736<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> 2737<div class="api_metadata"> 2738<span>Added in: v0.1.1</span> 2739</div> 2740<ul> 2741<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2742</ul> 2743<p><strong>Only valid for response obtained from <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a>.</strong></p> 2744<p>The 3-digit HTTP response status code. E.G. <code>404</code>.</p> 2745<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> 2746<div class="api_metadata"> 2747<span>Added in: v0.11.10</span> 2748</div> 2749<ul> 2750<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2751</ul> 2752<p><strong>Only valid for response obtained from <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a>.</strong></p> 2753<p>The HTTP response status message (reason phrase). E.G. <code>OK</code> or <code>Internal Server Error</code>.</p> 2754<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> 2755<div class="api_metadata"> 2756<span>Added in: v0.3.0</span> 2757</div> 2758<ul> 2759<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2760</ul> 2761<p>The request/response trailers object. Only populated at the <code>'end'</code> event.</p> 2762<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> 2763<div class="api_metadata"> 2764<span>Added in: v18.3.0</span> 2765</div> 2766<ul> 2767<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2768</ul> 2769<p>Similar to <a href="#messagetrailers"><code>message.trailers</code></a>, but there is no join logic and the values are 2770always arrays of strings, even for headers received just once. 2771Only populated at the <code>'end'</code> event.</p> 2772<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> 2773<div class="api_metadata"> 2774<span>Added in: v0.1.90</span> 2775</div> 2776<ul> 2777<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2778</ul> 2779<p><strong>Only valid for request obtained from <a href="#class-httpserver"><code>http.Server</code></a>.</strong></p> 2780<p>Request URL string. This contains only the URL that is present in the actual 2781HTTP request. Take the following request:</p> 2782<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> 2783<span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code> <button class="copy-button">copy</button></pre> 2784<p>To parse the URL into its parts:</p> 2785<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> 2786<p>When <code>request.url</code> is <code>'/status?name=ryan'</code> and <code>request.headers.host</code> is 2787<code>'localhost:3000'</code>:</p> 2788<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node</span> 2789<span class="hljs-meta prompt_">> </span><span class="language-bash">new URL(request.url, `http://<span class="hljs-variable">${request.headers.host}</span>`)</span> 2790URL { 2791 href: 'http://localhost:3000/status?name=ryan', 2792 origin: 'http://localhost:3000', 2793 protocol: 'http:', 2794 username: '', 2795 password: '', 2796 host: 'localhost:3000', 2797 hostname: 'localhost', 2798 port: '3000', 2799 pathname: '/status', 2800 search: '?name=ryan', 2801 searchParams: URLSearchParams { 'name' => 'ryan' }, 2802 hash: '' 2803}</code> <button class="copy-button">copy</button></pre> 2804</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> 2805<div class="api_metadata"> 2806<span>Added in: v0.1.17</span> 2807</div> 2808<ul> 2809<li>Extends: <a href="stream.html#stream" class="type"><Stream></a></li> 2810</ul> 2811<p>This class serves as the parent class of <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a> 2812and <a href="#class-httpserverresponse"><code>http.ServerResponse</code></a>. It is an abstract outgoing message from 2813the perspective of the participants of an HTTP transaction.</p> 2814<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> 2815<div class="api_metadata"> 2816<span>Added in: v0.3.6</span> 2817</div> 2818<p>Emitted when the buffer of the message is free again.</p> 2819<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> 2820<div class="api_metadata"> 2821<span>Added in: v0.1.17</span> 2822</div> 2823<p>Emitted when the transmission is finished successfully.</p> 2824<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> 2825<div class="api_metadata"> 2826<span>Added in: v0.11.6</span> 2827</div> 2828<p>Emitted after <code>outgoingMessage.end()</code> is called. 2829When the event is emitted, all data has been processed but not necessarily 2830completely flushed.</p> 2831<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> 2832<div class="api_metadata"> 2833<span>Added in: v0.3.0</span> 2834</div> 2835<ul> 2836<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2837</ul> 2838<p>Adds HTTP trailers (headers but at the end of the message) to the message.</p> 2839<p>Trailers will <strong>only</strong> be emitted if the message is chunked encoded. If not, 2840the trailers will be silently discarded.</p> 2841<p>HTTP requires the <code>Trailer</code> header to be sent to emit trailers, 2842with a list of header field names in its value, e.g.</p> 2843<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>, 2844 <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> }); 2845message.<span class="hljs-title function_">write</span>(fileData); 2846message.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-MD5'</span>: <span class="hljs-string">'7895bf4b8828b55ceaf47747b4bca667'</span> }); 2847message.<span class="hljs-title function_">end</span>();</code> <button class="copy-button">copy</button></pre> 2848<p>Attempting to set a header field name or value that contains invalid characters 2849will result in a <code>TypeError</code> being thrown.</p> 2850<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> 2851<div class="api_metadata"> 2852<span>Added in: v18.3.0</span> 2853</div> 2854<ul> 2855<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> 2856<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> 2857<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 2858</ul> 2859<p>Append a single header value for the header object.</p> 2860<p>If the value is an array, this is equivalent of calling this method multiple 2861times.</p> 2862<p>If there were no previous value for the header, this is equivalent of calling 2863<a href="#outgoingmessagesetheadername-value"><code>outgoingMessage.setHeader(name, value)</code></a>.</p> 2864<p>Depending of the value of <code>options.uniqueHeaders</code> when the client request or the 2865server were created, this will end up in the header being sent multiple times or 2866a single time with values joined using <code>; </code>.</p> 2867<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> 2868<div class="api_metadata"> 2869<span>Added in: v0.3.0</span><span>Deprecated since: v15.12.0, v14.17.1</span> 2870</div> 2871<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> 2872<p>Alias of <a href="#outgoingmessagesocket"><code>outgoingMessage.socket</code></a>.</p> 2873<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> 2874<div class="api_metadata"> 2875<span>Added in: v13.2.0, v12.16.0</span> 2876</div> 2877<p>See <a href="stream.html#writablecork"><code>writable.cork()</code></a>.</p> 2878<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> 2879<div class="api_metadata"> 2880<span>Added in: v0.3.0</span> 2881</div> 2882<ul> 2883<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> 2884<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 2885</ul> 2886<p>Destroys the message. Once a socket is associated with the message 2887and is connected, that socket will be destroyed as well.</p> 2888<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> 2889<div class="api_metadata"> 2890<details class="changelog"><summary>History</summary> 2891<table> 2892<tbody><tr><th>Version</th><th>Changes</th></tr> 2893<tr><td>v15.0.0</td> 2894<td><p>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 2895<tr><td>v0.11.6</td> 2896<td><p>add <code>callback</code> argument.</p></td></tr> 2897<tr><td>v0.1.90</td> 2898<td><p><span>Added in: v0.1.90</span></p></td></tr> 2899</tbody></table> 2900</details> 2901</div> 2902<ul> 2903<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> 2904<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> 2905<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> 2906<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 2907</ul> 2908<p>Finishes the outgoing message. If any parts of the body are unsent, it will 2909flush them to the underlying system. If the message is chunked, it will 2910send the terminating chunk <code>0\r\n\r\n</code>, and send the trailers (if any).</p> 2911<p>If <code>chunk</code> is specified, it is equivalent to calling 2912<code>outgoingMessage.write(chunk, encoding)</code>, followed by 2913<code>outgoingMessage.end(callback)</code>.</p> 2914<p>If <code>callback</code> is provided, it will be called when the message is finished 2915(equivalent to a listener of the <code>'finish'</code> event).</p> 2916<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> 2917<div class="api_metadata"> 2918<span>Added in: v1.6.0</span> 2919</div> 2920<p>Flushes the message headers.</p> 2921<p>For efficiency reason, Node.js normally buffers the message headers 2922until <code>outgoingMessage.end()</code> is called or the first chunk of message data 2923is written. It then tries to pack the headers and data into a single TCP 2924packet.</p> 2925<p>It is usually desired (it saves a TCP round-trip), but not when the first 2926data is not sent until possibly much later. <code>outgoingMessage.flushHeaders()</code> 2927bypasses the optimization and kickstarts the message.</p> 2928<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> 2929<div class="api_metadata"> 2930<span>Added in: v0.4.0</span> 2931</div> 2932<ul> 2933<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> 2934<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> 2935</ul> 2936<p>Gets the value of the HTTP header with the given name. If that header is not 2937set, the returned value will be <code>undefined</code>.</p> 2938<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> 2939<div class="api_metadata"> 2940<span>Added in: v7.7.0</span> 2941</div> 2942<ul> 2943<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 2944</ul> 2945<p>Returns an array containing the unique names of the current outgoing headers. 2946All names are lowercase.</p> 2947<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> 2948<div class="api_metadata"> 2949<span>Added in: v7.7.0</span> 2950</div> 2951<ul> 2952<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2953</ul> 2954<p>Returns a shallow copy of the current outgoing headers. Since a shallow 2955copy is used, array values may be mutated without additional calls to 2956various header-related HTTP module methods. The keys of the returned 2957object are the header names and the values are the respective header 2958values. All header names are lowercase.</p> 2959<p>The object returned by the <code>outgoingMessage.getHeaders()</code> method does 2960not prototypically inherit from the JavaScript <code>Object</code>. This means that 2961typical <code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, 2962and others are not defined and will not work.</p> 2963<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>); 2964outgoingMessage.<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>]); 2965 2966<span class="hljs-keyword">const</span> headers = outgoingMessage.<span class="hljs-title function_">getHeaders</span>(); 2967<span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</span></code> <button class="copy-button">copy</button></pre> 2968<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> 2969<div class="api_metadata"> 2970<span>Added in: v7.7.0</span> 2971</div> 2972<ul> 2973<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2974<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2975</ul> 2976<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the 2977outgoing headers. The header name is case-insensitive.</p> 2978<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> 2979<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> 2980<div class="api_metadata"> 2981<span>Added in: v0.9.3</span> 2982</div> 2983<ul> 2984<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2985</ul> 2986<p>Read-only. <code>true</code> if the headers were sent, otherwise <code>false</code>.</p> 2987<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> 2988<div class="api_metadata"> 2989<span>Added in: v9.0.0</span> 2990</div> 2991<p>Overrides the <code>stream.pipe()</code> method inherited from the legacy <code>Stream</code> class 2992which is the parent class of <code>http.OutgoingMessage</code>.</p> 2993<p>Calling this method will throw an <code>Error</code> because <code>outgoingMessage</code> is a 2994write-only stream.</p> 2995<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> 2996<div class="api_metadata"> 2997<span>Added in: v0.4.0</span> 2998</div> 2999<ul> 3000<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> 3001</ul> 3002<p>Removes a header that is queued for implicit sending.</p> 3003<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> 3004<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> 3005<div class="api_metadata"> 3006<span>Added in: v0.4.0</span> 3007</div> 3008<ul> 3009<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> 3010<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> 3011<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 3012</ul> 3013<p>Sets a single header value. If the header already exists in the to-be-sent 3014headers, its value will be replaced. Use an array of strings to send multiple 3015headers with the same name.</p> 3016<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> 3017<div class="api_metadata"> 3018<span>Added in: v18.15.0</span> 3019</div> 3020<ul> 3021<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> 3022<li>Returns: <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 3023</ul> 3024<p>Returns the response object.</p> 3025<p>Sets multiple header values for implicit headers. 3026<code>headers</code> must be an instance of <a href="globals.html#class-headers"><code>Headers</code></a> or <code>Map</code>, 3027if a header already exists in the to-be-sent headers, 3028its value will be replaced.</p> 3029<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> }); 3030response.<span class="hljs-title function_">setHeaders</span>(headers);</code> <button class="copy-button">copy</button></pre> 3031<p>or</p> 3032<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>]]); 3033res.<span class="hljs-title function_">setHeaders</span>(headers);</code> <button class="copy-button">copy</button></pre> 3034<p>When headers have been set with <a href="#outgoingmessagesetheadersheaders"><code>outgoingMessage.setHeaders()</code></a>, 3035they will be merged with any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, 3036with the headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p> 3037<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span> 3038<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> { 3039 <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> }); 3040 res.<span class="hljs-title function_">setHeaders</span>(headers); 3041 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> }); 3042 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>); 3043});</code> <button class="copy-button">copy</button></pre> 3044<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> 3045<div class="api_metadata"> 3046<span>Added in: v0.9.12</span> 3047</div> 3048<ul> 3049<li><code>msesc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 3050<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 3051occurs. Same as binding to the <code>timeout</code> event.</li> 3052<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li> 3053</ul> 3054<p>Once a socket is associated with the message and is connected, 3055<a href="net.html#socketsettimeouttimeout-callback"><code>socket.setTimeout()</code></a> will be called with <code>msecs</code> as the first parameter.</p> 3056<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> 3057<div class="api_metadata"> 3058<span>Added in: v0.3.0</span> 3059</div> 3060<ul> 3061<li><a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 3062</ul> 3063<p>Reference to the underlying socket. Usually, users will not want to access 3064this property.</p> 3065<p>After calling <code>outgoingMessage.end()</code>, this property will be nulled.</p> 3066<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> 3067<div class="api_metadata"> 3068<span>Added in: v13.2.0, v12.16.0</span> 3069</div> 3070<p>See <a href="stream.html#writableuncork"><code>writable.uncork()</code></a></p> 3071<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> 3072<div class="api_metadata"> 3073<span>Added in: v13.2.0, v12.16.0</span> 3074</div> 3075<ul> 3076<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 3077</ul> 3078<p>The number of times <code>outgoingMessage.cork()</code> has been called.</p> 3079<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> 3080<div class="api_metadata"> 3081<span>Added in: v12.9.0</span> 3082</div> 3083<ul> 3084<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3085</ul> 3086<p>Is <code>true</code> if <code>outgoingMessage.end()</code> has been called. This property does 3087not indicate whether the data has been flushed. For that purpose, use 3088<code>message.writableFinished</code> instead.</p> 3089<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> 3090<div class="api_metadata"> 3091<span>Added in: v12.7.0</span> 3092</div> 3093<ul> 3094<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3095</ul> 3096<p>Is <code>true</code> if all data has been flushed to the underlying system.</p> 3097<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> 3098<div class="api_metadata"> 3099<span>Added in: v12.9.0</span> 3100</div> 3101<ul> 3102<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 3103</ul> 3104<p>The <code>highWaterMark</code> of the underlying socket if assigned. Otherwise, the default 3105buffer level when <a href="stream.html#writablewritechunk-encoding-callback"><code>writable.write()</code></a> starts returning false (<code>16384</code>).</p> 3106<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> 3107<div class="api_metadata"> 3108<span>Added in: v12.9.0</span> 3109</div> 3110<ul> 3111<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 3112</ul> 3113<p>The number of buffered bytes.</p> 3114<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> 3115<div class="api_metadata"> 3116<span>Added in: v12.9.0</span> 3117</div> 3118<ul> 3119<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3120</ul> 3121<p>Always <code>false</code>.</p> 3122<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> 3123<div class="api_metadata"> 3124<details class="changelog"><summary>History</summary> 3125<table> 3126<tbody><tr><th>Version</th><th>Changes</th></tr> 3127<tr><td>v15.0.0</td> 3128<td><p>The <code>chunk</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 3129<tr><td>v0.11.6</td> 3130<td><p>The <code>callback</code> argument was added.</p></td></tr> 3131<tr><td>v0.1.29</td> 3132<td><p><span>Added in: v0.1.29</span></p></td></tr> 3133</tbody></table> 3134</details> 3135</div> 3136<ul> 3137<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> 3138<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> 3139<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 3140<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3141</ul> 3142<p>Sends a chunk of the body. This method can be called multiple times.</p> 3143<p>The <code>encoding</code> argument is only relevant when <code>chunk</code> is a string. Defaults to 3144<code>'utf8'</code>.</p> 3145<p>The <code>callback</code> argument is optional and will be called when this chunk of data 3146is flushed.</p> 3147<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel 3148buffer. Returns <code>false</code> if all or part of the data was queued in the user 3149memory. The <code>'drain'</code> event will be emitted when the buffer is free again.</p> 3150</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> 3151<div class="api_metadata"> 3152<span>Added in: v0.11.8</span> 3153</div> 3154<ul> 3155<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 3156</ul> 3157<p>A list of the HTTP methods that are supported by the parser.</p> 3158</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> 3159<div class="api_metadata"> 3160<span>Added in: v0.1.22</span> 3161</div> 3162<ul> 3163<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 3164</ul> 3165<p>A collection of all the standard HTTP response status codes, and the 3166short description of each. For example, <code>http.STATUS_CODES[404] === 'Not Found'</code>.</p> 3167</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> 3168<div class="api_metadata"> 3169<details class="changelog"><summary>History</summary> 3170<table> 3171<tbody><tr><th>Version</th><th>Changes</th></tr> 3172<tr><td>v18.17.0</td> 3173<td><p>The <code>highWaterMark</code> option is supported now.</p></td></tr> 3174<tr><td>v18.0.0</td> 3175<td><p>The <code>requestTimeout</code>, <code>headersTimeout</code>, <code>keepAliveTimeout</code>, and <code>connectionsCheckingInterval</code> options are supported now.</p></td></tr> 3176<tr><td>v18.0.0</td> 3177<td><p>The <code>noDelay</code> option now defaults to <code>true</code>.</p></td></tr> 3178<tr><td>v17.7.0</td> 3179<td><p>The <code>noDelay</code>, <code>keepAlive</code> and <code>keepAliveInitialDelay</code> options are supported now.</p></td></tr> 3180<tr><td>v13.3.0</td> 3181<td><p>The <code>maxHeaderSize</code> option is supported now.</p></td></tr> 3182<tr><td>v13.8.0, v12.15.0, v10.19.0</td> 3183<td><p>The <code>insecureHTTPParser</code> option is supported now.</p></td></tr> 3184<tr><td>v9.6.0, v8.12.0</td> 3185<td><p>The <code>options</code> argument is supported now.</p></td></tr> 3186<tr><td>v0.1.13</td> 3187<td><p><span>Added in: v0.1.13</span></p></td></tr> 3188</tbody></table> 3189</details> 3190</div> 3191<ul> 3192<li> 3193<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 3194<ul> 3195<li><code>connectionsCheckingInterval</code>: Sets the interval value in milliseconds to 3196check for request and headers timeout in incomplete requests. 3197<strong>Default:</strong> <code>30000</code>.</li> 3198<li><code>headersTimeout</code>: Sets the timeout value in milliseconds for receiving 3199the complete HTTP headers from the client. 3200See <a href="#serverheaderstimeout"><code>server.headersTimeout</code></a> for more information. 3201<strong>Default:</strong> <code>60000</code>.</li> 3202<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' 3203<code>readableHighWaterMark</code> and <code>writableHighWaterMark</code>. This affects 3204<code>highWaterMark</code> property of both <code>IncomingMessage</code> and <code>ServerResponse</code>. 3205<strong>Default:</strong> See <a href="stream.html#streamgetdefaulthighwatermarkobjectmode"><code>stream.getDefaultHighWaterMark()</code></a>.</li> 3206<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 3207invalid HTTP headers when <code>true</code>. Using the insecure parser should be 3208avoided. See <a href="cli.html#--insecure-http-parser"><code>--insecure-http-parser</code></a> for more information. 3209<strong>Default:</strong> <code>false</code>.</li> 3210<li><code>IncomingMessage</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a> Specifies the <code>IncomingMessage</code> 3211class to be used. Useful for extending the original <code>IncomingMessage</code>. 3212<strong>Default:</strong> <code>IncomingMessage</code>.</li> 3213<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 3214headers in a request with <code>, </code> instead of discarding the duplicates. 3215See <a href="#messageheaders"><code>message.headers</code></a> for more information. 3216<strong>Default:</strong> <code>false</code>.</li> 3217<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 3218on the socket immediately after a new incoming connection is received, 3219similarly on what is done in [<code>socket.setKeepAlive([enable][, initialDelay])</code>][<code>socket.setKeepAlive(enable, initialDelay)</code>]. 3220<strong>Default:</strong> <code>false</code>.</li> 3221<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 3222initial delay before the first keepalive probe is sent on an idle socket. 3223<strong>Default:</strong> <code>0</code>.</li> 3224<li><code>requestTimeout</code>: Sets the timeout value in milliseconds for receiving 3225the entire request from the client. 3226See <a href="#serverrequesttimeout"><code>server.requestTimeout</code></a> for more information. 3227<strong>Default:</strong> <code>300000</code>.</li> 3228<li><code>ServerResponse</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a> Specifies the <code>ServerResponse</code> class 3229to be used. Useful for extending the original <code>ServerResponse</code>. <strong>Default:</strong> 3230<code>ServerResponse</code>.</li> 3231<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 3232once. If the header's value is an array, the items will be joined 3233using <code>; </code>.</li> 3234</ul> 3235</li> 3236<li> 3237<p><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> 3238</li> 3239<li> 3240<p>Returns: <a href="http.html#class-httpserver" class="type"><http.Server></a></p> 3241</li> 3242</ul> 3243<p>Returns a new instance of <a href="#class-httpserver"><code>http.Server</code></a>.</p> 3244<p>The <code>requestListener</code> is a function which is automatically 3245added to the <a href="#event-request"><code>'request'</code></a> event.</p> 3246<pre><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>); 3247 3248<span class="hljs-comment">// Create a local server to receive data from</span> 3249<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> { 3250 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> }); 3251 res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 3252 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>, 3253 })); 3254}); 3255 3256server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre> 3257<pre><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>); 3258 3259<span class="hljs-comment">// Create a local server to receive data from</span> 3260<span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>(); 3261 3262<span class="hljs-comment">// Listen to the request event</span> 3263server.<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> { 3264 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> }); 3265 res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 3266 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>, 3267 })); 3268}); 3269 3270server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre> 3271</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> 3272</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> 3273<div class="api_metadata"> 3274<details class="changelog"><summary>History</summary> 3275<table> 3276<tbody><tr><th>Version</th><th>Changes</th></tr> 3277<tr><td>v10.9.0</td> 3278<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr> 3279<tr><td>v7.5.0</td> 3280<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr> 3281<tr><td>v0.3.6</td> 3282<td><p><span>Added in: v0.3.6</span></p></td></tr> 3283</tbody></table> 3284</details> 3285</div> 3286<ul> 3287<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> 3288<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 3289<a href="#httprequestoptions-callback"><code>http.request()</code></a>, with the method set to GET by default.</li> 3290<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 3291<li>Returns: <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 3292</ul> 3293<p>Since most requests are GET requests without bodies, Node.js provides this 3294convenience method. The only difference between this method and 3295<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> 3296automatically. The callback must take care to consume the response 3297data for reasons stated in <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a> section.</p> 3298<p>The <code>callback</code> is invoked with a single argument that is an instance of 3299<a href="#class-httpincomingmessage"><code>http.IncomingMessage</code></a>.</p> 3300<p>JSON fetching example:</p> 3301<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> { 3302 <span class="hljs-keyword">const</span> { statusCode } = res; 3303 <span class="hljs-keyword">const</span> contentType = res.<span class="hljs-property">headers</span>[<span class="hljs-string">'content-type'</span>]; 3304 3305 <span class="hljs-keyword">let</span> error; 3306 <span class="hljs-comment">// Any 2xx status code signals a successful response but</span> 3307 <span class="hljs-comment">// here we're only checking for 200.</span> 3308 <span class="hljs-keyword">if</span> (statusCode !== <span class="hljs-number">200</span>) { 3309 error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Request Failed.\n'</span> + 3310 <span class="hljs-string">`Status Code: <span class="hljs-subst">${statusCode}</span>`</span>); 3311 } <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)) { 3312 error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Invalid content-type.\n'</span> + 3313 <span class="hljs-string">`Expected application/json but received <span class="hljs-subst">${contentType}</span>`</span>); 3314 } 3315 <span class="hljs-keyword">if</span> (error) { 3316 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(error.<span class="hljs-property">message</span>); 3317 <span class="hljs-comment">// Consume response data to free up memory</span> 3318 res.<span class="hljs-title function_">resume</span>(); 3319 <span class="hljs-keyword">return</span>; 3320 } 3321 3322 res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>); 3323 <span class="hljs-keyword">let</span> rawData = <span class="hljs-string">''</span>; 3324 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; }); 3325 res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 3326 <span class="hljs-keyword">try</span> { 3327 <span class="hljs-keyword">const</span> parsedData = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(rawData); 3328 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(parsedData); 3329 } <span class="hljs-keyword">catch</span> (e) { 3330 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e.<span class="hljs-property">message</span>); 3331 } 3332 }); 3333}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 3334 <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>); 3335}); 3336 3337<span class="hljs-comment">// Create a local server to receive data from</span> 3338<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> { 3339 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> }); 3340 res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 3341 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>, 3342 })); 3343}); 3344 3345server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre> 3346</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> 3347<div class="api_metadata"> 3348<span>Added in: v0.5.9</span> 3349</div> 3350<ul> 3351<li><a href="http.html#class-httpagent" class="type"><http.Agent></a></li> 3352</ul> 3353<p>Global instance of <code>Agent</code> which is used as the default for all HTTP client 3354requests.</p> 3355</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> 3356<div class="api_metadata"> 3357<span>Added in: v11.6.0, v10.15.0</span> 3358</div> 3359<ul> 3360<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 3361</ul> 3362<p>Read-only property specifying the maximum allowed size of HTTP headers in bytes. 3363Defaults to 16 KiB. Configurable using the <a href="cli.html#--max-http-header-sizesize"><code>--max-http-header-size</code></a> CLI 3364option.</p> 3365<p>This can be overridden for servers and client requests by passing the 3366<code>maxHeaderSize</code> option.</p> 3367</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> 3368</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> 3369<div class="api_metadata"> 3370<details class="changelog"><summary>History</summary> 3371<table> 3372<tbody><tr><th>Version</th><th>Changes</th></tr> 3373<tr><td>v16.7.0, v14.18.0</td> 3374<td><p>When using a <code>URL</code> object parsed username and password will now be properly URI decoded.</p></td></tr> 3375<tr><td>v15.3.0, v14.17.0</td> 3376<td><p>It is possible to abort a request with an AbortSignal.</p></td></tr> 3377<tr><td>v13.3.0</td> 3378<td><p>The <code>maxHeaderSize</code> option is supported now.</p></td></tr> 3379<tr><td>v13.8.0, v12.15.0, v10.19.0</td> 3380<td><p>The <code>insecureHTTPParser</code> option is supported now.</p></td></tr> 3381<tr><td>v10.9.0</td> 3382<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr> 3383<tr><td>v7.5.0</td> 3384<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr> 3385<tr><td>v0.3.6</td> 3386<td><p><span>Added in: v0.3.6</span></p></td></tr> 3387</tbody></table> 3388</details> 3389</div> 3390<ul> 3391<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> 3392<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3393<ul> 3394<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 3395values: 3396<ul> 3397<li><code>undefined</code> (default): use <a href="#httpglobalagent"><code>http.globalAgent</code></a> for this host and port.</li> 3398<li><code>Agent</code> object: explicitly use the passed in <code>Agent</code>.</li> 3399<li><code>false</code>: causes a new <code>Agent</code> with default values to be used.</li> 3400</ul> 3401</li> 3402<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 3403Authorization header.</li> 3404<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 3405use for the request when the <code>agent</code> option is not used. This can be used to 3406avoid creating a custom <code>Agent</code> class just to override the default 3407<code>createConnection</code> function. See <a href="#agentcreateconnectionoptions-callback"><code>agent.createConnection()</code></a> for more 3408details. Any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream is a valid return value.</li> 3409<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> 3410<code>agent.defaultPort</code> if an <code>Agent</code> is used, else <code>undefined</code>.</li> 3411<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 3412<code>hostname</code>. Valid values are <code>4</code> or <code>6</code>. When unspecified, both IP v4 and 3413v6 will be used.</li> 3414<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> 3415<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> 3416<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 3417request to. <strong>Default:</strong> <code>'localhost'</code>.</li> 3418<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>, 3419<code>hostname</code> will be used if both <code>host</code> and <code>hostname</code> are specified.</li> 3420<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 3421invalid HTTP headers when <code>true</code>. Using the insecure parser should be 3422avoided. See <a href="cli.html#--insecure-http-parser"><code>--insecure-http-parser</code></a> for more information. 3423<strong>Default:</strong> <code>false</code></li> 3424<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 3425multiple headers in a request with <code>, </code> instead of discarding 3426the duplicates. See <a href="#messageheaders"><code>message.headers</code></a> for more information. 3427<strong>Default:</strong> <code>false</code>.</li> 3428<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> 3429<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> 3430<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> 3431<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 3432<a href="cli.html#--max-http-header-sizesize"><code>--max-http-header-size</code></a> (the maximum length of response headers in 3433bytes) for responses received from the server. 3434<strong>Default:</strong> 16384 (16 KiB).</li> 3435<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> 3436<code>'GET'</code>.</li> 3437<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. 3438E.G. <code>'/index.html?page=12'</code>. An exception is thrown when the request path 3439contains illegal characters. Currently, only spaces are rejected but that 3440may change in the future. <strong>Default:</strong> <code>'/'</code>.</li> 3441<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, 3442else <code>80</code>.</li> 3443<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> 3444<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 3445<code>Host</code> header. Defaults to <code>true</code>.</li> 3446<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a>: An AbortSignal that may be used to abort an ongoing 3447request.</li> 3448<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> 3449or <code>port</code> is specified, as those specify a TCP Socket.</li> 3450<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. 3451This will set the timeout before the socket is connected.</li> 3452<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 3453only once. If the header's value is an array, the items will be joined 3454using <code>; </code>.</li> 3455</ul> 3456</li> 3457<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 3458<li>Returns: <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 3459</ul> 3460<p><code>options</code> in <a href="net.html#socketconnectoptions-connectlistener"><code>socket.connect()</code></a> are also supported.</p> 3461<p>Node.js maintains several connections per server to make HTTP requests. 3462This function allows one to transparently issue requests.</p> 3463<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 3464string, 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> 3465object, it will be automatically converted to an ordinary <code>options</code> object.</p> 3466<p>If both <code>url</code> and <code>options</code> are specified, the objects are merged, with the 3467<code>options</code> properties taking precedence.</p> 3468<p>The optional <code>callback</code> parameter will be added as a one-time listener for 3469the <a href="#event-response"><code>'response'</code></a> event.</p> 3470<p><code>http.request()</code> returns an instance of the <a href="#class-httpclientrequest"><code>http.ClientRequest</code></a> 3471class. The <code>ClientRequest</code> instance is a writable stream. If one needs to 3472upload a file with a POST request, then write to the <code>ClientRequest</code> object.</p> 3473<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 3474 3475<span class="hljs-keyword">const</span> postData = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ 3476 <span class="hljs-string">'msg'</span>: <span class="hljs-string">'Hello World!'</span>, 3477}); 3478 3479<span class="hljs-keyword">const</span> options = { 3480 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'www.google.com'</span>, 3481 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>, 3482 <span class="hljs-attr">path</span>: <span class="hljs-string">'/upload'</span>, 3483 <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>, 3484 <span class="hljs-attr">headers</span>: { 3485 <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>, 3486 <span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(postData), 3487 }, 3488}; 3489 3490<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> { 3491 <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>); 3492 <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>); 3493 res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>); 3494 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> { 3495 <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>); 3496 }); 3497 res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 3498 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'No more data in response.'</span>); 3499 }); 3500}); 3501 3502req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 3503 <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>); 3504}); 3505 3506<span class="hljs-comment">// Write data to request body</span> 3507req.<span class="hljs-title function_">write</span>(postData); 3508req.<span class="hljs-title function_">end</span>();</code> <button class="copy-button">copy</button></pre> 3509<p>In the example <code>req.end()</code> was called. With <code>http.request()</code> one 3510must always call <code>req.end()</code> to signify the end of the request - 3511even if there is no data being written to the request body.</p> 3512<p>If any error is encountered during the request (be that with DNS resolution, 3513TCP level errors, or actual HTTP parse errors) an <code>'error'</code> event is emitted 3514on the returned request object. As with all <code>'error'</code> events, if no listeners 3515are registered the error will be thrown.</p> 3516<p>There are a few special headers that should be noted.</p> 3517<ul> 3518<li> 3519<p>Sending a 'Connection: keep-alive' will notify Node.js that the connection to 3520the server should be persisted until the next request.</p> 3521</li> 3522<li> 3523<p>Sending a 'Content-Length' header will disable the default chunked encoding.</p> 3524</li> 3525<li> 3526<p>Sending an 'Expect' header will immediately send the request headers. 3527Usually, when sending 'Expect: 100-continue', both a timeout and a listener 3528for the <code>'continue'</code> event should be set. See RFC 2616 Section 8.2.3 for more 3529information.</p> 3530</li> 3531<li> 3532<p>Sending an Authorization header will override using the <code>auth</code> option 3533to compute basic authentication.</p> 3534</li> 3535</ul> 3536<p>Example using a <a href="url.html#the-whatwg-url-api"><code>URL</code></a> as <code>options</code>:</p> 3537<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>); 3538 3539<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> { 3540 <span class="hljs-comment">// ...</span> 3541});</code> <button class="copy-button">copy</button></pre> 3542<p>In a successful request, the following events will be emitted in the following 3543order:</p> 3544<ul> 3545<li><code>'socket'</code></li> 3546<li><code>'response'</code> 3547<ul> 3548<li><code>'data'</code> any number of times, on the <code>res</code> object 3549(<code>'data'</code> will not be emitted at all if the response body is empty, for 3550instance, in most redirects)</li> 3551<li><code>'end'</code> on the <code>res</code> object</li> 3552</ul> 3553</li> 3554<li><code>'close'</code></li> 3555</ul> 3556<p>In the case of a connection error, the following events will be emitted:</p> 3557<ul> 3558<li><code>'socket'</code></li> 3559<li><code>'error'</code></li> 3560<li><code>'close'</code></li> 3561</ul> 3562<p>In the case of a premature connection close before the response is received, 3563the following events will be emitted in the following order:</p> 3564<ul> 3565<li><code>'socket'</code></li> 3566<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code 3567<code>'ECONNRESET'</code></li> 3568<li><code>'close'</code></li> 3569</ul> 3570<p>In the case of a premature connection close after the response is received, 3571the following events will be emitted in the following order:</p> 3572<ul> 3573<li><code>'socket'</code></li> 3574<li><code>'response'</code> 3575<ul> 3576<li><code>'data'</code> any number of times, on the <code>res</code> object</li> 3577</ul> 3578</li> 3579<li>(connection closed here)</li> 3580<li><code>'aborted'</code> on the <code>res</code> object</li> 3581<li><code>'error'</code> on the <code>res</code> object with an error with message 3582<code>'Error: aborted'</code> and code <code>'ECONNRESET'</code></li> 3583<li><code>'close'</code></li> 3584<li><code>'close'</code> on the <code>res</code> object</li> 3585</ul> 3586<p>If <code>req.destroy()</code> is called before a socket is assigned, the following 3587events will be emitted in the following order:</p> 3588<ul> 3589<li>(<code>req.destroy()</code> called here)</li> 3590<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code 3591<code>'ECONNRESET'</code>, or the error with which <code>req.destroy()</code> was called</li> 3592<li><code>'close'</code></li> 3593</ul> 3594<p>If <code>req.destroy()</code> is called before the connection succeeds, the following 3595events will be emitted in the following order:</p> 3596<ul> 3597<li><code>'socket'</code></li> 3598<li>(<code>req.destroy()</code> called here)</li> 3599<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code 3600<code>'ECONNRESET'</code>, or the error with which <code>req.destroy()</code> was called</li> 3601<li><code>'close'</code></li> 3602</ul> 3603<p>If <code>req.destroy()</code> is called after the response is received, the following 3604events will be emitted in the following order:</p> 3605<ul> 3606<li><code>'socket'</code></li> 3607<li><code>'response'</code> 3608<ul> 3609<li><code>'data'</code> any number of times, on the <code>res</code> object</li> 3610</ul> 3611</li> 3612<li>(<code>req.destroy()</code> called here)</li> 3613<li><code>'aborted'</code> on the <code>res</code> object</li> 3614<li><code>'error'</code> on the <code>res</code> object with an error with message <code>'Error: aborted'</code> 3615and code <code>'ECONNRESET'</code>, or the error with which <code>req.destroy()</code> was called</li> 3616<li><code>'close'</code></li> 3617<li><code>'close'</code> on the <code>res</code> object</li> 3618</ul> 3619<p>If <code>req.abort()</code> is called before a socket is assigned, the following 3620events will be emitted in the following order:</p> 3621<ul> 3622<li>(<code>req.abort()</code> called here)</li> 3623<li><code>'abort'</code></li> 3624<li><code>'close'</code></li> 3625</ul> 3626<p>If <code>req.abort()</code> is called before the connection succeeds, the following 3627events will be emitted in the following order:</p> 3628<ul> 3629<li><code>'socket'</code></li> 3630<li>(<code>req.abort()</code> called here)</li> 3631<li><code>'abort'</code></li> 3632<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code 3633<code>'ECONNRESET'</code></li> 3634<li><code>'close'</code></li> 3635</ul> 3636<p>If <code>req.abort()</code> is called after the response is received, the following 3637events will be emitted in the following order:</p> 3638<ul> 3639<li><code>'socket'</code></li> 3640<li><code>'response'</code> 3641<ul> 3642<li><code>'data'</code> any number of times, on the <code>res</code> object</li> 3643</ul> 3644</li> 3645<li>(<code>req.abort()</code> called here)</li> 3646<li><code>'abort'</code></li> 3647<li><code>'aborted'</code> on the <code>res</code> object</li> 3648<li><code>'error'</code> on the <code>res</code> object with an error with message 3649<code>'Error: aborted'</code> and code <code>'ECONNRESET'</code>.</li> 3650<li><code>'close'</code></li> 3651<li><code>'close'</code> on the <code>res</code> object</li> 3652</ul> 3653<p>Setting the <code>timeout</code> option or using the <code>setTimeout()</code> function will 3654not abort the request or do anything besides add a <code>'timeout'</code> event.</p> 3655<p>Passing an <code>AbortSignal</code> and then calling <code>abort()</code> on the corresponding 3656<code>AbortController</code> will behave the same way as calling <code>.destroy()</code> on the 3657request. Specifically, the <code>'error'</code> event will be emitted with an error with 3658the message <code>'AbortError: The operation was aborted'</code>, the code <code>'ABORT_ERR'</code> 3659and the <code>cause</code>, if one was provided.</p> 3660</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> 3661<div class="api_metadata"> 3662<details class="changelog"><summary>History</summary> 3663<table> 3664<tbody><tr><th>Version</th><th>Changes</th></tr> 3665<tr><td>v18.14.0</td> 3666<td><p>The <code>label</code> parameter is added.</p></td></tr> 3667<tr><td>v14.3.0</td> 3668<td><p><span>Added in: v14.3.0</span></p></td></tr> 3669</tbody></table> 3670</details> 3671</div> 3672<ul> 3673<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3674<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> 3675</ul> 3676<p>Performs the low-level validations on the provided <code>name</code> that are done when 3677<code>res.setHeader(name, value)</code> is called.</p> 3678<p>Passing illegal value as <code>name</code> will result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown, 3679identified by <code>code: 'ERR_INVALID_HTTP_TOKEN'</code>.</p> 3680<p>It is not necessary to use this method before passing headers to an HTTP request 3681or response. The HTTP module will automatically validate such headers. 3682Examples:</p> 3683<p>Example:</p> 3684<pre><code class="language-js"><span class="hljs-keyword">const</span> { validateHeaderName } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 3685 3686<span class="hljs-keyword">try</span> { 3687 <span class="hljs-title function_">validateHeaderName</span>(<span class="hljs-string">''</span>); 3688} <span class="hljs-keyword">catch</span> (err) { 3689 <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> 3690 <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> 3691 <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> 3692}</code> <button class="copy-button">copy</button></pre> 3693</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> 3694<div class="api_metadata"> 3695<span>Added in: v14.3.0</span> 3696</div> 3697<ul> 3698<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3699<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3700</ul> 3701<p>Performs the low-level validations on the provided <code>value</code> that are done when 3702<code>res.setHeader(name, value)</code> is called.</p> 3703<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> 3704<ul> 3705<li>Undefined value error is identified by <code>code: 'ERR_HTTP_INVALID_HEADER_VALUE'</code>.</li> 3706<li>Invalid value character error is identified by <code>code: 'ERR_INVALID_CHAR'</code>.</li> 3707</ul> 3708<p>It is not necessary to use this method before passing headers to an HTTP request 3709or response. The HTTP module will automatically validate such headers.</p> 3710<p>Examples:</p> 3711<pre><code class="language-js"><span class="hljs-keyword">const</span> { validateHeaderValue } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 3712 3713<span class="hljs-keyword">try</span> { 3714 <span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-literal">undefined</span>); 3715} <span class="hljs-keyword">catch</span> (err) { 3716 <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> 3717 <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> 3718 <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> 3719} 3720 3721<span class="hljs-keyword">try</span> { 3722 <span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-string">'oʊmɪɡə'</span>); 3723} <span class="hljs-keyword">catch</span> (err) { 3724 <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> 3725 <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> 3726 <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> 3727}</code> <button class="copy-button">copy</button></pre> 3728</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> 3729<div class="api_metadata"> 3730<span>Added in: v18.8.0</span> 3731</div> 3732<ul> 3733<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> 3734</ul> 3735<p>Set the maximum number of idle HTTP parsers.</p></section> 3736 <!-- API END --> 3737 </div> 3738 </div> 3739 </div> 3740</body> 3741</html> 3742