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