• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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="v14.21.2">
7  <title>HTTP | Node.js v14.21.2 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/http.html">
12</head>
13<body class="alt apidoc" id="api-section-http">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http active">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="http" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.21.2 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <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"/>
97              <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"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <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"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="http.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v19.x/api/http.html">19.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v18.x/api/http.html">18.x <b>LTS</b></a></li>
121<li><a href="https://nodejs.org/docs/latest-v17.x/api/http.html">17.x</a></li>
122<li><a href="https://nodejs.org/docs/latest-v16.x/api/http.html">16.x <b>LTS</b></a></li>
123<li><a href="https://nodejs.org/docs/latest-v15.x/api/http.html">15.x</a></li>
124<li><a href="https://nodejs.org/docs/latest-v14.x/api/http.html">14.x <b>LTS</b></a></li>
125<li><a href="https://nodejs.org/docs/latest-v13.x/api/http.html">13.x</a></li>
126<li><a href="https://nodejs.org/docs/latest-v12.x/api/http.html">12.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v11.x/api/http.html">11.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v10.x/api/http.html">10.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v9.x/api/http.html">9.x</a></li>
130<li><a href="https://nodejs.org/docs/latest-v8.x/api/http.html">8.x</a></li>
131<li><a href="https://nodejs.org/docs/latest-v7.x/api/http.html">7.x</a></li>
132<li><a href="https://nodejs.org/docs/latest-v6.x/api/http.html">6.x</a></li>
133<li><a href="https://nodejs.org/docs/latest-v5.x/api/http.html">5.x</a></li>
134<li><a href="https://nodejs.org/docs/latest-v4.x/api/http.html">4.x</a></li>
135<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/http.html">0.12.x</a></li>
136<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/http.html">0.10.x</a></li></ol>
137    </li>
138
139            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/http.md">Edit on GitHub</a></li>
140          </ul>
141        </div>
142        <hr>
143      </header>
144
145      <details id="toc" open><summary>Table of contents</summary><ul>
146<li><span class="stability_2"><a href="#http_http">HTTP</a></span>
147<ul>
148<li><a href="#http_class_http_agent">Class: <code>http.Agent</code></a>
149<ul>
150<li><a href="#http_new_agent_options"><code>new Agent([options])</code></a></li>
151<li><a href="#http_agent_createconnection_options_callback"><code>agent.createConnection(options[, callback])</code></a></li>
152<li><a href="#http_agent_keepsocketalive_socket"><code>agent.keepSocketAlive(socket)</code></a></li>
153<li><a href="#http_agent_reusesocket_socket_request"><code>agent.reuseSocket(socket, request)</code></a></li>
154<li><a href="#http_agent_destroy"><code>agent.destroy()</code></a></li>
155<li><a href="#http_agent_freesockets"><code>agent.freeSockets</code></a></li>
156<li><a href="#http_agent_getname_options"><code>agent.getName(options)</code></a></li>
157<li><a href="#http_agent_maxfreesockets"><code>agent.maxFreeSockets</code></a></li>
158<li><a href="#http_agent_maxsockets"><code>agent.maxSockets</code></a></li>
159<li><a href="#http_agent_maxtotalsockets"><code>agent.maxTotalSockets</code></a></li>
160<li><a href="#http_agent_requests"><code>agent.requests</code></a></li>
161<li><a href="#http_agent_sockets"><code>agent.sockets</code></a></li>
162</ul>
163</li>
164<li><a href="#http_class_http_clientrequest">Class: <code>http.ClientRequest</code></a>
165<ul>
166<li><a href="#http_event_abort">Event: <code>'abort'</code></a></li>
167<li><a href="#http_event_connect">Event: <code>'connect'</code></a></li>
168<li><a href="#http_event_continue">Event: <code>'continue'</code></a></li>
169<li><a href="#http_event_information">Event: <code>'information'</code></a></li>
170<li><a href="#http_event_response">Event: <code>'response'</code></a></li>
171<li><a href="#http_event_socket">Event: <code>'socket'</code></a></li>
172<li><a href="#http_event_timeout">Event: <code>'timeout'</code></a></li>
173<li><a href="#http_event_upgrade">Event: <code>'upgrade'</code></a></li>
174<li><span class="stability_0"><a href="#http_request_abort"><code>request.abort()</code></a></span></li>
175<li><a href="#http_request_aborted"><code>request.aborted</code></a></li>
176<li><span class="stability_0"><a href="#http_request_connection"><code>request.connection</code></a></span></li>
177<li><a href="#http_request_end_data_encoding_callback"><code>request.end([data[, encoding]][, callback])</code></a></li>
178<li><a href="#http_request_destroy_error"><code>request.destroy([error])</code></a>
179<ul>
180<li><a href="#http_request_destroyed"><code>request.destroyed</code></a></li>
181</ul>
182</li>
183<li><span class="stability_0"><a href="#http_request_finished"><code>request.finished</code></a></span></li>
184<li><a href="#http_request_flushheaders"><code>request.flushHeaders()</code></a></li>
185<li><a href="#http_request_getheader_name"><code>request.getHeader(name)</code></a></li>
186<li><a href="#http_request_getrawheadernames"><code>request.getRawHeaderNames()</code></a></li>
187<li><a href="#http_request_maxheaderscount"><code>request.maxHeadersCount</code></a></li>
188<li><a href="#http_request_path"><code>request.path</code></a></li>
189<li><a href="#http_request_method"><code>request.method</code></a></li>
190<li><a href="#http_request_host"><code>request.host</code></a></li>
191<li><a href="#http_request_protocol"><code>request.protocol</code></a></li>
192<li><a href="#http_request_removeheader_name"><code>request.removeHeader(name)</code></a></li>
193<li><a href="#http_request_reusedsocket"><code>request.reusedSocket</code></a></li>
194<li><a href="#http_request_setheader_name_value"><code>request.setHeader(name, value)</code></a></li>
195<li><a href="#http_request_setnodelay_nodelay"><code>request.setNoDelay([noDelay])</code></a></li>
196<li><a href="#http_request_setsocketkeepalive_enable_initialdelay"><code>request.setSocketKeepAlive([enable][, initialDelay])</code></a></li>
197<li><a href="#http_request_settimeout_timeout_callback"><code>request.setTimeout(timeout[, callback])</code></a></li>
198<li><a href="#http_request_socket"><code>request.socket</code></a></li>
199<li><a href="#http_request_writableended"><code>request.writableEnded</code></a></li>
200<li><a href="#http_request_writablefinished"><code>request.writableFinished</code></a></li>
201<li><a href="#http_request_write_chunk_encoding_callback"><code>request.write(chunk[, encoding][, callback])</code></a></li>
202</ul>
203</li>
204<li><a href="#http_class_http_server">Class: <code>http.Server</code></a>
205<ul>
206<li><a href="#http_event_checkcontinue">Event: <code>'checkContinue'</code></a></li>
207<li><a href="#http_event_checkexpectation">Event: <code>'checkExpectation'</code></a></li>
208<li><a href="#http_event_clienterror">Event: <code>'clientError'</code></a></li>
209<li><a href="#http_event_close">Event: <code>'close'</code></a></li>
210<li><a href="#http_event_connect_1">Event: <code>'connect'</code></a></li>
211<li><a href="#http_event_connection">Event: <code>'connection'</code></a></li>
212<li><a href="#http_event_request">Event: <code>'request'</code></a></li>
213<li><a href="#http_event_upgrade_1">Event: <code>'upgrade'</code></a></li>
214<li><a href="#http_server_close_callback"><code>server.close([callback])</code></a></li>
215<li><a href="#http_server_headerstimeout"><code>server.headersTimeout</code></a></li>
216<li><a href="#http_server_listen"><code>server.listen()</code></a></li>
217<li><a href="#http_server_listening"><code>server.listening</code></a></li>
218<li><a href="#http_server_maxheaderscount"><code>server.maxHeadersCount</code></a></li>
219<li><a href="#http_server_requesttimeout"><code>server.requestTimeout</code></a></li>
220<li><a href="#http_server_settimeout_msecs_callback"><code>server.setTimeout([msecs][, callback])</code></a></li>
221<li><a href="#http_server_timeout"><code>server.timeout</code></a></li>
222<li><a href="#http_server_keepalivetimeout"><code>server.keepAliveTimeout</code></a></li>
223</ul>
224</li>
225<li><a href="#http_class_http_serverresponse">Class: <code>http.ServerResponse</code></a>
226<ul>
227<li><a href="#http_event_close_1">Event: <code>'close'</code></a></li>
228<li><a href="#http_event_finish">Event: <code>'finish'</code></a></li>
229<li><a href="#http_response_addtrailers_headers"><code>response.addTrailers(headers)</code></a></li>
230<li><span class="stability_0"><a href="#http_response_connection"><code>response.connection</code></a></span></li>
231<li><a href="#http_response_cork"><code>response.cork()</code></a></li>
232<li><a href="#http_response_end_data_encoding_callback"><code>response.end([data[, encoding]][, callback])</code></a></li>
233<li><span class="stability_0"><a href="#http_response_finished"><code>response.finished</code></a></span></li>
234<li><a href="#http_response_flushheaders"><code>response.flushHeaders()</code></a></li>
235<li><a href="#http_response_getheader_name"><code>response.getHeader(name)</code></a></li>
236<li><a href="#http_response_getheadernames"><code>response.getHeaderNames()</code></a></li>
237<li><a href="#http_response_getheaders"><code>response.getHeaders()</code></a></li>
238<li><a href="#http_response_hasheader_name"><code>response.hasHeader(name)</code></a></li>
239<li><a href="#http_response_headerssent"><code>response.headersSent</code></a></li>
240<li><a href="#http_response_removeheader_name"><code>response.removeHeader(name)</code></a></li>
241<li><a href="#http_response_senddate"><code>response.sendDate</code></a></li>
242<li><a href="#http_response_setheader_name_value"><code>response.setHeader(name, value)</code></a></li>
243<li><a href="#http_response_settimeout_msecs_callback"><code>response.setTimeout(msecs[, callback])</code></a></li>
244<li><a href="#http_response_socket"><code>response.socket</code></a></li>
245<li><a href="#http_response_statuscode"><code>response.statusCode</code></a></li>
246<li><a href="#http_response_statusmessage"><code>response.statusMessage</code></a></li>
247<li><a href="#http_response_uncork"><code>response.uncork()</code></a></li>
248<li><a href="#http_response_writableended"><code>response.writableEnded</code></a></li>
249<li><a href="#http_response_writablefinished"><code>response.writableFinished</code></a></li>
250<li><a href="#http_response_write_chunk_encoding_callback"><code>response.write(chunk[, encoding][, callback])</code></a></li>
251<li><a href="#http_response_writecontinue"><code>response.writeContinue()</code></a></li>
252<li><a href="#http_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li>
253<li><a href="#http_response_writeprocessing"><code>response.writeProcessing()</code></a></li>
254</ul>
255</li>
256<li><a href="#http_class_http_incomingmessage">Class: <code>http.IncomingMessage</code></a>
257<ul>
258<li><a href="#http_event_aborted">Event: <code>'aborted'</code></a></li>
259<li><a href="#http_event_close_2">Event: <code>'close'</code></a></li>
260<li><a href="#http_message_aborted"><code>message.aborted</code></a></li>
261<li><a href="#http_message_complete"><code>message.complete</code></a></li>
262<li><a href="#http_message_destroy_error"><code>message.destroy([error])</code></a></li>
263<li><a href="#http_message_headers"><code>message.headers</code></a></li>
264<li><a href="#http_message_httpversion"><code>message.httpVersion</code></a></li>
265<li><a href="#http_message_method"><code>message.method</code></a></li>
266<li><a href="#http_message_rawheaders"><code>message.rawHeaders</code></a></li>
267<li><a href="#http_message_rawtrailers"><code>message.rawTrailers</code></a></li>
268<li><a href="#http_message_settimeout_msecs_callback"><code>message.setTimeout(msecs[, callback])</code></a></li>
269<li><a href="#http_message_socket"><code>message.socket</code></a></li>
270<li><a href="#http_message_statuscode"><code>message.statusCode</code></a></li>
271<li><a href="#http_message_statusmessage"><code>message.statusMessage</code></a></li>
272<li><a href="#http_message_trailers"><code>message.trailers</code></a></li>
273<li><a href="#http_message_url"><code>message.url</code></a></li>
274</ul>
275</li>
276<li><a href="#http_class_http_outgoingmessage">Class: <code>http.OutgoingMessage</code></a>
277<ul>
278<li><a href="#http_event_drain">Event: <code>drain</code></a></li>
279<li><a href="#http_event_finish_1">Event: <code>finish</code></a></li>
280<li><a href="#http_event_prefinish">Event: <code>prefinish</code></a></li>
281<li><a href="#http_outgoingmessage_addtrailers_headers"><code>outgoingMessage.addTrailers(headers)</code></a></li>
282<li><span class="stability_0"><a href="#http_outgoingmessage_connection"><code>outgoingMessage.connection</code></a></span></li>
283<li><a href="#http_outgoingmessage_cork"><code>outgoingMessage.cork()</code></a></li>
284<li><a href="#http_outgoingmessage_destroy_error"><code>outgoingMessage.destroy([error])</code></a></li>
285<li><a href="#http_outgoingmessage_end_chunk_encoding_callback"><code>outgoingMessage.end(chunk[, encoding][, callback])</code></a></li>
286<li><a href="#http_outgoingmessage_flushheaders"><code>outgoingMessage.flushHeaders()</code></a></li>
287<li><a href="#http_outgoingmessage_getheader_name"><code>outgoingMessage.getHeader(name)</code></a></li>
288<li><a href="#http_outgoingmessage_getheadernames"><code>outgoingMessage.getHeaderNames()</code></a></li>
289<li><a href="#http_outgoingmessage_getheaders"><code>outgoingMessage.getHeaders()</code></a></li>
290<li><a href="#http_outgoingmessage_hasheader_name"><code>outgoingMessage.hasHeader(name)</code></a></li>
291<li><a href="#http_outgoingmessage_headerssent"><code>outgoingMessage.headersSent</code></a></li>
292<li><a href="#http_outgoingmessage_pipe"><code>outgoingMessage.pipe()</code></a></li>
293<li><a href="#http_outgoingmessage_removeheader"><code>outgoingMessage.removeHeader()</code></a></li>
294<li><a href="#http_outgoingmessage_setheader_name_value"><code>outgoingMessage.setHeader(name, value)</code></a></li>
295<li><a href="#http_outgoingmessage_settimeout_msesc_callback"><code>outgoingMessage.setTimeout(msesc[, callback])</code></a></li>
296<li><a href="#http_outgoingmessage_socket"><code>outgoingMessage.socket</code></a></li>
297<li><a href="#http_outgoingmessage_uncork"><code>outgoingMessage.uncork()</code></a></li>
298<li><a href="#http_outgoingmessage_writablecorked"><code>outgoingMessage.writableCorked</code></a></li>
299<li><a href="#http_outgoingmessage_writableended"><code>outgoingMessage.writableEnded</code></a></li>
300<li><a href="#http_outgoingmessage_writablefinished"><code>outgoingMessage.writableFinished</code></a></li>
301<li><a href="#http_outgoingmessage_writablehighwatermark"><code>outgoingMessage.writableHighWaterMark</code></a></li>
302<li><a href="#http_outgoingmessage_writablelength"><code>outgoingMessage.writableLength</code></a></li>
303<li><a href="#http_outgoingmessage_writableobjectmode"><code>outgoingMessage.writableObjectMode</code></a></li>
304<li><a href="#http_outgoingmessage_write_chunk_encoding_callback"><code>outgoingMessage.write(chunk[, encoding][, callback])</code></a></li>
305</ul>
306</li>
307<li><a href="#http_http_methods"><code>http.METHODS</code></a></li>
308<li><a href="#http_http_status_codes"><code>http.STATUS_CODES</code></a></li>
309<li><a href="#http_http_createserver_options_requestlistener"><code>http.createServer([options][, requestListener])</code></a></li>
310<li><a href="#http_http_get_options_callback"><code>http.get(options[, callback])</code></a></li>
311<li><a href="#http_http_get_url_options_callback"><code>http.get(url[, options][, callback])</code></a></li>
312<li><a href="#http_http_globalagent"><code>http.globalAgent</code></a></li>
313<li><a href="#http_http_maxheadersize"><code>http.maxHeaderSize</code></a></li>
314<li><a href="#http_http_request_options_callback"><code>http.request(options[, callback])</code></a></li>
315<li><a href="#http_http_request_url_options_callback"><code>http.request(url[, options][, callback])</code></a></li>
316<li><a href="#http_http_validateheadername_name"><code>http.validateHeaderName(name)</code></a></li>
317<li><a href="#http_http_validateheadervalue_name_value"><code>http.validateHeaderValue(name, value)</code></a></li>
318</ul>
319</li>
320</ul></details>
321
322      <div id="apicontent">
323        <h2>HTTP<span><a class="mark" href="#http_http" id="http_http">#</a></span></h2>
324
325<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
326<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.21.2/lib/http.js">lib/http.js</a></p>
327<p>To use the HTTP server and client one must <code>require('http')</code>.</p>
328<p>The HTTP interfaces in Node.js are designed to support many features
329of the protocol which have been traditionally difficult to use.
330In particular, large, possibly chunk-encoded, messages. The interface is
331careful to never buffer entire requests or responses, so the
332user is able to stream data.</p>
333<p>HTTP message headers are represented by an object like this:</p>
334<!-- eslint-skip -->
335<pre><code class="language-js">{ <span class="hljs-string">'content-length'</span>: <span class="hljs-string">'123'</span>,
336  <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain'</span>,
337  <span class="hljs-string">'connection'</span>: <span class="hljs-string">'keep-alive'</span>,
338  <span class="hljs-string">'host'</span>: <span class="hljs-string">'mysite.com'</span>,
339  <span class="hljs-string">'accept'</span>: <span class="hljs-string">'*/*'</span> }</code></pre>
340<p>Keys are lowercased. Values are not modified.</p>
341<p>In order to support the full spectrum of possible HTTP applications, the Node.js
342HTTP API is very low-level. It deals with stream handling and message
343parsing only. It parses a message into headers and body but it does not
344parse the actual headers or the body.</p>
345<p>See <a href="#http_message_headers"><code>message.headers</code></a> for details on how duplicate headers are handled.</p>
346<p>The raw headers as they were received are retained in the <code>rawHeaders</code>
347property, which is an array of <code>[key, value, key2, value2, ...]</code>. For
348example, the previous message header object might have a <code>rawHeaders</code>
349list like the following:</p>
350<!-- eslint-disable semi -->
351<pre><code class="language-js">[ <span class="hljs-string">'ConTent-Length'</span>, <span class="hljs-string">'123456'</span>,
352  <span class="hljs-string">'content-LENGTH'</span>, <span class="hljs-string">'123'</span>,
353  <span class="hljs-string">'content-type'</span>, <span class="hljs-string">'text/plain'</span>,
354  <span class="hljs-string">'CONNECTION'</span>, <span class="hljs-string">'keep-alive'</span>,
355  <span class="hljs-string">'Host'</span>, <span class="hljs-string">'mysite.com'</span>,
356  <span class="hljs-string">'accepT'</span>, <span class="hljs-string">'*/*'</span> ]</code></pre>
357<section><h3>Class: <code>http.Agent</code><span><a class="mark" href="#http_class_http_agent" id="http_class_http_agent">#</a></span></h3>
358<div class="api_metadata">
359<span>Added in: v0.3.4</span>
360</div>
361<p>An <code>Agent</code> is responsible for managing connection persistence
362and reuse for HTTP clients. It maintains a queue of pending requests
363for a given host and port, reusing a single socket connection for each
364until the queue is empty, at which time the socket is either destroyed
365or put into a pool where it is kept to be used again for requests to the
366same host and port. Whether it is destroyed or pooled depends on the
367<code>keepAlive</code> <a href="#http_new_agent_options">option</a>.</p>
368<p>Pooled connections have TCP Keep-Alive enabled for them, but servers may
369still close idle connections, in which case they will be removed from the
370pool and a new connection will be made when a new HTTP request is made for
371that host and port. Servers may also refuse to allow multiple requests
372over the same connection, in which case the connection will have to be
373remade for every request and cannot be pooled. The <code>Agent</code> will still make
374the requests to that server, but each one will occur over a new connection.</p>
375<p>When a connection is closed by the client or the server, it is removed
376from the pool. Any unused sockets in the pool will be unrefed so as not
377to keep the Node.js process running when there are no outstanding requests.
378(see <a href="net.html#net_socket_unref"><code>socket.unref()</code></a>).</p>
379<p>It is good practice, to <a href="#http_agent_destroy"><code>destroy()</code></a> an <code>Agent</code> instance when it is no
380longer in use, because unused sockets consume OS resources.</p>
381<p>Sockets are removed from an agent when the socket emits either
382a <code>'close'</code> event or an <code>'agentRemove'</code> event. When intending to keep one
383HTTP request open for a long time without keeping it in the agent, something
384like the following may be done:</p>
385<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> {
386  <span class="hljs-comment">// Do stuff</span>
387}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'socket'</span>, <span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
388  socket.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'agentRemove'</span>);
389});</code></pre>
390<p>An agent may also be used for an individual request. By providing
391<code>{agent: false}</code> as an option to the <code>http.get()</code> or <code>http.request()</code>
392functions, a one-time use <code>Agent</code> with default options will be used
393for the client connection.</p>
394<p><code>agent:false</code>:</p>
395<pre><code class="language-js">http.<span class="hljs-title function_">get</span>({
396  <span class="hljs-attr">hostname</span>: <span class="hljs-string">'localhost'</span>,
397  <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
398  <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
399  <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>
400}, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
401  <span class="hljs-comment">// Do stuff with response</span>
402});</code></pre>
403<h4><code>new Agent([options])</code><span><a class="mark" href="#http_new_agent_options" id="http_new_agent_options">#</a></span></h4>
404<div class="api_metadata">
405<details class="changelog"><summary>History</summary>
406<table>
407<tbody><tr><th>Version</th><th>Changes</th></tr>
408<tr><td>v14.17.0</td>
409<td><p>Change the default scheduling from 'fifo' to 'lifo'.</p></td></tr>
410<tr><td>v14.5.0</td>
411<td><p>Add <code>maxTotalSockets</code> option to agent constructor.</p></td></tr>
412<tr><td>v14.5.0</td>
413<td><p>Add <code>scheduling</code> option to specify the free socket scheduling strategy.</p></td></tr>
414<tr><td>v0.3.4</td>
415<td><p><span>Added in: v0.3.4</span></p></td></tr>
416</tbody></table>
417</details>
418</div>
419<ul>
420<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.
421Can have the following fields:
422<ul>
423<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
424outstanding requests, so they can be used for future requests without
425having to reestablish a TCP connection. Not to be confused with the
426<code>keep-alive</code> value of the <code>Connection</code> header. The <code>Connection: keep-alive</code>
427header is always sent when using an agent except when the <code>Connection</code>
428header is explicitly specified or when the <code>keepAlive</code> and <code>maxSockets</code>
429options are respectively set to <code>false</code> and <code>Infinity</code>, in which case
430<code>Connection: close</code> will be used. <strong>Default:</strong> <code>false</code>.</li>
431<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
432the <a href="net.html#net_socket_setkeepalive_enable_initialdelay">initial delay</a>
433for TCP Keep-Alive packets. Ignored when the
434<code>keepAlive</code> option is <code>false</code> or <code>undefined</code>. <strong>Default:</strong> <code>1000</code>.</li>
435<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.
436If the same host opens multiple concurrent connections, each request
437will use new socket until the <code>maxSockets</code> value is reached.
438If the host attempts to open more connections than <code>maxSockets</code>,
439the additional requests will enter into a pending request queue, and
440will enter active connection state when an existing connection terminates.
441This makes sure there are at most <code>maxSockets</code> active connections at
442any point in time, from a given host.
443<strong>Default:</strong> <code>Infinity</code>.</li>
444<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
445all hosts in total. Each request will use a new socket
446until the maximum is reached.
447<strong>Default:</strong> <code>Infinity</code>.</li>
448<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 to leave open
449in a free state. Only relevant if <code>keepAlive</code> is set to <code>true</code>.
450<strong>Default:</strong> <code>256</code>.</li>
451<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
452the next free socket to use. It can be <code>'fifo'</code> or <code>'lifo'</code>.
453The main difference between the two scheduling strategies is that <code>'lifo'</code>
454selects the most recently used socket, while <code>'fifo'</code> selects
455the least recently used socket.
456In case of a low rate of request per second, the <code>'lifo'</code> scheduling
457will lower the risk of picking a socket that might have been closed
458by the server due to inactivity.
459In case of a high rate of request per second,
460the <code>'fifo'</code> scheduling will maximize the number of open sockets,
461while the <code>'lifo'</code> scheduling will keep it as low as possible.
462<strong>Default:</strong> <code>'lifo'</code>.</li>
463<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.
464This will set the timeout when the socket is created.</li>
465</ul>
466</li>
467</ul>
468<p><code>options</code> in <a href="net.html#net_socket_connect_options_connectlistener"><code>socket.connect()</code></a> are also supported.</p>
469<p>The default <a href="#http_http_globalagent"><code>http.globalAgent</code></a> that is used by <a href="#http_http_request_options_callback"><code>http.request()</code></a> has all
470of these values set to their respective defaults.</p>
471<p>To configure any of them, a custom <a href="#http_class_http_agent"><code>http.Agent</code></a> instance must be created.</p>
472<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
473<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> });
474options.<span class="hljs-property">agent</span> = keepAliveAgent;
475http.<span class="hljs-title function_">request</span>(options, onResponseCallback);</code></pre>
476<h4><code>agent.createConnection(options[, callback])</code><span><a class="mark" href="#http_agent_createconnection_options_callback" id="http_agent_createconnection_options_callback">#</a></span></h4>
477<div class="api_metadata">
478<span>Added in: v0.11.4</span>
479</div>
480<ul>
481<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
482<a href="net.html#net_net_createconnection_options_connectlistener"><code>net.createConnection()</code></a> for the format of the options</li>
483<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>
484<li>Returns: <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
485</ul>
486<p>Produces a socket/stream to be used for HTTP requests.</p>
487<p>By default, this function is the same as <a href="net.html#net_net_createconnection_options_connectlistener"><code>net.createConnection()</code></a>. However,
488custom agents may override this method in case greater flexibility is desired.</p>
489<p>A socket/stream can be supplied in one of two ways: by returning the
490socket/stream from this function, or by passing the socket/stream to <code>callback</code>.</p>
491<p>This method is guaranteed to return an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
492a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specifies a socket
493type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
494<p><code>callback</code> has a signature of <code>(err, stream)</code>.</p>
495<h4><code>agent.keepSocketAlive(socket)</code><span><a class="mark" href="#http_agent_keepsocketalive_socket" id="http_agent_keepsocketalive_socket">#</a></span></h4>
496<div class="api_metadata">
497<span>Added in: v8.1.0</span>
498</div>
499<ul>
500<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
501</ul>
502<p>Called when <code>socket</code> is detached from a request and could be persisted by the
503<code>Agent</code>. Default behavior is to:</p>
504<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>);
505socket.<span class="hljs-title function_">unref</span>();
506<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;</code></pre>
507<p>This method can be overridden by a particular <code>Agent</code> subclass. If this
508method returns a falsy value, the socket will be destroyed instead of persisting
509it for use with the next request.</p>
510<p>The <code>socket</code> argument can be an instance of <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>, a subclass of
511<a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>.</p>
512<h4><code>agent.reuseSocket(socket, request)</code><span><a class="mark" href="#http_agent_reusesocket_socket_request" id="http_agent_reusesocket_socket_request">#</a></span></h4>
513<div class="api_metadata">
514<span>Added in: v8.1.0</span>
515</div>
516<ul>
517<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
518<li><code>request</code> <a href="http.html#http_class_http_clientrequest" class="type">&#x3C;http.ClientRequest></a></li>
519</ul>
520<p>Called when <code>socket</code> is attached to <code>request</code> after being persisted because of
521the keep-alive options. Default behavior is to:</p>
522<pre><code class="language-js">socket.<span class="hljs-title function_">ref</span>();</code></pre>
523<p>This method can be overridden by a particular <code>Agent</code> subclass.</p>
524<p>The <code>socket</code> argument can be an instance of <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>, a subclass of
525<a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>.</p>
526<h4><code>agent.destroy()</code><span><a class="mark" href="#http_agent_destroy" id="http_agent_destroy">#</a></span></h4>
527<div class="api_metadata">
528<span>Added in: v0.11.4</span>
529</div>
530<p>Destroy any sockets that are currently in use by the agent.</p>
531<p>It is usually not necessary to do this. However, if using an
532agent with <code>keepAlive</code> enabled, then it is best to explicitly shut down
533the agent when it is no longer needed. Otherwise,
534sockets might stay open for quite a long time before the server
535terminates them.</p>
536<h4><code>agent.freeSockets</code><span><a class="mark" href="#http_agent_freesockets" id="http_agent_freesockets">#</a></span></h4>
537<div class="api_metadata">
538<span>Added in: v0.11.4</span>
539</div>
540<ul>
541<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
542</ul>
543<p>An object which contains arrays of sockets currently awaiting use by
544the agent when <code>keepAlive</code> is enabled. Do not modify.</p>
545<p>Sockets in the <code>freeSockets</code> list will be automatically destroyed and
546removed from the array on <code>'timeout'</code>.</p>
547<h4><code>agent.getName(options)</code><span><a class="mark" href="#http_agent_getname_options" id="http_agent_getname_options">#</a></span></h4>
548<div class="api_metadata">
549<span>Added in: v0.11.4</span>
550</div>
551<ul>
552<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
553<ul>
554<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
555request to</li>
556<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>
557<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
558when issuing the request</li>
559<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>
560</ul>
561</li>
562<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
563</ul>
564<p>Get a unique name for a set of request options, to determine whether a
565connection can be reused. For an HTTP agent, this returns
566<code>host:port:localAddress</code> or <code>host:port:localAddress:family</code>. For an HTTPS agent,
567the name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options
568that determine socket reusability.</p>
569<h4><code>agent.maxFreeSockets</code><span><a class="mark" href="#http_agent_maxfreesockets" id="http_agent_maxfreesockets">#</a></span></h4>
570<div class="api_metadata">
571<span>Added in: v0.11.7</span>
572</div>
573<ul>
574<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
575</ul>
576<p>By default set to 256. For agents with <code>keepAlive</code> enabled, this
577sets the maximum number of sockets that will be left open in the free
578state.</p>
579<h4><code>agent.maxSockets</code><span><a class="mark" href="#http_agent_maxsockets" id="http_agent_maxsockets">#</a></span></h4>
580<div class="api_metadata">
581<span>Added in: v0.3.6</span>
582</div>
583<ul>
584<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
585</ul>
586<p>By default set to <code>Infinity</code>. Determines how many concurrent sockets the agent
587can have open per origin. Origin is the returned value of <a href="#http_agent_getname_options"><code>agent.getName()</code></a>.</p>
588<h4><code>agent.maxTotalSockets</code><span><a class="mark" href="#http_agent_maxtotalsockets" id="http_agent_maxtotalsockets">#</a></span></h4>
589<div class="api_metadata">
590<span>Added in: v14.5.0</span>
591</div>
592<ul>
593<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
594</ul>
595<p>By default set to <code>Infinity</code>. Determines how many concurrent sockets the agent
596can have open. Unlike <code>maxSockets</code>, this parameter applies across all origins.</p>
597<h4><code>agent.requests</code><span><a class="mark" href="#http_agent_requests" id="http_agent_requests">#</a></span></h4>
598<div class="api_metadata">
599<span>Added in: v0.5.9</span>
600</div>
601<ul>
602<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
603</ul>
604<p>An object which contains queues of requests that have not yet been assigned to
605sockets. Do not modify.</p>
606<h4><code>agent.sockets</code><span><a class="mark" href="#http_agent_sockets" id="http_agent_sockets">#</a></span></h4>
607<div class="api_metadata">
608<span>Added in: v0.3.6</span>
609</div>
610<ul>
611<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
612</ul>
613<p>An object which contains arrays of sockets currently in use by the
614agent. Do not modify.</p>
615</section><section><h3>Class: <code>http.ClientRequest</code><span><a class="mark" href="#http_class_http_clientrequest" id="http_class_http_clientrequest">#</a></span></h3>
616<div class="api_metadata">
617<span>Added in: v0.1.17</span>
618</div>
619<ul>
620<li>Extends: <a href="stream.html#stream_stream" class="type">&#x3C;Stream></a></li>
621</ul>
622<p>This object is created internally and returned from <a href="#http_http_request_options_callback"><code>http.request()</code></a>. It
623represents an <em>in-progress</em> request whose header has already been queued. The
624header is still mutable using the <a href="#http_request_setheader_name_value"><code>setHeader(name, value)</code></a>,
625<a href="#http_request_getheader_name"><code>getHeader(name)</code></a>, <a href="#http_request_removeheader_name"><code>removeHeader(name)</code></a> API. The actual header will
626be sent along with the first data chunk or when calling <a href="#http_request_end_data_encoding_callback"><code>request.end()</code></a>.</p>
627<p>To get the response, add a listener for <a href="#http_event_response"><code>'response'</code></a> to the request object.
628<a href="#http_event_response"><code>'response'</code></a> will be emitted from the request object when the response
629headers have been received. The <a href="#http_event_response"><code>'response'</code></a> event is executed with one
630argument which is an instance of <a href="#http_class_http_incomingmessage"><code>http.IncomingMessage</code></a>.</p>
631<p>During the <a href="#http_event_response"><code>'response'</code></a> event, one can add listeners to the
632response object; particularly to listen for the <code>'data'</code> event.</p>
633<p>If no <a href="#http_event_response"><code>'response'</code></a> handler is added, then the response will be
634entirely discarded. However, if a <a href="#http_event_response"><code>'response'</code></a> event handler is added,
635then the data from the response object <strong>must</strong> be consumed, either by
636calling <code>response.read()</code> whenever there is a <code>'readable'</code> event, or
637by adding a <code>'data'</code> handler, or by calling the <code>.resume()</code> method.
638Until the data is consumed, the <code>'end'</code> event will not fire. Also, until
639the data is read it will consume memory that can eventually lead to a
640'process out of memory' error.</p>
641<p>Unlike the <code>request</code> object, if the response closes prematurely, the
642<code>response</code> object does not emit an <code>'error'</code> event but instead emits the
643<code>'aborted'</code> event.</p>
644<p>Node.js does not check whether Content-Length and the length of the
645body which has been transmitted are equal or not.</p>
646<h4>Event: <code>'abort'</code><span><a class="mark" href="#http_event_abort" id="http_event_abort">#</a></span></h4>
647<div class="api_metadata">
648<span>Added in: v1.4.1</span>
649</div>
650<p>Emitted when the request has been aborted by the client. This event is only
651emitted on the first call to <code>abort()</code>.</p>
652<h4>Event: <code>'connect'</code><span><a class="mark" href="#http_event_connect" id="http_event_connect">#</a></span></h4>
653<div class="api_metadata">
654<span>Added in: v0.7.0</span>
655</div>
656<ul>
657<li><code>response</code> <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a></li>
658<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
659<li><code>head</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
660</ul>
661<p>Emitted each time a server responds to a request with a <code>CONNECT</code> method. If
662this event is not being listened for, clients receiving a <code>CONNECT</code> method will
663have their connections closed.</p>
664<p>This event is guaranteed to be passed an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
665a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specifies a socket
666type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
667<p>A client and server pair demonstrating how to listen for the <code>'connect'</code> event:</p>
668<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
669<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>);
670<span class="hljs-keyword">const</span> { <span class="hljs-variable constant_">URL</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
671
672<span class="hljs-comment">// Create an HTTP tunneling proxy</span>
673<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> {
674  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> });
675  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
676});
677proxy.<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> {
678  <span class="hljs-comment">// Connect to an origin server</span>
679  <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>);
680  <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> {
681    clientSocket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 200 Connection Established\r\n'</span> +
682                    <span class="hljs-string">'Proxy-agent: Node.js-Proxy\r\n'</span> +
683                    <span class="hljs-string">'\r\n'</span>);
684    serverSocket.<span class="hljs-title function_">write</span>(head);
685    serverSocket.<span class="hljs-title function_">pipe</span>(clientSocket);
686    clientSocket.<span class="hljs-title function_">pipe</span>(serverSocket);
687  });
688});
689
690<span class="hljs-comment">// Now that proxy is running</span>
691proxy.<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> {
692
693  <span class="hljs-comment">// Make a request to a tunneling proxy</span>
694  <span class="hljs-keyword">const</span> options = {
695    <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
696    <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
697    <span class="hljs-attr">method</span>: <span class="hljs-string">'CONNECT'</span>,
698    <span class="hljs-attr">path</span>: <span class="hljs-string">'www.google.com:80'</span>
699  };
700
701  <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options);
702  req.<span class="hljs-title function_">end</span>();
703
704  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> {
705    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got connected!'</span>);
706
707    <span class="hljs-comment">// Make a request over an HTTP tunnel</span>
708    socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'GET / HTTP/1.1\r\n'</span> +
709                 <span class="hljs-string">'Host: www.google.com:80\r\n'</span> +
710                 <span class="hljs-string">'Connection: close\r\n'</span> +
711                 <span class="hljs-string">'\r\n'</span>);
712    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> {
713      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk.<span class="hljs-title function_">toString</span>());
714    });
715    socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
716      proxy.<span class="hljs-title function_">close</span>();
717    });
718  });
719});</code></pre>
720<h4>Event: <code>'continue'</code><span><a class="mark" href="#http_event_continue" id="http_event_continue">#</a></span></h4>
721<div class="api_metadata">
722<span>Added in: v0.3.2</span>
723</div>
724<p>Emitted when the server sends a '100 Continue' HTTP response, usually because
725the request contained 'Expect: 100-continue'. This is an instruction that
726the client should send the request body.</p>
727<h4>Event: <code>'information'</code><span><a class="mark" href="#http_event_information" id="http_event_information">#</a></span></h4>
728<div class="api_metadata">
729<span>Added in: v10.0.0</span>
730</div>
731<ul>
732<li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
733<ul>
734<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>
735<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>
736<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>
737<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>
738<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>
739<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>
740<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>
741</ul>
742</li>
743</ul>
744<p>Emitted when the server sends a 1xx intermediate response (excluding 101
745Upgrade). The listeners of this event will receive an object containing the
746HTTP version, status code, status message, key-value headers object,
747and array with the raw header names followed by their respective values.</p>
748<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
749
750<span class="hljs-keyword">const</span> options = {
751  <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
752  <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
753  <span class="hljs-attr">path</span>: <span class="hljs-string">'/length_request'</span>
754};
755
756<span class="hljs-comment">// Make a request</span>
757<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options);
758req.<span class="hljs-title function_">end</span>();
759
760req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'information'</span>, <span class="hljs-function">(<span class="hljs-params">info</span>) =></span> {
761  <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>);
762});</code></pre>
763<p>101 Upgrade statuses do not fire this event due to their break from the
764traditional HTTP request/response chain, such as web sockets, in-place TLS
765upgrades, or HTTP 2.0. To be notified of 101 Upgrade notices, listen for the
766<a href="#http_event_upgrade"><code>'upgrade'</code></a> event instead.</p>
767<h4>Event: <code>'response'</code><span><a class="mark" href="#http_event_response" id="http_event_response">#</a></span></h4>
768<div class="api_metadata">
769<span>Added in: v0.1.0</span>
770</div>
771<ul>
772<li><code>response</code> <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a></li>
773</ul>
774<p>Emitted when a response is received to this request. This event is emitted only
775once.</p>
776<h4>Event: <code>'socket'</code><span><a class="mark" href="#http_event_socket" id="http_event_socket">#</a></span></h4>
777<div class="api_metadata">
778<span>Added in: v0.5.3</span>
779</div>
780<ul>
781<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
782</ul>
783<p>This event is guaranteed to be passed an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
784a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specifies a socket
785type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
786<h4>Event: <code>'timeout'</code><span><a class="mark" href="#http_event_timeout" id="http_event_timeout">#</a></span></h4>
787<div class="api_metadata">
788<span>Added in: v0.7.8</span>
789</div>
790<p>Emitted when the underlying socket times out from inactivity. This only notifies
791that the socket has been idle. The request must be aborted manually.</p>
792<p>See also: <a href="#http_request_settimeout_timeout_callback"><code>request.setTimeout()</code></a>.</p>
793<h4>Event: <code>'upgrade'</code><span><a class="mark" href="#http_event_upgrade" id="http_event_upgrade">#</a></span></h4>
794<div class="api_metadata">
795<span>Added in: v0.1.94</span>
796</div>
797<ul>
798<li><code>response</code> <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a></li>
799<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
800<li><code>head</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
801</ul>
802<p>Emitted each time a server responds to a request with an upgrade. If this
803event is not being listened for and the response status code is 101 Switching
804Protocols, clients receiving an upgrade header will have their connections
805closed.</p>
806<p>This event is guaranteed to be passed an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
807a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specifies a socket
808type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
809<p>A client server pair demonstrating how to listen for the <code>'upgrade'</code> event.</p>
810<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
811
812<span class="hljs-comment">// Create an HTTP server</span>
813<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> {
814  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> });
815  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
816});
817server.<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> {
818  socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'</span> +
819               <span class="hljs-string">'Upgrade: WebSocket\r\n'</span> +
820               <span class="hljs-string">'Connection: Upgrade\r\n'</span> +
821               <span class="hljs-string">'\r\n'</span>);
822
823  socket.<span class="hljs-title function_">pipe</span>(socket); <span class="hljs-comment">// echo back</span>
824});
825
826<span class="hljs-comment">// Now that server is running</span>
827server.<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> {
828
829  <span class="hljs-comment">// make a request</span>
830  <span class="hljs-keyword">const</span> options = {
831    <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
832    <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
833    <span class="hljs-attr">headers</span>: {
834      <span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Upgrade'</span>,
835      <span class="hljs-string">'Upgrade'</span>: <span class="hljs-string">'websocket'</span>
836    }
837  };
838
839  <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options);
840  req.<span class="hljs-title function_">end</span>();
841
842  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> {
843    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'got upgraded!'</span>);
844    socket.<span class="hljs-title function_">end</span>();
845    process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
846  });
847});</code></pre>
848<h4><code>request.abort()</code><span><a class="mark" href="#http_request_abort" id="http_request_abort">#</a></span></h4>
849<div class="api_metadata">
850<span>Added in: v0.3.8</span><span>Deprecated since: v14.1.0</span>
851</div>
852<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#http_request_destroy_error"><code>request.destroy()</code></a> instead.</div><p></p>
853<p>Marks the request as aborting. Calling this will cause remaining data
854in the response to be dropped and the socket to be destroyed.</p>
855<h4><code>request.aborted</code><span><a class="mark" href="#http_request_aborted" id="http_request_aborted">#</a></span></h4>
856<div class="api_metadata">
857<details class="changelog"><summary>History</summary>
858<table>
859<tbody><tr><th>Version</th><th>Changes</th></tr>
860<tr><td>v11.0.0</td>
861<td><p>The <code>aborted</code> property is no longer a timestamp number.</p></td></tr>
862<tr><td>v0.11.14</td>
863<td><p><span>Added in: v0.11.14</span></p></td></tr>
864</tbody></table>
865</details>
866</div>
867<ul>
868<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
869</ul>
870<p>The <code>request.aborted</code> property will be <code>true</code> if the request has
871been aborted.</p>
872<h4><code>request.connection</code><span><a class="mark" href="#http_request_connection" id="http_request_connection">#</a></span></h4>
873<div class="api_metadata">
874<span>Added in: v0.3.0</span><span>Deprecated since: v13.0.0</span>
875</div>
876<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a href="#http_request_socket"><code>request.socket</code></a>.</div><p></p>
877<ul>
878<li><a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
879</ul>
880<p>See <a href="#http_request_socket"><code>request.socket</code></a>.</p>
881<h4><code>request.end([data[, encoding]][, callback])</code><span><a class="mark" href="#http_request_end_data_encoding_callback" id="http_request_end_data_encoding_callback">#</a></span></h4>
882<div class="api_metadata">
883<details class="changelog"><summary>History</summary>
884<table>
885<tbody><tr><th>Version</th><th>Changes</th></tr>
886<tr><td>v10.0.0</td>
887<td><p>This method now returns a reference to <code>ClientRequest</code>.</p></td></tr>
888<tr><td>v0.1.90</td>
889<td><p><span>Added in: v0.1.90</span></p></td></tr>
890</tbody></table>
891</details>
892</div>
893<ul>
894<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#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
895<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>
896<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>
897<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type">&#x3C;this></a></li>
898</ul>
899<p>Finishes sending the request. If any parts of the body are
900unsent, it will flush them to the stream. If the request is
901chunked, this will send the terminating <code>'0\r\n\r\n'</code>.</p>
902<p>If <code>data</code> is specified, it is equivalent to calling
903<a href="#http_request_write_chunk_encoding_callback"><code>request.write(data, encoding)</code></a> followed by <code>request.end(callback)</code>.</p>
904<p>If <code>callback</code> is specified, it will be called when the request stream
905is finished.</p>
906<h4><code>request.destroy([error])</code><span><a class="mark" href="#http_request_destroy_error" id="http_request_destroy_error">#</a></span></h4>
907<div class="api_metadata">
908<details class="changelog"><summary>History</summary>
909<table>
910<tbody><tr><th>Version</th><th>Changes</th></tr>
911<tr><td>v14.5.0</td>
912<td><p>The function returns <code>this</code> for consistency with other Readable streams.</p></td></tr>
913<tr><td>v0.3.0</td>
914<td><p><span>Added in: v0.3.0</span></p></td></tr>
915</tbody></table>
916</details>
917</div>
918<ul>
919<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>
920<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type">&#x3C;this></a></li>
921</ul>
922<p>Destroy the request. Optionally emit an <code>'error'</code> event,
923and emit a <code>'close'</code> event. Calling this will cause remaining data
924in the response to be dropped and the socket to be destroyed.</p>
925<p>See <a href="stream.html#stream_writable_destroy_error"><code>writable.destroy()</code></a> for further details.</p>
926<h5><code>request.destroyed</code><span><a class="mark" href="#http_request_destroyed" id="http_request_destroyed">#</a></span></h5>
927<div class="api_metadata">
928<span>Added in: v14.1.0</span>
929</div>
930<ul>
931<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
932</ul>
933<p>Is <code>true</code> after <a href="#http_request_destroy_error"><code>request.destroy()</code></a> has been called.</p>
934<p>See <a href="stream.html#stream_writable_destroyed"><code>writable.destroyed</code></a> for further details.</p>
935<h4><code>request.finished</code><span><a class="mark" href="#http_request_finished" id="http_request_finished">#</a></span></h4>
936<div class="api_metadata">
937<span>Added in: v0.0.1</span><span>Deprecated since: v13.4.0, v12.16.0</span>
938</div>
939<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a href="#http_request_writableended"><code>request.writableEnded</code></a>.</div><p></p>
940<ul>
941<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
942</ul>
943<p>The <code>request.finished</code> property will be <code>true</code> if <a href="#http_request_end_data_encoding_callback"><code>request.end()</code></a>
944has been called. <code>request.end()</code> will automatically be called if the
945request was initiated via <a href="#http_http_get_options_callback"><code>http.get()</code></a>.</p>
946<h4><code>request.flushHeaders()</code><span><a class="mark" href="#http_request_flushheaders" id="http_request_flushheaders">#</a></span></h4>
947<div class="api_metadata">
948<span>Added in: v1.6.0</span>
949</div>
950<p>Flushes the request headers.</p>
951<p>For efficiency reasons, Node.js normally buffers the request headers until
952<code>request.end()</code> is called or the first chunk of request data is written. It
953then tries to pack the request headers and data into a single TCP packet.</p>
954<p>That's usually desired (it saves a TCP round-trip), but not when the first
955data is not sent until possibly much later. <code>request.flushHeaders()</code> bypasses
956the optimization and kickstarts the request.</p>
957<h4><code>request.getHeader(name)</code><span><a class="mark" href="#http_request_getheader_name" id="http_request_getheader_name">#</a></span></h4>
958<div class="api_metadata">
959<span>Added in: v1.6.0</span>
960</div>
961<ul>
962<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>
963<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
964</ul>
965<p>Reads out a header on the request. The name is case-insensitive.
966The type of the return value depends on the arguments provided to
967<a href="#http_request_setheader_name_value"><code>request.setHeader()</code></a>.</p>
968<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>);
969request.<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));
970request.<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>]);
971<span class="hljs-keyword">const</span> contentType = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Type'</span>);
972<span class="hljs-comment">// 'contentType' is 'text/html'</span>
973<span class="hljs-keyword">const</span> contentLength = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Length'</span>);
974<span class="hljs-comment">// 'contentLength' is of type number</span>
975<span class="hljs-keyword">const</span> cookie = request.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Cookie'</span>);
976<span class="hljs-comment">// 'cookie' is of type string[]</span></code></pre>
977<h4><code>request.getRawHeaderNames()</code><span><a class="mark" href="#http_request_getrawheadernames" id="http_request_getrawheadernames">#</a></span></h4>
978<div class="api_metadata">
979<span>Added in: v14.17.0</span>
980</div>
981<ul>
982<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
983</ul>
984<p>Returns an array containing the unique names of the current outgoing raw
985headers. Header names are returned with their exact casing being set.</p>
986<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>);
987request.<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>]);
988
989<span class="hljs-keyword">const</span> headerNames = request.<span class="hljs-title function_">getRawHeaderNames</span>();
990<span class="hljs-comment">// headerNames === ['Foo', 'Set-Cookie']</span></code></pre>
991<h4><code>request.maxHeadersCount</code><span><a class="mark" href="#http_request_maxheaderscount" id="http_request_maxheaderscount">#</a></span></h4>
992<ul>
993<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>
994</ul>
995<p>Limits maximum response headers count. If set to 0, no limit will be applied.</p>
996<h4><code>request.path</code><span><a class="mark" href="#http_request_path" id="http_request_path">#</a></span></h4>
997<div class="api_metadata">
998<span>Added in: v0.4.0</span>
999</div>
1000<ul>
1001<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>
1002</ul>
1003<h4><code>request.method</code><span><a class="mark" href="#http_request_method" id="http_request_method">#</a></span></h4>
1004<div class="api_metadata">
1005<span>Added in: v0.1.97</span>
1006</div>
1007<ul>
1008<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>
1009</ul>
1010<h4><code>request.host</code><span><a class="mark" href="#http_request_host" id="http_request_host">#</a></span></h4>
1011<div class="api_metadata">
1012<span>Added in: v14.5.0</span>
1013</div>
1014<ul>
1015<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>
1016</ul>
1017<h4><code>request.protocol</code><span><a class="mark" href="#http_request_protocol" id="http_request_protocol">#</a></span></h4>
1018<div class="api_metadata">
1019<span>Added in: v14.5.0</span>
1020</div>
1021<ul>
1022<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>
1023</ul>
1024<h4><code>request.removeHeader(name)</code><span><a class="mark" href="#http_request_removeheader_name" id="http_request_removeheader_name">#</a></span></h4>
1025<div class="api_metadata">
1026<span>Added in: v1.6.0</span>
1027</div>
1028<ul>
1029<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>
1030</ul>
1031<p>Removes a header that's already defined into headers object.</p>
1032<pre><code class="language-js">request.<span class="hljs-title function_">removeHeader</span>(<span class="hljs-string">'Content-Type'</span>);</code></pre>
1033<h4><code>request.reusedSocket</code><span><a class="mark" href="#http_request_reusedsocket" id="http_request_reusedsocket">#</a></span></h4>
1034<div class="api_metadata">
1035<span>Added in: v13.0.0, v12.16.0</span>
1036</div>
1037<ul>
1038<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>
1039</ul>
1040<p>When sending request through a keep-alive enabled agent, the underlying socket
1041might be reused. But if server closes connection at unfortunate time, client
1042may run into a 'ECONNRESET' error.</p>
1043<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
1044
1045<span class="hljs-comment">// Server has a 5 seconds keep-alive timeout by default</span>
1046http
1047  .<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
1048    res.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\n'</span>);
1049    res.<span class="hljs-title function_">end</span>();
1050  })
1051  .<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);
1052
1053<span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
1054  <span class="hljs-comment">// Adapting a keep-alive agent</span>
1055  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> {
1056    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> {
1057      <span class="hljs-comment">// Do nothing</span>
1058    });
1059  });
1060}, <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></pre>
1061<p>By marking a request whether it reused socket or not, we can do
1062automatic error retry base on it.</p>
1063<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
1064<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> });
1065
1066<span class="hljs-keyword">function</span> <span class="hljs-title function_">retriableRequest</span>(<span class="hljs-params"></span>) {
1067  <span class="hljs-keyword">const</span> req = http
1068    .<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> {
1069      <span class="hljs-comment">// ...</span>
1070    })
1071    .<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
1072      <span class="hljs-comment">// Check if retry is needed</span>
1073      <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>) {
1074        <span class="hljs-title function_">retriableRequest</span>();
1075      }
1076    });
1077}
1078
1079<span class="hljs-title function_">retriableRequest</span>();</code></pre>
1080<h4><code>request.setHeader(name, value)</code><span><a class="mark" href="#http_request_setheader_name_value" id="http_request_setheader_name_value">#</a></span></h4>
1081<div class="api_metadata">
1082<span>Added in: v1.6.0</span>
1083</div>
1084<ul>
1085<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>
1086<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>
1087</ul>
1088<p>Sets a single header value for headers object. If this header already exists in
1089the to-be-sent headers, its value will be replaced. Use an array of strings
1090here to send multiple headers with the same name. Non-string values will be
1091stored without modification. Therefore, <a href="#http_request_getheader_name"><code>request.getHeader()</code></a> may return
1092non-string values. However, the non-string values will be converted to strings
1093for network transmission.</p>
1094<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></pre>
1095<p>or</p>
1096<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></pre>
1097<h4><code>request.setNoDelay([noDelay])</code><span><a class="mark" href="#http_request_setnodelay_nodelay" id="http_request_setnodelay_nodelay">#</a></span></h4>
1098<div class="api_metadata">
1099<span>Added in: v0.5.9</span>
1100</div>
1101<ul>
1102<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>
1103</ul>
1104<p>Once a socket is assigned to this request and is connected
1105<a href="net.html#net_socket_setnodelay_nodelay"><code>socket.setNoDelay()</code></a> will be called.</p>
1106<h4><code>request.setSocketKeepAlive([enable][, initialDelay])</code><span><a class="mark" href="#http_request_setsocketkeepalive_enable_initialdelay" id="http_request_setsocketkeepalive_enable_initialdelay">#</a></span></h4>
1107<div class="api_metadata">
1108<span>Added in: v0.5.9</span>
1109</div>
1110<ul>
1111<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>
1112<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>
1113</ul>
1114<p>Once a socket is assigned to this request and is connected
1115<a href="net.html#net_socket_setkeepalive_enable_initialdelay"><code>socket.setKeepAlive()</code></a> will be called.</p>
1116<h4><code>request.setTimeout(timeout[, callback])</code><span><a class="mark" href="#http_request_settimeout_timeout_callback" id="http_request_settimeout_timeout_callback">#</a></span></h4>
1117<div class="api_metadata">
1118<details class="changelog"><summary>History</summary>
1119<table>
1120<tbody><tr><th>Version</th><th>Changes</th></tr>
1121<tr><td>v9.0.0</td>
1122<td><p>Consistently set socket timeout only when the socket connects.</p></td></tr>
1123<tr><td>v0.5.9</td>
1124<td><p><span>Added in: v0.5.9</span></p></td></tr>
1125</tbody></table>
1126</details>
1127</div>
1128<ul>
1129<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>
1130<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.
1131Same as binding to the <code>'timeout'</code> event.</li>
1132<li>Returns: <a href="http.html#http_class_http_clientrequest" class="type">&#x3C;http.ClientRequest></a></li>
1133</ul>
1134<p>Once a socket is assigned to this request and is connected
1135<a href="net.html#net_socket_settimeout_timeout_callback"><code>socket.setTimeout()</code></a> will be called.</p>
1136<h4><code>request.socket</code><span><a class="mark" href="#http_request_socket" id="http_request_socket">#</a></span></h4>
1137<div class="api_metadata">
1138<span>Added in: v0.3.0</span>
1139</div>
1140<ul>
1141<li><a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
1142</ul>
1143<p>Reference to the underlying socket. Usually users will not want to access
1144this property. In particular, the socket will not emit <code>'readable'</code> events
1145because of how the protocol parser attaches to the socket.</p>
1146<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
1147<span class="hljs-keyword">const</span> options = {
1148  <span class="hljs-attr">host</span>: <span class="hljs-string">'www.google.com'</span>,
1149};
1150<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">get</span>(options);
1151req.<span class="hljs-title function_">end</span>();
1152req.<span class="hljs-title function_">once</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
1153  <span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localAddress</span>;
1154  <span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">localPort</span>;
1155  <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>);
1156  <span class="hljs-comment">// Consume response object</span>
1157});</code></pre>
1158<p>This property is guaranteed to be an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
1159a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specified a socket
1160type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
1161<h4><code>request.writableEnded</code><span><a class="mark" href="#http_request_writableended" id="http_request_writableended">#</a></span></h4>
1162<div class="api_metadata">
1163<span>Added in: v12.9.0</span>
1164</div>
1165<ul>
1166<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1167</ul>
1168<p>Is <code>true</code> after <a href="#http_request_end_data_encoding_callback"><code>request.end()</code></a> has been called. This property
1169does not indicate whether the data has been flushed, for this use
1170<a href="#http_request_writablefinished"><code>request.writableFinished</code></a> instead.</p>
1171<h4><code>request.writableFinished</code><span><a class="mark" href="#http_request_writablefinished" id="http_request_writablefinished">#</a></span></h4>
1172<div class="api_metadata">
1173<span>Added in: v12.7.0</span>
1174</div>
1175<ul>
1176<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1177</ul>
1178<p>Is <code>true</code> if all data has been flushed to the underlying system, immediately
1179before the <a href="#http_event_finish"><code>'finish'</code></a> event is emitted.</p>
1180<h4><code>request.write(chunk[, encoding][, callback])</code><span><a class="mark" href="#http_request_write_chunk_encoding_callback" id="http_request_write_chunk_encoding_callback">#</a></span></h4>
1181<div class="api_metadata">
1182<span>Added in: v0.1.29</span>
1183</div>
1184<ul>
1185<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#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
1186<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>
1187<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>
1188<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1189</ul>
1190<p>Sends a chunk of the body. This method can be called multiple times. If no
1191<code>Content-Length</code> is set, data will automatically be encoded in HTTP Chunked
1192transfer encoding, so that server knows when the data ends. The
1193<code>Transfer-Encoding: chunked</code> header is added. Calling <a href="#http_request_end_data_encoding_callback"><code>request.end()</code></a>
1194is necessary to finish sending the request.</p>
1195<p>The <code>encoding</code> argument is optional and only applies when <code>chunk</code> is a string.
1196Defaults to <code>'utf8'</code>.</p>
1197<p>The <code>callback</code> argument is optional and will be called when this chunk of data
1198is flushed, but only if the chunk is non-empty.</p>
1199<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel
1200buffer. Returns <code>false</code> if all or part of the data was queued in user memory.
1201<code>'drain'</code> will be emitted when the buffer is free again.</p>
1202<p>When <code>write</code> function is called with empty string or buffer, it does
1203nothing and waits for more input.</p>
1204</section><section><h3>Class: <code>http.Server</code><span><a class="mark" href="#http_class_http_server" id="http_class_http_server">#</a></span></h3>
1205<div class="api_metadata">
1206<span>Added in: v0.1.17</span>
1207</div>
1208<ul>
1209<li>Extends: <a href="net.html#net_class_net_server" class="type">&#x3C;net.Server></a></li>
1210</ul>
1211<h4>Event: <code>'checkContinue'</code><span><a class="mark" href="#http_event_checkcontinue" id="http_event_checkcontinue">#</a></span></h4>
1212<div class="api_metadata">
1213<span>Added in: v0.3.0</span>
1214</div>
1215<ul>
1216<li><code>request</code> <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a></li>
1217<li><code>response</code> <a href="http.html#http_class_http_serverresponse" class="type">&#x3C;http.ServerResponse></a></li>
1218</ul>
1219<p>Emitted each time a request with an HTTP <code>Expect: 100-continue</code> is received.
1220If this event is not listened for, the server will automatically respond
1221with a <code>100 Continue</code> as appropriate.</p>
1222<p>Handling this event involves calling <a href="#http_response_writecontinue"><code>response.writeContinue()</code></a> if the
1223client should continue to send the request body, or generating an appropriate
1224HTTP response (e.g. 400 Bad Request) if the client should not continue to send
1225the request body.</p>
1226<p>When this event is emitted and handled, the <a href="#http_event_request"><code>'request'</code></a> event will
1227not be emitted.</p>
1228<h4>Event: <code>'checkExpectation'</code><span><a class="mark" href="#http_event_checkexpectation" id="http_event_checkexpectation">#</a></span></h4>
1229<div class="api_metadata">
1230<span>Added in: v5.5.0</span>
1231</div>
1232<ul>
1233<li><code>request</code> <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a></li>
1234<li><code>response</code> <a href="http.html#http_class_http_serverresponse" class="type">&#x3C;http.ServerResponse></a></li>
1235</ul>
1236<p>Emitted each time a request with an HTTP <code>Expect</code> header is received, where the
1237value is not <code>100-continue</code>. If this event is not listened for, the server will
1238automatically respond with a <code>417 Expectation Failed</code> as appropriate.</p>
1239<p>When this event is emitted and handled, the <a href="#http_event_request"><code>'request'</code></a> event will
1240not be emitted.</p>
1241<h4>Event: <code>'clientError'</code><span><a class="mark" href="#http_event_clienterror" id="http_event_clienterror">#</a></span></h4>
1242<div class="api_metadata">
1243<details class="changelog"><summary>History</summary>
1244<table>
1245<tbody><tr><th>Version</th><th>Changes</th></tr>
1246<tr><td>v12.0.0</td>
1247<td><p>The default behavior will return a 431 Request Header Fields Too Large if a HPE_HEADER_OVERFLOW error occurs.</p></td></tr>
1248<tr><td>v9.4.0</td>
1249<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>
1250<tr><td>v6.0.0</td>
1251<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>
1252<tr><td>v0.1.94</td>
1253<td><p><span>Added in: v0.1.94</span></p></td></tr>
1254</tbody></table>
1255</details>
1256</div>
1257<ul>
1258<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>
1259<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
1260</ul>
1261<p>If a client connection emits an <code>'error'</code> event, it will be forwarded here.
1262Listener of this event is responsible for closing/destroying the underlying
1263socket. For example, one may wish to more gracefully close the socket with a
1264custom HTTP response instead of abruptly severing the connection.</p>
1265<p>This event is guaranteed to be passed an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
1266a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specifies a socket
1267type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
1268<p>Default behavior is to try close the socket with a HTTP '400 Bad Request',
1269or a HTTP '431 Request Header Fields Too Large' in the case of a
1270<a href="errors.html#errors_hpe_header_overflow"><code>HPE_HEADER_OVERFLOW</code></a> error. If the socket is not writable or has already
1271written data it is immediately destroyed.</p>
1272<p><code>socket</code> is the <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> object that the error originated from.</p>
1273<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
1274
1275<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> {
1276  res.<span class="hljs-title function_">end</span>();
1277});
1278server.<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> {
1279  socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</span>);
1280});
1281server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre>
1282<p>When the <code>'clientError'</code> event occurs, there is no <code>request</code> or <code>response</code>
1283object, so any HTTP response sent, including response headers and payload,
1284<em>must</em> be written directly to the <code>socket</code> object. Care must be taken to
1285ensure the response is a properly formatted HTTP response message.</p>
1286<p><code>err</code> is an instance of <code>Error</code> with two extra columns:</p>
1287<ul>
1288<li><code>bytesParsed</code>: the bytes count of request packet that Node.js may have parsed
1289correctly;</li>
1290<li><code>rawPacket</code>: the raw packet of current request.</li>
1291</ul>
1292<p>In some cases, the client has already received the response and/or the socket
1293has already been destroyed, like in case of <code>ECONNRESET</code> errors. Before
1294trying to send data to the socket, it is better to check that it is still
1295writable.</p>
1296<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> {
1297  <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>) {
1298    <span class="hljs-keyword">return</span>;
1299  }
1300
1301  socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</span>);
1302});</code></pre>
1303<h4>Event: <code>'close'</code><span><a class="mark" href="#http_event_close" id="http_event_close">#</a></span></h4>
1304<div class="api_metadata">
1305<span>Added in: v0.1.4</span>
1306</div>
1307<p>Emitted when the server closes.</p>
1308<h4>Event: <code>'connect'</code><span><a class="mark" href="#http_event_connect_1" id="http_event_connect_1">#</a></span></h4>
1309<div class="api_metadata">
1310<span>Added in: v0.7.0</span>
1311</div>
1312<ul>
1313<li><code>request</code> <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a> Arguments for the HTTP request, as it is in
1314the <a href="#http_event_request"><code>'request'</code></a> event</li>
1315<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a> Network socket between the server and client</li>
1316<li><code>head</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The first packet of the tunneling stream (may be empty)</li>
1317</ul>
1318<p>Emitted each time a client requests an HTTP <code>CONNECT</code> method. If this event is
1319not listened for, then clients requesting a <code>CONNECT</code> method will have their
1320connections closed.</p>
1321<p>This event is guaranteed to be passed an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
1322a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specifies a socket
1323type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
1324<p>After this event is emitted, the request's socket will not have a <code>'data'</code>
1325event listener, meaning it will need to be bound in order to handle data
1326sent to the server on that socket.</p>
1327<h4>Event: <code>'connection'</code><span><a class="mark" href="#http_event_connection" id="http_event_connection">#</a></span></h4>
1328<div class="api_metadata">
1329<span>Added in: v0.1.0</span>
1330</div>
1331<ul>
1332<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
1333</ul>
1334<p>This event is emitted when a new TCP stream is established. <code>socket</code> is
1335typically an object of type <a href="net.html#net_class_net_socket"><code>net.Socket</code></a>. Usually users will not want to
1336access this event. In particular, the socket will not emit <code>'readable'</code> events
1337because of how the protocol parser attaches to the socket. The <code>socket</code> can
1338also be accessed at <code>request.socket</code>.</p>
1339<p>This event can also be explicitly emitted by users to inject connections
1340into the HTTP server. In that case, any <a href="stream.html#stream_class_stream_duplex"><code>Duplex</code></a> stream can be passed.</p>
1341<p>If <code>socket.setTimeout()</code> is called here, the timeout will be replaced with
1342<code>server.keepAliveTimeout</code> when the socket has served a request (if
1343<code>server.keepAliveTimeout</code> is non-zero).</p>
1344<p>This event is guaranteed to be passed an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
1345a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specifies a socket
1346type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
1347<h4>Event: <code>'request'</code><span><a class="mark" href="#http_event_request" id="http_event_request">#</a></span></h4>
1348<div class="api_metadata">
1349<span>Added in: v0.1.0</span>
1350</div>
1351<ul>
1352<li><code>request</code> <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a></li>
1353<li><code>response</code> <a href="http.html#http_class_http_serverresponse" class="type">&#x3C;http.ServerResponse></a></li>
1354</ul>
1355<p>Emitted each time there is a request. There may be multiple requests
1356per connection (in the case of HTTP Keep-Alive connections).</p>
1357<h4>Event: <code>'upgrade'</code><span><a class="mark" href="#http_event_upgrade_1" id="http_event_upgrade_1">#</a></span></h4>
1358<div class="api_metadata">
1359<details class="changelog"><summary>History</summary>
1360<table>
1361<tbody><tr><th>Version</th><th>Changes</th></tr>
1362<tr><td>v10.0.0</td>
1363<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>
1364<tr><td>v0.1.94</td>
1365<td><p><span>Added in: v0.1.94</span></p></td></tr>
1366</tbody></table>
1367</details>
1368</div>
1369<ul>
1370<li><code>request</code> <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a> Arguments for the HTTP request, as it is in
1371the <a href="#http_event_request"><code>'request'</code></a> event</li>
1372<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a> Network socket between the server and client</li>
1373<li><code>head</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The first packet of the upgraded stream (may be empty)</li>
1374</ul>
1375<p>Emitted each time a client requests an HTTP upgrade. Listening to this event
1376is optional and clients cannot insist on a protocol change.</p>
1377<p>After this event is emitted, the request's socket will not have a <code>'data'</code>
1378event listener, meaning it will need to be bound in order to handle data
1379sent to the server on that socket.</p>
1380<p>This event is guaranteed to be passed an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
1381a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specifies a socket
1382type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
1383<h4><code>server.close([callback])</code><span><a class="mark" href="#http_server_close_callback" id="http_server_close_callback">#</a></span></h4>
1384<div class="api_metadata">
1385<span>Added in: v0.1.90</span>
1386</div>
1387<ul>
1388<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>
1389</ul>
1390<p>Stops the server from accepting new connections. See <a href="net.html#net_server_close_callback"><code>net.Server.close()</code></a>.</p>
1391<h4><code>server.headersTimeout</code><span><a class="mark" href="#http_server_headerstimeout" id="http_server_headerstimeout">#</a></span></h4>
1392<div class="api_metadata">
1393<span>Added in: v11.3.0, v10.14.0</span>
1394</div>
1395<ul>
1396<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>60000</code></li>
1397</ul>
1398<p>Limit the amount of time the parser will wait to receive the complete HTTP
1399headers.</p>
1400<p>In case of inactivity, the rules defined in <a href="#http_server_timeout"><code>server.timeout</code></a> apply. However,
1401that inactivity based timeout would still allow the connection to be kept open
1402if the headers are being sent very slowly (by default, up to a byte per 2
1403minutes). In order to prevent this, whenever header data arrives an additional
1404check is made that more than <code>server.headersTimeout</code> milliseconds has not
1405passed since the connection was established. If the check fails, a <code>'timeout'</code>
1406event is emitted on the server object, and (by default) the socket is destroyed.
1407See <a href="#http_server_timeout"><code>server.timeout</code></a> for more information on how timeout behavior can be
1408customized.</p>
1409<h4><code>server.listen()</code><span><a class="mark" href="#http_server_listen" id="http_server_listen">#</a></span></h4>
1410<p>Starts the HTTP server listening for connections.
1411This method is identical to <a href="net.html#net_server_listen"><code>server.listen()</code></a> from <a href="net.html#net_class_net_server"><code>net.Server</code></a>.</p>
1412<h4><code>server.listening</code><span><a class="mark" href="#http_server_listening" id="http_server_listening">#</a></span></h4>
1413<div class="api_metadata">
1414<span>Added in: v5.7.0</span>
1415</div>
1416<ul>
1417<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>
1418</ul>
1419<h4><code>server.maxHeadersCount</code><span><a class="mark" href="#http_server_maxheaderscount" id="http_server_maxheaderscount">#</a></span></h4>
1420<div class="api_metadata">
1421<span>Added in: v0.7.0</span>
1422</div>
1423<ul>
1424<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>
1425</ul>
1426<p>Limits maximum incoming headers count. If set to 0, no limit will be applied.</p>
1427<h4><code>server.requestTimeout</code><span><a class="mark" href="#http_server_requesttimeout" id="http_server_requesttimeout">#</a></span></h4>
1428<div class="api_metadata">
1429<span>Added in: v14.11.0</span>
1430</div>
1431<ul>
1432<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>0</code></li>
1433</ul>
1434<p>Sets the timeout value in milliseconds for receiving the entire request from
1435the client.</p>
1436<p>If the timeout expires, the server responds with status 408 without
1437forwarding the request to the request listener and then closes the connection.</p>
1438<p>It must be set to a non-zero value (e.g. 120 seconds) to protect against
1439potential Denial-of-Service attacks in case the server is deployed without a
1440reverse proxy in front.</p>
1441<h4><code>server.setTimeout([msecs][, callback])</code><span><a class="mark" href="#http_server_settimeout_msecs_callback" id="http_server_settimeout_msecs_callback">#</a></span></h4>
1442<div class="api_metadata">
1443<details class="changelog"><summary>History</summary>
1444<table>
1445<tbody><tr><th>Version</th><th>Changes</th></tr>
1446<tr><td>v13.0.0</td>
1447<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
1448<tr><td>v0.9.12</td>
1449<td><p><span>Added in: v0.9.12</span></p></td></tr>
1450</tbody></table>
1451</details>
1452</div>
1453<ul>
1454<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>
1455<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>
1456<li>Returns: <a href="http.html#http_class_http_server" class="type">&#x3C;http.Server></a></li>
1457</ul>
1458<p>Sets the timeout value for sockets, and emits a <code>'timeout'</code> event on
1459the Server object, passing the socket as an argument, if a timeout
1460occurs.</p>
1461<p>If there is a <code>'timeout'</code> event listener on the Server object, then it
1462will be called with the timed-out socket as an argument.</p>
1463<p>By default, the Server does not timeout sockets. However, if a callback
1464is assigned to the Server's <code>'timeout'</code> event, timeouts must be handled
1465explicitly.</p>
1466<h4><code>server.timeout</code><span><a class="mark" href="#http_server_timeout" id="http_server_timeout">#</a></span></h4>
1467<div class="api_metadata">
1468<details class="changelog"><summary>History</summary>
1469<table>
1470<tbody><tr><th>Version</th><th>Changes</th></tr>
1471<tr><td>v13.0.0</td>
1472<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
1473<tr><td>v0.9.12</td>
1474<td><p><span>Added in: v0.9.12</span></p></td></tr>
1475</tbody></table>
1476</details>
1477</div>
1478<ul>
1479<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>
1480</ul>
1481<p>The number of milliseconds of inactivity before a socket is presumed
1482to have timed out.</p>
1483<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p>
1484<p>The socket timeout logic is set up on connection, so changing this
1485value only affects new connections to the server, not any existing connections.</p>
1486<h4><code>server.keepAliveTimeout</code><span><a class="mark" href="#http_server_keepalivetimeout" id="http_server_keepalivetimeout">#</a></span></h4>
1487<div class="api_metadata">
1488<span>Added in: v8.0.0</span>
1489</div>
1490<ul>
1491<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>
1492</ul>
1493<p>The number of milliseconds of inactivity a server needs to wait for additional
1494incoming data, after it has finished writing the last response, before a socket
1495will be destroyed. If the server receives new data before the keep-alive
1496timeout has fired, it will reset the regular inactivity timeout, i.e.,
1497<a href="#http_server_timeout"><code>server.timeout</code></a>.</p>
1498<p>A value of <code>0</code> will disable the keep-alive timeout behavior on incoming
1499connections.
1500A value of <code>0</code> makes the http server behave similarly to Node.js versions prior
1501to 8.0.0, which did not have a keep-alive timeout.</p>
1502<p>The socket timeout logic is set up on connection, so changing this value only
1503affects new connections to the server, not any existing connections.</p>
1504</section><section><h3>Class: <code>http.ServerResponse</code><span><a class="mark" href="#http_class_http_serverresponse" id="http_class_http_serverresponse">#</a></span></h3>
1505<div class="api_metadata">
1506<span>Added in: v0.1.17</span>
1507</div>
1508<ul>
1509<li>Extends: <a href="stream.html#stream_stream" class="type">&#x3C;Stream></a></li>
1510</ul>
1511<p>This object is created internally by an HTTP server, not by the user. It is
1512passed as the second parameter to the <a href="#http_event_request"><code>'request'</code></a> event.</p>
1513<h4>Event: <code>'close'</code><span><a class="mark" href="#http_event_close_1" id="http_event_close_1">#</a></span></h4>
1514<div class="api_metadata">
1515<span>Added in: v0.6.7</span>
1516</div>
1517<p>Indicates that the response is completed, or its underlying connection was
1518terminated prematurely (before the response completion).</p>
1519<h4>Event: <code>'finish'</code><span><a class="mark" href="#http_event_finish" id="http_event_finish">#</a></span></h4>
1520<div class="api_metadata">
1521<span>Added in: v0.3.6</span>
1522</div>
1523<p>Emitted when the response has been sent. More specifically, this event is
1524emitted when the last segment of the response headers and body have been
1525handed off to the operating system for transmission over the network. It
1526does not imply that the client has received anything yet.</p>
1527<h4><code>response.addTrailers(headers)</code><span><a class="mark" href="#http_response_addtrailers_headers" id="http_response_addtrailers_headers">#</a></span></h4>
1528<div class="api_metadata">
1529<span>Added in: v0.3.0</span>
1530</div>
1531<ul>
1532<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>
1533</ul>
1534<p>This method adds HTTP trailing headers (a header but at the end of the
1535message) to the response.</p>
1536<p>Trailers will <strong>only</strong> be emitted if chunked encoding is used for the
1537response; if it is not (e.g. if the request was HTTP/1.0), they will
1538be silently discarded.</p>
1539<p>HTTP requires the <code>Trailer</code> header to be sent in order to
1540emit trailers, with a list of the header fields in its value. E.g.,</p>
1541<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>,
1542                          <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> });
1543response.<span class="hljs-title function_">write</span>(fileData);
1544response.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-MD5'</span>: <span class="hljs-string">'7895bf4b8828b55ceaf47747b4bca667'</span> });
1545response.<span class="hljs-title function_">end</span>();</code></pre>
1546<p>Attempting to set a header field name or value that contains invalid characters
1547will result in a <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> being thrown.</p>
1548<h4><code>response.connection</code><span><a class="mark" href="#http_response_connection" id="http_response_connection">#</a></span></h4>
1549<div class="api_metadata">
1550<span>Added in: v0.3.0</span><span>Deprecated since: v13.0.0</span>
1551</div>
1552<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a href="#http_response_socket"><code>response.socket</code></a>.</div><p></p>
1553<ul>
1554<li><a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
1555</ul>
1556<p>See <a href="#http_response_socket"><code>response.socket</code></a>.</p>
1557<h4><code>response.cork()</code><span><a class="mark" href="#http_response_cork" id="http_response_cork">#</a></span></h4>
1558<div class="api_metadata">
1559<span>Added in: v13.2.0, v12.16.0</span>
1560</div>
1561<p>See <a href="stream.html#stream_writable_cork"><code>writable.cork()</code></a>.</p>
1562<h4><code>response.end([data[, encoding]][, callback])</code><span><a class="mark" href="#http_response_end_data_encoding_callback" id="http_response_end_data_encoding_callback">#</a></span></h4>
1563<div class="api_metadata">
1564<details class="changelog"><summary>History</summary>
1565<table>
1566<tbody><tr><th>Version</th><th>Changes</th></tr>
1567<tr><td>v10.0.0</td>
1568<td><p>This method now returns a reference to <code>ServerResponse</code>.</p></td></tr>
1569<tr><td>v0.1.90</td>
1570<td><p><span>Added in: v0.1.90</span></p></td></tr>
1571</tbody></table>
1572</details>
1573</div>
1574<ul>
1575<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#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
1576<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>
1577<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>
1578<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type">&#x3C;this></a></li>
1579</ul>
1580<p>This method signals to the server that all of the response headers and body
1581have been sent; that server should consider this message complete.
1582The method, <code>response.end()</code>, MUST be called on each response.</p>
1583<p>If <code>data</code> is specified, it is similar in effect to calling
1584<a href="#http_response_write_chunk_encoding_callback"><code>response.write(data, encoding)</code></a> followed by <code>response.end(callback)</code>.</p>
1585<p>If <code>callback</code> is specified, it will be called when the response stream
1586is finished.</p>
1587<h4><code>response.finished</code><span><a class="mark" href="#http_response_finished" id="http_response_finished">#</a></span></h4>
1588<div class="api_metadata">
1589<span>Added in: v0.0.2</span><span>Deprecated since: v13.4.0, v12.16.0</span>
1590</div>
1591<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a href="#http_response_writableended"><code>response.writableEnded</code></a>.</div><p></p>
1592<ul>
1593<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1594</ul>
1595<p>The <code>response.finished</code> property will be <code>true</code> if <a href="#http_response_end_data_encoding_callback"><code>response.end()</code></a>
1596has been called.</p>
1597<h4><code>response.flushHeaders()</code><span><a class="mark" href="#http_response_flushheaders" id="http_response_flushheaders">#</a></span></h4>
1598<div class="api_metadata">
1599<span>Added in: v1.6.0</span>
1600</div>
1601<p>Flushes the response headers. See also: <a href="#http_request_flushheaders"><code>request.flushHeaders()</code></a>.</p>
1602<h4><code>response.getHeader(name)</code><span><a class="mark" href="#http_response_getheader_name" id="http_response_getheader_name">#</a></span></h4>
1603<div class="api_metadata">
1604<span>Added in: v0.4.0</span>
1605</div>
1606<ul>
1607<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>
1608<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1609</ul>
1610<p>Reads out a header that's already been queued but not sent to the client.
1611The name is case-insensitive. The type of the return value depends
1612on the arguments provided to <a href="#http_response_setheader_name_value"><code>response.setHeader()</code></a>.</p>
1613<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>);
1614response.<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));
1615response.<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>]);
1616<span class="hljs-keyword">const</span> contentType = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'content-type'</span>);
1617<span class="hljs-comment">// contentType is 'text/html'</span>
1618<span class="hljs-keyword">const</span> contentLength = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'Content-Length'</span>);
1619<span class="hljs-comment">// contentLength is of type number</span>
1620<span class="hljs-keyword">const</span> setCookie = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'set-cookie'</span>);
1621<span class="hljs-comment">// setCookie is of type string[]</span></code></pre>
1622<h4><code>response.getHeaderNames()</code><span><a class="mark" href="#http_response_getheadernames" id="http_response_getheadernames">#</a></span></h4>
1623<div class="api_metadata">
1624<span>Added in: v7.7.0</span>
1625</div>
1626<ul>
1627<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
1628</ul>
1629<p>Returns an array containing the unique names of the current outgoing headers.
1630All header names are lowercase.</p>
1631<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>);
1632response.<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>]);
1633
1634<span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">getHeaderNames</span>();
1635<span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code></pre>
1636<h4><code>response.getHeaders()</code><span><a class="mark" href="#http_response_getheaders" id="http_response_getheaders">#</a></span></h4>
1637<div class="api_metadata">
1638<span>Added in: v7.7.0</span>
1639</div>
1640<ul>
1641<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
1642</ul>
1643<p>Returns a shallow copy of the current outgoing headers. Since a shallow copy
1644is used, array values may be mutated without additional calls to various
1645header-related http module methods. The keys of the returned object are the
1646header names and the values are the respective header values. All header names
1647are lowercase.</p>
1648<p>The object returned by the <code>response.getHeaders()</code> method <em>does not</em>
1649prototypically inherit from the JavaScript <code>Object</code>. This means that typical
1650<code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, and others
1651are not defined and <em>will not work</em>.</p>
1652<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>);
1653response.<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>]);
1654
1655<span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHeaders</span>();
1656<span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</span></code></pre>
1657<h4><code>response.hasHeader(name)</code><span><a class="mark" href="#http_response_hasheader_name" id="http_response_hasheader_name">#</a></span></h4>
1658<div class="api_metadata">
1659<span>Added in: v7.7.0</span>
1660</div>
1661<ul>
1662<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>
1663<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1664</ul>
1665<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the
1666outgoing headers. The header name matching is case-insensitive.</p>
1667<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></pre>
1668<h4><code>response.headersSent</code><span><a class="mark" href="#http_response_headerssent" id="http_response_headerssent">#</a></span></h4>
1669<div class="api_metadata">
1670<span>Added in: v0.9.3</span>
1671</div>
1672<ul>
1673<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1674</ul>
1675<p>Boolean (read-only). True if headers were sent, false otherwise.</p>
1676<h4><code>response.removeHeader(name)</code><span><a class="mark" href="#http_response_removeheader_name" id="http_response_removeheader_name">#</a></span></h4>
1677<div class="api_metadata">
1678<span>Added in: v0.4.0</span>
1679</div>
1680<ul>
1681<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>
1682</ul>
1683<p>Removes a header that's queued for implicit sending.</p>
1684<pre><code class="language-js">response.<span class="hljs-title function_">removeHeader</span>(<span class="hljs-string">'Content-Encoding'</span>);</code></pre>
1685<h4><code>response.sendDate</code><span><a class="mark" href="#http_response_senddate" id="http_response_senddate">#</a></span></h4>
1686<div class="api_metadata">
1687<span>Added in: v0.7.5</span>
1688</div>
1689<ul>
1690<li><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>When true, the Date header will be automatically generated and sent in
1693the response if it is not already present in the headers. Defaults to true.</p>
1694<p>This should only be disabled for testing; HTTP requires the Date header
1695in responses.</p>
1696<h4><code>response.setHeader(name, value)</code><span><a class="mark" href="#http_response_setheader_name_value" id="http_response_setheader_name_value">#</a></span></h4>
1697<div class="api_metadata">
1698<span>Added in: v0.4.0</span>
1699</div>
1700<ul>
1701<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>
1702<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>
1703<li>Returns: <a href="http.html#http_class_http_serverresponse" class="type">&#x3C;http.ServerResponse></a></li>
1704</ul>
1705<p>Returns the response object.</p>
1706<p>Sets a single header value for implicit headers. If this header already exists
1707in the to-be-sent headers, its value will be replaced. Use an array of strings
1708here to send multiple headers with the same name. Non-string values will be
1709stored without modification. Therefore, <a href="#http_response_getheader_name"><code>response.getHeader()</code></a> may return
1710non-string values. However, the non-string values will be converted to strings
1711for network transmission. The same response object is returned to the caller,
1712to enable call chaining.</p>
1713<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></pre>
1714<p>or</p>
1715<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></pre>
1716<p>Attempting to set a header field name or value that contains invalid characters
1717will result in a <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> being thrown.</p>
1718<p>When headers have been set with <a href="#http_response_setheader_name_value"><code>response.setHeader()</code></a>, they will be merged
1719with any headers passed to <a href="#http_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a>, with the headers passed
1720to <a href="#http_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a> given precedence.</p>
1721<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span>
1722<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> {
1723  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>);
1724  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
1725  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> });
1726  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
1727});</code></pre>
1728<p>If <a href="#http_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a> method is called and this method has not been
1729called, it will directly write the supplied header values onto the network
1730channel without caching internally, and the <a href="#http_response_getheader_name"><code>response.getHeader()</code></a> on the
1731header will not yield the expected result. If progressive population of headers
1732is desired with potential future retrieval and modification, use
1733<a href="#http_response_setheader_name_value"><code>response.setHeader()</code></a> instead of <a href="#http_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a>.</p>
1734<h4><code>response.setTimeout(msecs[, callback])</code><span><a class="mark" href="#http_response_settimeout_msecs_callback" id="http_response_settimeout_msecs_callback">#</a></span></h4>
1735<div class="api_metadata">
1736<span>Added in: v0.9.12</span>
1737</div>
1738<ul>
1739<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>
1740<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>
1741<li>Returns: <a href="http.html#http_class_http_serverresponse" class="type">&#x3C;http.ServerResponse></a></li>
1742</ul>
1743<p>Sets the Socket's timeout value to <code>msecs</code>. If a callback is
1744provided, then it is added as a listener on the <code>'timeout'</code> event on
1745the response object.</p>
1746<p>If no <code>'timeout'</code> listener is added to the request, the response, or
1747the server, then sockets are destroyed when they time out. If a handler is
1748assigned to the request, the response, or the server's <code>'timeout'</code> events,
1749timed out sockets must be handled explicitly.</p>
1750<h4><code>response.socket</code><span><a class="mark" href="#http_response_socket" id="http_response_socket">#</a></span></h4>
1751<div class="api_metadata">
1752<span>Added in: v0.3.0</span>
1753</div>
1754<ul>
1755<li><a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
1756</ul>
1757<p>Reference to the underlying socket. Usually users will not want to access
1758this property. In particular, the socket will not emit <code>'readable'</code> events
1759because of how the protocol parser attaches to the socket. After
1760<code>response.end()</code>, the property is nulled.</p>
1761<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
1762<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> {
1763  <span class="hljs-keyword">const</span> ip = res.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>;
1764  <span class="hljs-keyword">const</span> port = res.<span class="hljs-property">socket</span>.<span class="hljs-property">remotePort</span>;
1765  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>);
1766}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code></pre>
1767<p>This property is guaranteed to be an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
1768a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specified a socket
1769type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
1770<h4><code>response.statusCode</code><span><a class="mark" href="#http_response_statuscode" id="http_response_statuscode">#</a></span></h4>
1771<div class="api_metadata">
1772<span>Added in: v0.4.0</span>
1773</div>
1774<ul>
1775<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>
1776</ul>
1777<p>When using implicit headers (not calling <a href="#http_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a> explicitly),
1778this property controls the status code that will be sent to the client when
1779the headers get flushed.</p>
1780<pre><code class="language-js">response.<span class="hljs-property">statusCode</span> = <span class="hljs-number">404</span>;</code></pre>
1781<p>After response header was sent to the client, this property indicates the
1782status code which was sent out.</p>
1783<h4><code>response.statusMessage</code><span><a class="mark" href="#http_response_statusmessage" id="http_response_statusmessage">#</a></span></h4>
1784<div class="api_metadata">
1785<span>Added in: v0.11.8</span>
1786</div>
1787<ul>
1788<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1789</ul>
1790<p>When using implicit headers (not calling <a href="#http_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a> explicitly),
1791this property controls the status message that will be sent to the client when
1792the headers get flushed. If this is left as <code>undefined</code> then the standard
1793message for the status code will be used.</p>
1794<pre><code class="language-js">response.<span class="hljs-property">statusMessage</span> = <span class="hljs-string">'Not found'</span>;</code></pre>
1795<p>After response header was sent to the client, this property indicates the
1796status message which was sent out.</p>
1797<h4><code>response.uncork()</code><span><a class="mark" href="#http_response_uncork" id="http_response_uncork">#</a></span></h4>
1798<div class="api_metadata">
1799<span>Added in: v13.2.0, v12.16.0</span>
1800</div>
1801<p>See <a href="stream.html#stream_writable_uncork"><code>writable.uncork()</code></a>.</p>
1802<h4><code>response.writableEnded</code><span><a class="mark" href="#http_response_writableended" id="http_response_writableended">#</a></span></h4>
1803<div class="api_metadata">
1804<span>Added in: v12.9.0</span>
1805</div>
1806<ul>
1807<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1808</ul>
1809<p>Is <code>true</code> after <a href="#http_response_end_data_encoding_callback"><code>response.end()</code></a> has been called. This property
1810does not indicate whether the data has been flushed, for this use
1811<a href="#http_response_writablefinished"><code>response.writableFinished</code></a> instead.</p>
1812<h4><code>response.writableFinished</code><span><a class="mark" href="#http_response_writablefinished" id="http_response_writablefinished">#</a></span></h4>
1813<div class="api_metadata">
1814<span>Added in: v12.7.0</span>
1815</div>
1816<ul>
1817<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1818</ul>
1819<p>Is <code>true</code> if all data has been flushed to the underlying system, immediately
1820before the <a href="#http_event_finish"><code>'finish'</code></a> event is emitted.</p>
1821<h4><code>response.write(chunk[, encoding][, callback])</code><span><a class="mark" href="#http_response_write_chunk_encoding_callback" id="http_response_write_chunk_encoding_callback">#</a></span></h4>
1822<div class="api_metadata">
1823<span>Added in: v0.1.29</span>
1824</div>
1825<ul>
1826<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#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
1827<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>
1828<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>
1829<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1830</ul>
1831<p>If this method is called and <a href="#http_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a> has not been called,
1832it will switch to implicit header mode and flush the implicit headers.</p>
1833<p>This sends a chunk of the response body. This method may
1834be called multiple times to provide successive parts of the body.</p>
1835<p>In the <code>http</code> module, the response body is omitted when the
1836request is a HEAD request. Similarly, the <code>204</code> and <code>304</code> responses
1837<em>must not</em> include a message body.</p>
1838<p><code>chunk</code> can be a string or a buffer. If <code>chunk</code> is a string,
1839the second parameter specifies how to encode it into a byte stream.
1840<code>callback</code> will be called when this chunk of data is flushed.</p>
1841<p>This is the raw HTTP body and has nothing to do with higher-level multi-part
1842body encodings that may be used.</p>
1843<p>The first time <a href="#http_response_write_chunk_encoding_callback"><code>response.write()</code></a> is called, it will send the buffered
1844header information and the first chunk of the body to the client. The second
1845time <a href="#http_response_write_chunk_encoding_callback"><code>response.write()</code></a> is called, Node.js assumes data will be streamed,
1846and sends the new data separately. That is, the response is buffered up to the
1847first chunk of the body.</p>
1848<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel
1849buffer. Returns <code>false</code> if all or part of the data was queued in user memory.
1850<code>'drain'</code> will be emitted when the buffer is free again.</p>
1851<h4><code>response.writeContinue()</code><span><a class="mark" href="#http_response_writecontinue" id="http_response_writecontinue">#</a></span></h4>
1852<div class="api_metadata">
1853<span>Added in: v0.3.0</span>
1854</div>
1855<p>Sends a HTTP/1.1 100 Continue message to the client, indicating that
1856the request body should be sent. See the <a href="#http_event_checkcontinue"><code>'checkContinue'</code></a> event on
1857<code>Server</code>.</p>
1858<h4><code>response.writeHead(statusCode[, statusMessage][, headers])</code><span><a class="mark" href="#http_response_writehead_statuscode_statusmessage_headers" id="http_response_writehead_statuscode_statusmessage_headers">#</a></span></h4>
1859<div class="api_metadata">
1860<details class="changelog"><summary>History</summary>
1861<table>
1862<tbody><tr><th>Version</th><th>Changes</th></tr>
1863<tr><td>v14.14.0</td>
1864<td><p>Allow passing headers as an array.</p></td></tr>
1865<tr><td>v11.10.0, v10.17.0</td>
1866<td><p>Return <code>this</code> from <code>writeHead()</code> to allow chaining with <code>end()</code>.</p></td></tr>
1867<tr><td>v5.11.0, v4.4.5</td>
1868<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>
1869<tr><td>v0.1.30</td>
1870<td><p><span>Added in: v0.1.30</span></p></td></tr>
1871</tbody></table>
1872</details>
1873</div>
1874<ul>
1875<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>
1876<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>
1877<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>
1878<li>Returns: <a href="http.html#http_class_http_serverresponse" class="type">&#x3C;http.ServerResponse></a></li>
1879</ul>
1880<p>Sends a response header to the request. The status code is a 3-digit HTTP
1881status code, like <code>404</code>. The last argument, <code>headers</code>, are the response headers.
1882Optionally one can give a human-readable <code>statusMessage</code> as the second
1883argument.</p>
1884<p><code>headers</code> may be an <code>Array</code> where the keys and values are in the same list.
1885It is <em>not</em> a list of tuples. So, the even-numbered offsets are key values,
1886and the odd-numbered offsets are the associated values. The array is in the same
1887format as <code>request.rawHeaders</code>.</p>
1888<p>Returns a reference to the <code>ServerResponse</code>, so that calls can be chained.</p>
1889<pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-string">'hello world'</span>;
1890response
1891  .<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, {
1892    <span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(body),
1893    <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span>
1894  })
1895  .<span class="hljs-title function_">end</span>(body);</code></pre>
1896<p>This method must only be called once on a message and it must
1897be called before <a href="#http_response_end_data_encoding_callback"><code>response.end()</code></a> is called.</p>
1898<p>If <a href="#http_response_write_chunk_encoding_callback"><code>response.write()</code></a> or <a href="#http_response_end_data_encoding_callback"><code>response.end()</code></a> are called before calling
1899this, the implicit/mutable headers will be calculated and call this function.</p>
1900<p>When headers have been set with <a href="#http_response_setheader_name_value"><code>response.setHeader()</code></a>, they will be merged
1901with any headers passed to <a href="#http_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a>, with the headers passed
1902to <a href="#http_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a> given precedence.</p>
1903<p>If this method is called and <a href="#http_response_setheader_name_value"><code>response.setHeader()</code></a> has not been called,
1904it will directly write the supplied header values onto the network channel
1905without caching internally, and the <a href="#http_response_getheader_name"><code>response.getHeader()</code></a> on the header
1906will not yield the expected result. If progressive population of headers is
1907desired with potential future retrieval and modification, use
1908<a href="#http_response_setheader_name_value"><code>response.setHeader()</code></a> instead.</p>
1909<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span>
1910<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> {
1911  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>);
1912  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
1913  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> });
1914  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
1915});</code></pre>
1916<p><code>Content-Length</code> is given in bytes, not characters. Use
1917<a href="buffer.html#buffer_static_method_buffer_bytelength_string_encoding"><code>Buffer.byteLength()</code></a> to determine the length of the body in bytes. Node.js
1918does not check whether <code>Content-Length</code> and the length of the body which has
1919been transmitted are equal or not.</p>
1920<p>Attempting to set a header field name or value that contains invalid characters
1921will result in a <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> being thrown.</p>
1922<h4><code>response.writeProcessing()</code><span><a class="mark" href="#http_response_writeprocessing" id="http_response_writeprocessing">#</a></span></h4>
1923<div class="api_metadata">
1924<span>Added in: v10.0.0</span>
1925</div>
1926<p>Sends a HTTP/1.1 102 Processing message to the client, indicating that
1927the request body should be sent.</p>
1928</section><section><h3>Class: <code>http.IncomingMessage</code><span><a class="mark" href="#http_class_http_incomingmessage" id="http_class_http_incomingmessage">#</a></span></h3>
1929<div class="api_metadata">
1930<details class="changelog"><summary>History</summary>
1931<table>
1932<tbody><tr><th>Version</th><th>Changes</th></tr>
1933<tr><td>v13.1.0, v12.16.0</td>
1934<td><p>The <code>readableHighWaterMark</code> value mirrors that of the socket.</p></td></tr>
1935<tr><td>v0.1.17</td>
1936<td><p><span>Added in: v0.1.17</span></p></td></tr>
1937</tbody></table>
1938</details>
1939</div>
1940<ul>
1941<li>Extends: <a href="stream.html#stream_class_stream_readable" class="type">&#x3C;stream.Readable></a></li>
1942</ul>
1943<p>An <code>IncomingMessage</code> object is created by <a href="#http_class_http_server"><code>http.Server</code></a> or
1944<a href="#http_class_http_clientrequest"><code>http.ClientRequest</code></a> and passed as the first argument to the <a href="#http_event_request"><code>'request'</code></a>
1945and <a href="#http_event_response"><code>'response'</code></a> event respectively. It may be used to access response
1946status, headers and data.</p>
1947<h4>Event: <code>'aborted'</code><span><a class="mark" href="#http_event_aborted" id="http_event_aborted">#</a></span></h4>
1948<div class="api_metadata">
1949<span>Added in: v0.3.8</span>
1950</div>
1951<p>Emitted when the request has been aborted.</p>
1952<h4>Event: <code>'close'</code><span><a class="mark" href="#http_event_close_2" id="http_event_close_2">#</a></span></h4>
1953<div class="api_metadata">
1954<span>Added in: v0.4.2</span>
1955</div>
1956<p>Indicates that the underlying connection was closed.</p>
1957<h4><code>message.aborted</code><span><a class="mark" href="#http_message_aborted" id="http_message_aborted">#</a></span></h4>
1958<div class="api_metadata">
1959<span>Added in: v10.1.0</span>
1960</div>
1961<ul>
1962<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1963</ul>
1964<p>The <code>message.aborted</code> property will be <code>true</code> if the request has
1965been aborted.</p>
1966<h4><code>message.complete</code><span><a class="mark" href="#http_message_complete" id="http_message_complete">#</a></span></h4>
1967<div class="api_metadata">
1968<span>Added in: v0.3.0</span>
1969</div>
1970<ul>
1971<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1972</ul>
1973<p>The <code>message.complete</code> property will be <code>true</code> if a complete HTTP message has
1974been received and successfully parsed.</p>
1975<p>This property is particularly useful as a means of determining if a client or
1976server fully transmitted a message before a connection was terminated:</p>
1977<pre><code class="language-js"><span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>({
1978  <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
1979  <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
1980  <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>
1981}, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
1982  res.<span class="hljs-title function_">resume</span>();
1983  res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
1984    <span class="hljs-keyword">if</span> (!res.<span class="hljs-property">complete</span>)
1985      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(
1986        <span class="hljs-string">'The connection was terminated while the message was still being sent'</span>);
1987  });
1988});</code></pre>
1989<h4><code>message.destroy([error])</code><span><a class="mark" href="#http_message_destroy_error" id="http_message_destroy_error">#</a></span></h4>
1990<div class="api_metadata">
1991<details class="changelog"><summary>History</summary>
1992<table>
1993<tbody><tr><th>Version</th><th>Changes</th></tr>
1994<tr><td>v14.5.0</td>
1995<td><p>The function returns <code>this</code> for consistency with other Readable streams.</p></td></tr>
1996<tr><td>v0.3.0</td>
1997<td><p><span>Added in: v0.3.0</span></p></td></tr>
1998</tbody></table>
1999</details>
2000</div>
2001<ul>
2002<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>
2003<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type">&#x3C;this></a></li>
2004</ul>
2005<p>Calls <code>destroy()</code> on the socket that received the <code>IncomingMessage</code>. If <code>error</code>
2006is provided, an <code>'error'</code> event is emitted on the socket and <code>error</code> is passed
2007as an argument to any listeners on the event.</p>
2008<h4><code>message.headers</code><span><a class="mark" href="#http_message_headers" id="http_message_headers">#</a></span></h4>
2009<div class="api_metadata">
2010<span>Added in: v0.1.5</span>
2011</div>
2012<ul>
2013<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
2014</ul>
2015<p>The request/response headers object.</p>
2016<p>Key-value pairs of header names and values. Header names are lower-cased.</p>
2017<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
2018<span class="hljs-comment">//</span>
2019<span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span>
2020<span class="hljs-comment">//   host: '127.0.0.1:8000',</span>
2021<span class="hljs-comment">//   accept: '*/*' }</span>
2022<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(request.<span class="hljs-property">headers</span>);</code></pre>
2023<p>Duplicates in raw headers are handled in the following ways, depending on the
2024header name:</p>
2025<ul>
2026<li>Duplicates of <code>age</code>, <code>authorization</code>, <code>content-length</code>, <code>content-type</code>,
2027<code>etag</code>, <code>expires</code>, <code>from</code>, <code>host</code>, <code>if-modified-since</code>, <code>if-unmodified-since</code>,
2028<code>last-modified</code>, <code>location</code>, <code>max-forwards</code>, <code>proxy-authorization</code>, <code>referer</code>,
2029<code>retry-after</code>, <code>server</code>, or <code>user-agent</code> are discarded.</li>
2030<li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li>
2031<li>For duplicate <code>cookie</code> headers, the values are joined together with '; '.</li>
2032<li>For all other headers, the values are joined together with ', '.</li>
2033</ul>
2034<h4><code>message.httpVersion</code><span><a class="mark" href="#http_message_httpversion" id="http_message_httpversion">#</a></span></h4>
2035<div class="api_metadata">
2036<span>Added in: v0.1.1</span>
2037</div>
2038<ul>
2039<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2040</ul>
2041<p>In case of server request, the HTTP version sent by the client. In the case of
2042client response, the HTTP version of the connected-to server.
2043Probably either <code>'1.1'</code> or <code>'1.0'</code>.</p>
2044<p>Also <code>message.httpVersionMajor</code> is the first integer and
2045<code>message.httpVersionMinor</code> is the second.</p>
2046<h4><code>message.method</code><span><a class="mark" href="#http_message_method" id="http_message_method">#</a></span></h4>
2047<div class="api_metadata">
2048<span>Added in: v0.1.1</span>
2049</div>
2050<ul>
2051<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2052</ul>
2053<p><strong>Only valid for request obtained from <a href="#http_class_http_server"><code>http.Server</code></a>.</strong></p>
2054<p>The request method as a string. Read only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.</p>
2055<h4><code>message.rawHeaders</code><span><a class="mark" href="#http_message_rawheaders" id="http_message_rawheaders">#</a></span></h4>
2056<div class="api_metadata">
2057<span>Added in: v0.11.6</span>
2058</div>
2059<ul>
2060<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
2061</ul>
2062<p>The raw request/response headers list exactly as they were received.</p>
2063<p>The keys and values are in the same list. It is <em>not</em> a
2064list of tuples. So, the even-numbered offsets are key values, and the
2065odd-numbered offsets are the associated values.</p>
2066<p>Header names are not lowercased, and duplicates are not merged.</p>
2067<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
2068<span class="hljs-comment">//</span>
2069<span class="hljs-comment">// [ 'user-agent',</span>
2070<span class="hljs-comment">//   'this is invalid because there can be only one',</span>
2071<span class="hljs-comment">//   'User-Agent',</span>
2072<span class="hljs-comment">//   'curl/7.22.0',</span>
2073<span class="hljs-comment">//   'Host',</span>
2074<span class="hljs-comment">//   '127.0.0.1:8000',</span>
2075<span class="hljs-comment">//   'ACCEPT',</span>
2076<span class="hljs-comment">//   '*/*' ]</span>
2077<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(request.<span class="hljs-property">rawHeaders</span>);</code></pre>
2078<h4><code>message.rawTrailers</code><span><a class="mark" href="#http_message_rawtrailers" id="http_message_rawtrailers">#</a></span></h4>
2079<div class="api_metadata">
2080<span>Added in: v0.11.6</span>
2081</div>
2082<ul>
2083<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
2084</ul>
2085<p>The raw request/response trailer keys and values exactly as they were
2086received. Only populated at the <code>'end'</code> event.</p>
2087<h4><code>message.setTimeout(msecs[, callback])</code><span><a class="mark" href="#http_message_settimeout_msecs_callback" id="http_message_settimeout_msecs_callback">#</a></span></h4>
2088<div class="api_metadata">
2089<span>Added in: v0.5.9</span>
2090</div>
2091<ul>
2092<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>
2093<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>
2094<li>Returns: <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a></li>
2095</ul>
2096<p>Calls <code>message.socket.setTimeout(msecs, callback)</code>.</p>
2097<h4><code>message.socket</code><span><a class="mark" href="#http_message_socket" id="http_message_socket">#</a></span></h4>
2098<div class="api_metadata">
2099<span>Added in: v0.3.0</span>
2100</div>
2101<ul>
2102<li><a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
2103</ul>
2104<p>The <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> object associated with the connection.</p>
2105<p>With HTTPS support, use <a href="tls.html#tls_tlssocket_getpeercertificate_detailed"><code>request.socket.getPeerCertificate()</code></a> to obtain the
2106client's authentication details.</p>
2107<p>This property is guaranteed to be an instance of the <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> class,
2108a subclass of <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>, unless the user specified a socket
2109type other than <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a>.</p>
2110<h4><code>message.statusCode</code><span><a class="mark" href="#http_message_statuscode" id="http_message_statuscode">#</a></span></h4>
2111<div class="api_metadata">
2112<span>Added in: v0.1.1</span>
2113</div>
2114<ul>
2115<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2116</ul>
2117<p><strong>Only valid for response obtained from <a href="#http_class_http_clientrequest"><code>http.ClientRequest</code></a>.</strong></p>
2118<p>The 3-digit HTTP response status code. E.G. <code>404</code>.</p>
2119<h4><code>message.statusMessage</code><span><a class="mark" href="#http_message_statusmessage" id="http_message_statusmessage">#</a></span></h4>
2120<div class="api_metadata">
2121<span>Added in: v0.11.10</span>
2122</div>
2123<ul>
2124<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2125</ul>
2126<p><strong>Only valid for response obtained from <a href="#http_class_http_clientrequest"><code>http.ClientRequest</code></a>.</strong></p>
2127<p>The HTTP response status message (reason phrase). E.G. <code>OK</code> or <code>Internal Server Error</code>.</p>
2128<h4><code>message.trailers</code><span><a class="mark" href="#http_message_trailers" id="http_message_trailers">#</a></span></h4>
2129<div class="api_metadata">
2130<span>Added in: v0.3.0</span>
2131</div>
2132<ul>
2133<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
2134</ul>
2135<p>The request/response trailers object. Only populated at the <code>'end'</code> event.</p>
2136<h4><code>message.url</code><span><a class="mark" href="#http_message_url" id="http_message_url">#</a></span></h4>
2137<div class="api_metadata">
2138<span>Added in: v0.1.90</span>
2139</div>
2140<ul>
2141<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2142</ul>
2143<p><strong>Only valid for request obtained from <a href="#http_class_http_server"><code>http.Server</code></a>.</strong></p>
2144<p>Request URL string. This contains only the URL that is present in the actual
2145HTTP request. Take the following request:</p>
2146<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>
2147<span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code></pre>
2148<p>To parse the URL into its parts:</p>
2149<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></pre>
2150<p>When <code>request.url</code> is <code>'/status?name=ryan'</code> and
2151<code>request.headers.host</code> is <code>'localhost:3000'</code>:</p>
2152<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node</span>
2153<span class="hljs-meta">> </span><span class="language-bash">new URL(request.url, `http://<span class="hljs-variable">${request.headers.host}</span>`)</span>
2154URL {
2155  href: 'http://localhost:3000/status?name=ryan',
2156  origin: 'http://localhost:3000',
2157  protocol: 'http:',
2158  username: '',
2159  password: '',
2160  host: 'localhost:3000',
2161  hostname: 'localhost',
2162  port: '3000',
2163  pathname: '/status',
2164  search: '?name=ryan',
2165  searchParams: URLSearchParams { 'name' => 'ryan' },
2166  hash: ''
2167}</code></pre>
2168</section><section><h3>Class: <code>http.OutgoingMessage</code><span><a class="mark" href="#http_class_http_outgoingmessage" id="http_class_http_outgoingmessage">#</a></span></h3>
2169<div class="api_metadata">
2170<span>Added in: v0.1.17</span>
2171</div>
2172<ul>
2173<li>Extends: <a href="stream.html#stream_stream" class="type">&#x3C;Stream></a></li>
2174</ul>
2175<p>This class serves as the parent class of <a href="#http_class_http_clientrequest"><code>http.ClientRequest</code></a>
2176and <a href="#http_class_http_serverresponse"><code>http.ServerResponse</code></a>. It is an abstract of outgoing message from
2177the perspective of the participants of HTTP transaction.</p>
2178<h4>Event: <code>drain</code><span><a class="mark" href="#http_event_drain" id="http_event_drain">#</a></span></h4>
2179<div class="api_metadata">
2180<span>Added in: v0.3.6</span>
2181</div>
2182<p>Emitted when the buffer of the message is free again.</p>
2183<h4>Event: <code>finish</code><span><a class="mark" href="#http_event_finish_1" id="http_event_finish_1">#</a></span></h4>
2184<div class="api_metadata">
2185<span>Added in: v0.1.17</span>
2186</div>
2187<p>Emitted when the transmission is finished successfully.</p>
2188<h4>Event: <code>prefinish</code><span><a class="mark" href="#http_event_prefinish" id="http_event_prefinish">#</a></span></h4>
2189<div class="api_metadata">
2190<span>Added in: v0.11.6</span>
2191</div>
2192<p>Emitted when <code>outgoingMessage.end</code> was called.
2193When the event is emitted, all data has been processed but not necessarily
2194completely flushed.</p>
2195<h4><code>outgoingMessage.addTrailers(headers)</code><span><a class="mark" href="#http_outgoingmessage_addtrailers_headers" id="http_outgoingmessage_addtrailers_headers">#</a></span></h4>
2196<div class="api_metadata">
2197<span>Added in: v0.3.0</span>
2198</div>
2199<ul>
2200<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>
2201</ul>
2202<p>Adds HTTP trailers (headers but at the end of the message) to the message.</p>
2203<p>Trailers are <strong>only</strong> be emitted if the message is chunked encoded. If not,
2204the trailer will be silently discarded.</p>
2205<p>HTTP requires the  <code>Trailer</code> header to be sent to emit trailers,
2206with a list of header fields in its value, e.g.</p>
2207<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>,
2208                         <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> });
2209message.<span class="hljs-title function_">write</span>(fileData);
2210message.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-MD5'</span>: <span class="hljs-string">'7895bf4b8828b55ceaf47747b4bca667'</span> });
2211message.<span class="hljs-title function_">end</span>();</code></pre>
2212<p>Attempting to set a header field name or value that contains invalid characters
2213will result in a <code>TypeError</code> being thrown.</p>
2214<h4><code>outgoingMessage.connection</code><span><a class="mark" href="#http_outgoingmessage_connection" id="http_outgoingmessage_connection">#</a></span></h4>
2215<div class="api_metadata">
2216<span>Added in: v0.3.0</span><span>Deprecated since: v14.17.1</span>
2217</div>
2218<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#http_outgoingmessage_socket"><code>outgoingMessage.socket</code></a> instead.</div><p></p>
2219<p>Aliases of <code>outgoingMessage.socket</code></p>
2220<h4><code>outgoingMessage.cork()</code><span><a class="mark" href="#http_outgoingmessage_cork" id="http_outgoingmessage_cork">#</a></span></h4>
2221<div class="api_metadata">
2222<span>Added in: v14.0.0</span>
2223</div>
2224<p>See <a href="stream.html#stream_writable_cork"><code>writable.cork()</code></a>.</p>
2225<h4><code>outgoingMessage.destroy([error])</code><span><a class="mark" href="#http_outgoingmessage_destroy_error" id="http_outgoingmessage_destroy_error">#</a></span></h4>
2226<div class="api_metadata">
2227<span>Added in: v0.3.0</span>
2228</div>
2229<ul>
2230<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>
2231<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type">&#x3C;this></a></li>
2232</ul>
2233<p>Destroys the message. Once a socket is associated with the message
2234and is connected, that socket will be destroyed as well.</p>
2235<h4><code>outgoingMessage.end(chunk[, encoding][, callback])</code><span><a class="mark" href="#http_outgoingmessage_end_chunk_encoding_callback" id="http_outgoingmessage_end_chunk_encoding_callback">#</a></span></h4>
2236<div class="api_metadata">
2237<details class="changelog"><summary>History</summary>
2238<table>
2239<tbody><tr><th>Version</th><th>Changes</th></tr>
2240<tr><td>v0.11.6</td>
2241<td><p>add <code>callback</code> argument.</p></td></tr>
2242<tr><td>v0.1.90</td>
2243<td><p><span>Added in: v0.1.90</span></p></td></tr>
2244</tbody></table>
2245</details>
2246</div>
2247<ul>
2248<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#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2249<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>utf-8</code></li>
2250<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>
2251<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type">&#x3C;this></a></li>
2252</ul>
2253<p>Finishes the outgoing message. If any parts of the body are unsent, it will
2254flush them to the underlying system. If the message is chunked, it will
2255send the terminating chunk <code>0\r\n\r\n</code>, and send the trailer (if any).</p>
2256<p>If <code>chunk</code> is specified, it is equivalent to call
2257<code>outgoingMessage.write(chunk, encoding)</code>, followed by
2258<code>outgoingMessage.end(callback)</code>.</p>
2259<p>If <code>callback</code> is provided, it will be called when the message is finished.
2260(equivalent to the callback to event <code>finish</code>)</p>
2261<h4><code>outgoingMessage.flushHeaders()</code><span><a class="mark" href="#http_outgoingmessage_flushheaders" id="http_outgoingmessage_flushheaders">#</a></span></h4>
2262<div class="api_metadata">
2263<span>Added in: v1.6.0</span>
2264</div>
2265<p>Compulsorily flushes the message headers</p>
2266<p>For efficiency reason, Node.js normally buffers the message headers
2267until <code>outgoingMessage.end()</code> is called or the first chunk of message data
2268is written. It then tries to pack the headers and data into a single TCP
2269packet.</p>
2270<p>It is usually desired (it saves a TCP round-trip), but not when the first
2271data is not sent until possibly much later. <code>outgoingMessage.flushHeaders()</code>
2272bypasses the optimization and kickstarts the request.</p>
2273<h4><code>outgoingMessage.getHeader(name)</code><span><a class="mark" href="#http_outgoingmessage_getheader_name" id="http_outgoingmessage_getheader_name">#</a></span></h4>
2274<div class="api_metadata">
2275<span>Added in: v0.4.0</span>
2276</div>
2277<ul>
2278<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>
2279<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>
2280</ul>
2281<p>Gets the value of HTTP header with the given name. If such a name doesn't
2282exist in message, it will be <code>undefined</code>.</p>
2283<h4><code>outgoingMessage.getHeaderNames()</code><span><a class="mark" href="#http_outgoingmessage_getheadernames" id="http_outgoingmessage_getheadernames">#</a></span></h4>
2284<div class="api_metadata">
2285<span>Added in: v8.0.0</span>
2286</div>
2287<ul>
2288<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
2289</ul>
2290<p>Returns an array of names of headers of the outgoing outgoingMessage. All
2291names are lowercase.</p>
2292<h4><code>outgoingMessage.getHeaders()</code><span><a class="mark" href="#http_outgoingmessage_getheaders" id="http_outgoingmessage_getheaders">#</a></span></h4>
2293<div class="api_metadata">
2294<span>Added in: v8.0.0</span>
2295</div>
2296<ul>
2297<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
2298</ul>
2299<p>Returns a shallow copy of the current outgoing headers. Since a shallow
2300copy is used, array values may be mutated without additional calls to
2301various header-related HTTP module methods. The keys of the returned
2302object are the header names and the values are the respective header
2303values. All header names are lowercase.</p>
2304<p>The object returned by the <code>outgoingMessage.getHeaders()</code> method does
2305not prototypically inherit from the JavaScript Object. This means that
2306typical Object methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>,
2307and others are not defined and will not work.</p>
2308<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>);
2309outgoingMessage.<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>]);
2310
2311<span class="hljs-keyword">const</span> headers = outgoingMessage.<span class="hljs-title function_">getHeaders</span>();
2312<span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</span></code></pre>
2313<h4><code>outgoingMessage.hasHeader(name)</code><span><a class="mark" href="#http_outgoingmessage_hasheader_name" id="http_outgoingmessage_hasheader_name">#</a></span></h4>
2314<div class="api_metadata">
2315<span>Added in: v8.0.0</span>
2316</div>
2317<ul>
2318<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>
2319<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
2320</ul>
2321<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the
2322outgoing headers. The header name is case-insensitive.</p>
2323<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></pre>
2324<h4><code>outgoingMessage.headersSent</code><span><a class="mark" href="#http_outgoingmessage_headerssent" id="http_outgoingmessage_headerssent">#</a></span></h4>
2325<div class="api_metadata">
2326<span>Added in: v0.9.3</span>
2327</div>
2328<ul>
2329<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
2330</ul>
2331<p>Read-only. <code>true</code> if the headers were sent, otherwise <code>false</code>.</p>
2332<h4><code>outgoingMessage.pipe()</code><span><a class="mark" href="#http_outgoingmessage_pipe" id="http_outgoingmessage_pipe">#</a></span></h4>
2333<div class="api_metadata">
2334<span>Added in: v9.0.0</span>
2335</div>
2336<p>Overrides the pipe method of legacy <code>Stream</code> which is the parent class of
2337<code>http.outgoingMessage</code>.</p>
2338<p>Since <code>OutgoingMessage</code> should be a write-only stream,
2339call this function will throw an <code>Error</code>. Thus, it disabled the pipe method
2340it inherits from <code>Stream</code>.</p>
2341<p>The User should not call this function directly.</p>
2342<h4><code>outgoingMessage.removeHeader()</code><span><a class="mark" href="#http_outgoingmessage_removeheader" id="http_outgoingmessage_removeheader">#</a></span></h4>
2343<div class="api_metadata">
2344<span>Added in: v0.4.0</span>
2345</div>
2346<p>Removes a header that is queued for implicit sending.</p>
2347<pre><code class="language-js">outgoingMessage.<span class="hljs-title function_">removeHeader</span>(<span class="hljs-string">'Content-Encoding'</span>);</code></pre>
2348<h4><code>outgoingMessage.setHeader(name, value)</code><span><a class="mark" href="#http_outgoingmessage_setheader_name_value" id="http_outgoingmessage_setheader_name_value">#</a></span></h4>
2349<div class="api_metadata">
2350<span>Added in: v0.4.0</span>
2351</div>
2352<ul>
2353<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>
2354<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Header value</li>
2355<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type">&#x3C;this></a></li>
2356</ul>
2357<p>Sets a single header value for the header object.</p>
2358<h4><code>outgoingMessage.setTimeout(msesc[, callback])</code><span><a class="mark" href="#http_outgoingmessage_settimeout_msesc_callback" id="http_outgoingmessage_settimeout_msesc_callback">#</a></span></h4>
2359<div class="api_metadata">
2360<span>Added in: v0.9.12</span>
2361</div>
2362<ul>
2363<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>
2364<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</li>
2365</ul>
2366<p>occurs, Same as binding to the <code>timeout</code> event.</p>
2367<ul>
2368<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type">&#x3C;this></a></li>
2369</ul>
2370<p>Once a socket is associated with the message and is connected,
2371<a href="net.html#net_socket_settimeout_timeout_callback"><code>socket.setTimeout()</code></a> will be called with <code>msecs</code> as the first parameter.</p>
2372<h4><code>outgoingMessage.socket</code><span><a class="mark" href="#http_outgoingmessage_socket" id="http_outgoingmessage_socket">#</a></span></h4>
2373<div class="api_metadata">
2374<span>Added in: v0.3.0</span>
2375</div>
2376<ul>
2377<li><a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
2378</ul>
2379<p>Reference to the underlying socket. Usually, users will not want to access
2380this property.</p>
2381<p>After calling <code>outgoingMessage.end()</code>, this property will be nulled.</p>
2382<h4><code>outgoingMessage.uncork()</code><span><a class="mark" href="#http_outgoingmessage_uncork" id="http_outgoingmessage_uncork">#</a></span></h4>
2383<div class="api_metadata">
2384<span>Added in: v14.0.0</span>
2385</div>
2386<p>See <a href="stream.html#stream_writable_uncork"><code>writable.uncork()</code></a></p>
2387<h4><code>outgoingMessage.writableCorked</code><span><a class="mark" href="#http_outgoingmessage_writablecorked" id="http_outgoingmessage_writablecorked">#</a></span></h4>
2388<div class="api_metadata">
2389<span>Added in: v14.0.0</span>
2390</div>
2391<ul>
2392<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2393</ul>
2394<p>This <code>outgoingMessage.writableCorked</code> will return the time how many
2395<code>outgoingMessage.cork()</code> have been called.</p>
2396<h4><code>outgoingMessage.writableEnded</code><span><a class="mark" href="#http_outgoingmessage_writableended" id="http_outgoingmessage_writableended">#</a></span></h4>
2397<div class="api_metadata">
2398<span>Added in: v13.0.0</span>
2399</div>
2400<ul>
2401<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
2402</ul>
2403<p>Readonly, <code>true</code> if <code>outgoingMessage.end()</code> has been called. Noted that
2404this property does not reflect whether the data has been flush. For that
2405purpose, use <code>message.writableFinished</code> instead.</p>
2406<h4><code>outgoingMessage.writableFinished</code><span><a class="mark" href="#http_outgoingmessage_writablefinished" id="http_outgoingmessage_writablefinished">#</a></span></h4>
2407<div class="api_metadata">
2408<span>Added in: v13.0.0</span>
2409</div>
2410<ul>
2411<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
2412</ul>
2413<p>Readonly. <code>true</code> if all data has been flushed to the underlying system.</p>
2414<h4><code>outgoingMessage.writableHighWaterMark</code><span><a class="mark" href="#http_outgoingmessage_writablehighwatermark" id="http_outgoingmessage_writablehighwatermark">#</a></span></h4>
2415<div class="api_metadata">
2416<span>Added in: v13.0.0</span>
2417</div>
2418<ul>
2419<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2420</ul>
2421<p>This <code>outgoingMessage.writableHighWaterMark</code> will be the <code>highWaterMark</code> of
2422underlying socket if socket exists. Else, it would be the default
2423<code>highWaterMark</code>.</p>
2424<p><code>highWaterMark</code> is the maximum amount of data that can be potentially
2425buffered by the socket.</p>
2426<h4><code>outgoingMessage.writableLength</code><span><a class="mark" href="#http_outgoingmessage_writablelength" id="http_outgoingmessage_writablelength">#</a></span></h4>
2427<div class="api_metadata">
2428<span>Added in: v13.0.0</span>
2429</div>
2430<ul>
2431<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2432</ul>
2433<p>Readonly, This <code>outgoingMessage.writableLength</code> contains the number of
2434bytes (or objects) in the buffer ready to send.</p>
2435<h4><code>outgoingMessage.writableObjectMode</code><span><a class="mark" href="#http_outgoingmessage_writableobjectmode" id="http_outgoingmessage_writableobjectmode">#</a></span></h4>
2436<div class="api_metadata">
2437<span>Added in: v13.0.0</span>
2438</div>
2439<ul>
2440<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
2441</ul>
2442<p>Readonly, always returns <code>false</code>.</p>
2443<h4><code>outgoingMessage.write(chunk[, encoding][, callback])</code><span><a class="mark" href="#http_outgoingmessage_write_chunk_encoding_callback" id="http_outgoingmessage_write_chunk_encoding_callback">#</a></span></h4>
2444<div class="api_metadata">
2445<details class="changelog"><summary>History</summary>
2446<table>
2447<tbody><tr><th>Version</th><th>Changes</th></tr>
2448<tr><td>v0.11.6</td>
2449<td><p>add <code>callback</code> argument.</p></td></tr>
2450<tr><td>v0.1.29</td>
2451<td><p><span>Added in: v0.1.29</span></p></td></tr>
2452</tbody></table>
2453</details>
2454</div>
2455<ul>
2456<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#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2457<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>utf-8</code></li>
2458<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>
2459<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
2460</ul>
2461<p>If this method is called and the header is not sent, it will call
2462<code>this._implicitHeader</code> to flush implicit header.
2463If the message should not have a body (indicated by <code>this._hasBody</code>),
2464the call is ignored and <code>chunk</code> will not be sent. It could be useful
2465when handling a particular message which must not include a body.
2466e.g. response to <code>HEAD</code> request, <code>204</code> and <code>304</code> response.</p>
2467<p><code>chunk</code> can be a string or a buffer. When <code>chunk</code> is a string, the
2468<code>encoding</code> parameter specifies how to encode <code>chunk</code> into a byte stream.
2469<code>callback</code> will be called when the <code>chunk</code> is flushed.</p>
2470<p>If the message is transferred in chucked encoding
2471(indicated by <code>this.chunkedEncoding</code>), <code>chunk</code> will be flushed as
2472one chunk among a stream of chunks. Otherwise, it will be flushed as the
2473body of message.</p>
2474<p>This method handles the raw body of the HTTP message and has nothing to do
2475with higher-level multi-part body encodings that may be used.</p>
2476<p>If it is the first call to this method of a message, it will send the
2477buffered header first, then flush the <code>chunk</code> as described above.</p>
2478<p>The second and successive calls to this method will assume the data
2479will be streamed and send the new data separately. It means that the response
2480is buffered up to the first chunk of the body.</p>
2481<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel
2482buffer. Returns <code>false</code> if all or part of the data was queued in the user
2483memory. Event <code>drain</code> will be emitted when the buffer is free again.</p>
2484</section><section><h3><code>http.METHODS</code><span><a class="mark" href="#http_http_methods" id="http_http_methods">#</a></span></h3>
2485<div class="api_metadata">
2486<span>Added in: v0.11.8</span>
2487</div>
2488<ul>
2489<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
2490</ul>
2491<p>A list of the HTTP methods that are supported by the parser.</p>
2492</section><section><h3><code>http.STATUS_CODES</code><span><a class="mark" href="#http_http_status_codes" id="http_http_status_codes">#</a></span></h3>
2493<div class="api_metadata">
2494<span>Added in: v0.1.22</span>
2495</div>
2496<ul>
2497<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
2498</ul>
2499<p>A collection of all the standard HTTP response status codes, and the
2500short description of each. For example, <code>http.STATUS_CODES[404] === 'Not Found'</code>.</p>
2501</section><section><h3><code>http.createServer([options][, requestListener])</code><span><a class="mark" href="#http_http_createserver_options_requestlistener" id="http_http_createserver_options_requestlistener">#</a></span></h3>
2502<div class="api_metadata">
2503<details class="changelog"><summary>History</summary>
2504<table>
2505<tbody><tr><th>Version</th><th>Changes</th></tr>
2506<tr><td>v13.3.0</td>
2507<td><p>The <code>maxHeaderSize</code> option is supported now.</p></td></tr>
2508<tr><td>v13.8.0, v12.15.0, v10.19.0</td>
2509<td><p>The <code>insecureHTTPParser</code> option is supported now.</p></td></tr>
2510<tr><td>v9.6.0, v8.12.0</td>
2511<td><p>The <code>options</code> argument is supported now.</p></td></tr>
2512<tr><td>v0.1.13</td>
2513<td><p><span>Added in: v0.1.13</span></p></td></tr>
2514</tbody></table>
2515</details>
2516</div>
2517<ul>
2518<li>
2519<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>
2520<ul>
2521<li><code>IncomingMessage</code> <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a> Specifies the <code>IncomingMessage</code>
2522class to be used. Useful for extending the original <code>IncomingMessage</code>.
2523<strong>Default:</strong> <code>IncomingMessage</code>.</li>
2524<li><code>ServerResponse</code> <a href="http.html#http_class_http_serverresponse" class="type">&#x3C;http.ServerResponse></a> Specifies the <code>ServerResponse</code> class
2525to be used. Useful for extending the original <code>ServerResponse</code>. <strong>Default:</strong>
2526<code>ServerResponse</code>.</li>
2527<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
2528invalid HTTP headers when <code>true</code>. Using the insecure parser should be
2529avoided. See <a href="cli.html#cli_insecure_http_parser"><code>--insecure-http-parser</code></a> for more information.
2530<strong>Default:</strong> <code>false</code></li>
2531<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
2532<a href="cli.html#cli_max_http_header_size_size"><code>--max-http-header-size</code></a> for requests received by this server, i.e.
2533the maximum length of request headers in bytes.
2534<strong>Default:</strong> 16384 (16KB).</li>
2535</ul>
2536</li>
2537<li>
2538<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>
2539</li>
2540<li>
2541<p>Returns: <a href="http.html#http_class_http_server" class="type">&#x3C;http.Server></a></p>
2542</li>
2543</ul>
2544<p>Returns a new instance of <a href="#http_class_http_server"><code>http.Server</code></a>.</p>
2545<p>The <code>requestListener</code> is a function which is automatically
2546added to the <a href="#http_event_request"><code>'request'</code></a> event.</p>
2547
2548<pre><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><code class="language-js cjs"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
2549
2550<span class="hljs-comment">// Create a local server to receive data from</span>
2551<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> {
2552  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> });
2553  res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">stringify</span>({
2554    <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
2555  }));
2556});
2557
2558server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre><code class="language-js cjs"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
2559
2560<span class="hljs-comment">// Create a local server to receive data from</span>
2561<span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>();
2562
2563<span class="hljs-comment">// Listen to the request event</span>
2564server.<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> {
2565  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> });
2566  res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">stringify</span>({
2567    <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
2568  }));
2569});
2570
2571server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre>
2572</section><section><h3><code>http.get(options[, callback])</code><span><a class="mark" href="#http_http_get_options_callback" id="http_http_get_options_callback">#</a></span></h3>
2573</section><section><h3><code>http.get(url[, options][, callback])</code><span><a class="mark" href="#http_http_get_url_options_callback" id="http_http_get_url_options_callback">#</a></span></h3>
2574<div class="api_metadata">
2575<details class="changelog"><summary>History</summary>
2576<table>
2577<tbody><tr><th>Version</th><th>Changes</th></tr>
2578<tr><td>v10.9.0</td>
2579<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr>
2580<tr><td>v7.5.0</td>
2581<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr>
2582<tr><td>v0.3.6</td>
2583<td><p><span>Added in: v0.3.6</span></p></td></tr>
2584</tbody></table>
2585</details>
2586</div>
2587<ul>
2588<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#url_the_whatwg_url_api" class="type">&#x3C;URL></a></li>
2589<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
2590<a href="#http_http_request_options_callback"><code>http.request()</code></a>, with the <code>method</code> always set to <code>GET</code>.
2591Properties that are inherited from the prototype are ignored.</li>
2592<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>
2593<li>Returns: <a href="http.html#http_class_http_clientrequest" class="type">&#x3C;http.ClientRequest></a></li>
2594</ul>
2595<p>Since most requests are GET requests without bodies, Node.js provides this
2596convenience method. The only difference between this method and
2597<a href="#http_http_request_options_callback"><code>http.request()</code></a> is that it sets the method to GET and calls <code>req.end()</code>
2598automatically. The callback must take care to consume the response
2599data for reasons stated in <a href="#http_class_http_clientrequest"><code>http.ClientRequest</code></a> section.</p>
2600<p>The <code>callback</code> is invoked with a single argument that is an instance of
2601<a href="#http_class_http_incomingmessage"><code>http.IncomingMessage</code></a>.</p>
2602<p>JSON fetching example:</p>
2603<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> {
2604  <span class="hljs-keyword">const</span> { statusCode } = res;
2605  <span class="hljs-keyword">const</span> contentType = res.<span class="hljs-property">headers</span>[<span class="hljs-string">'content-type'</span>];
2606
2607  <span class="hljs-keyword">let</span> error;
2608  <span class="hljs-comment">// Any 2xx status code signals a successful response but</span>
2609  <span class="hljs-comment">// here we're only checking for 200.</span>
2610  <span class="hljs-keyword">if</span> (statusCode !== <span class="hljs-number">200</span>) {
2611    error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Request Failed.\n'</span> +
2612                      <span class="hljs-string">`Status Code: <span class="hljs-subst">${statusCode}</span>`</span>);
2613  } <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)) {
2614    error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Invalid content-type.\n'</span> +
2615                      <span class="hljs-string">`Expected application/json but received <span class="hljs-subst">${contentType}</span>`</span>);
2616  }
2617  <span class="hljs-keyword">if</span> (error) {
2618    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(error.<span class="hljs-property">message</span>);
2619    <span class="hljs-comment">// Consume response data to free up memory</span>
2620    res.<span class="hljs-title function_">resume</span>();
2621    <span class="hljs-keyword">return</span>;
2622  }
2623
2624  res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
2625  <span class="hljs-keyword">let</span> rawData = <span class="hljs-string">''</span>;
2626  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; });
2627  res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
2628    <span class="hljs-keyword">try</span> {
2629      <span class="hljs-keyword">const</span> parsedData = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">parse</span>(rawData);
2630      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(parsedData);
2631    } <span class="hljs-keyword">catch</span> (e) {
2632      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e.<span class="hljs-property">message</span>);
2633    }
2634  });
2635}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
2636  <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>);
2637});
2638
2639<span class="hljs-comment">// Create a local server to receive data from</span>
2640<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> {
2641  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> });
2642  res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">stringify</span>({
2643    <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
2644  }));
2645});
2646
2647server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre>
2648</section><section><h3><code>http.globalAgent</code><span><a class="mark" href="#http_http_globalagent" id="http_http_globalagent">#</a></span></h3>
2649<div class="api_metadata">
2650<span>Added in: v0.5.9</span>
2651</div>
2652<ul>
2653<li><a href="http.html#http_class_http_agent" class="type">&#x3C;http.Agent></a></li>
2654</ul>
2655<p>Global instance of <code>Agent</code> which is used as the default for all HTTP client
2656requests.</p>
2657</section><section><h3><code>http.maxHeaderSize</code><span><a class="mark" href="#http_http_maxheadersize" id="http_http_maxheadersize">#</a></span></h3>
2658<div class="api_metadata">
2659<span>Added in: v11.6.0, v10.15.0</span>
2660</div>
2661<ul>
2662<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2663</ul>
2664<p>Read-only property specifying the maximum allowed size of HTTP headers in bytes.
2665Defaults to 8KB. Configurable using the <a href="cli.html#cli_max_http_header_size_size"><code>--max-http-header-size</code></a> CLI option.</p>
2666<p>This can be overridden for servers and client requests by passing the
2667<code>maxHeaderSize</code> option.</p>
2668</section><section><h3><code>http.request(options[, callback])</code><span><a class="mark" href="#http_http_request_options_callback" id="http_http_request_options_callback">#</a></span></h3>
2669</section><section><h3><code>http.request(url[, options][, callback])</code><span><a class="mark" href="#http_http_request_url_options_callback" id="http_http_request_url_options_callback">#</a></span></h3>
2670<div class="api_metadata">
2671<details class="changelog"><summary>History</summary>
2672<table>
2673<tbody><tr><th>Version</th><th>Changes</th></tr>
2674<tr><td>v14.18.0</td>
2675<td><p>When using a <code>URL</code> object parsed username and password will now be properly URI decoded.</p></td></tr>
2676<tr><td>v14.17.0</td>
2677<td><p>It is possible to abort a request with an AbortSignal.</p></td></tr>
2678<tr><td>v13.3.0</td>
2679<td><p>The <code>maxHeaderSize</code> option is supported now.</p></td></tr>
2680<tr><td>v13.8.0, v12.15.0, v10.19.0</td>
2681<td><p>The <code>insecureHTTPParser</code> option is supported now.</p></td></tr>
2682<tr><td>v10.9.0</td>
2683<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr>
2684<tr><td>v7.5.0</td>
2685<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr>
2686<tr><td>v0.3.6</td>
2687<td><p><span>Added in: v0.3.6</span></p></td></tr>
2688</tbody></table>
2689</details>
2690</div>
2691<ul>
2692<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#url_the_whatwg_url_api" class="type">&#x3C;URL></a></li>
2693<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2694<ul>
2695<li><code>agent</code> <a href="http.html#http_class_http_agent" 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="#http_class_http_agent"><code>Agent</code></a> behavior. Possible
2696values:
2697<ul>
2698<li><code>undefined</code> (default): use <a href="#http_http_globalagent"><code>http.globalAgent</code></a> for this host and port.</li>
2699<li><code>Agent</code> object: explicitly use the passed in <code>Agent</code>.</li>
2700<li><code>false</code>: causes a new <code>Agent</code> with default values to be used.</li>
2701</ul>
2702</li>
2703<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 i.e. <code>'user:password'</code> to compute an
2704Authorization header.</li>
2705<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
2706use for the request when the <code>agent</code> option is not used. This can be used to
2707avoid creating a custom <code>Agent</code> class just to override the default
2708<code>createConnection</code> function. See <a href="#http_agent_createconnection_options_callback"><code>agent.createConnection()</code></a> for more
2709details. Any <a href="stream.html#stream_class_stream_duplex"><code>Duplex</code></a> stream is a valid return value.</li>
2710<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>
2711<code>agent.defaultPort</code> if an <code>Agent</code> is used, else <code>undefined</code>.</li>
2712<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
2713<code>hostname</code>. Valid values are <code>4</code> or <code>6</code>. When unspecified, both IP v4 and
2714v6 will be used.</li>
2715<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>
2716<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#dns_supported_getaddrinfo_flags"><code>dns.lookup()</code> hints</a>.</li>
2717<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
2718request to. <strong>Default:</strong> <code>'localhost'</code>.</li>
2719<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#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a>,
2720<code>hostname</code> will be used if both <code>host</code> and <code>hostname</code> are specified.</li>
2721<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
2722invalid HTTP headers when <code>true</code>. Using the insecure parser should be
2723avoided. See <a href="cli.html#cli_insecure_http_parser"><code>--insecure-http-parser</code></a> for more information.
2724<strong>Default:</strong> <code>false</code></li>
2725<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>
2726<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>
2727<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#dns_dns_lookup_hostname_options_callback"><code>dns.lookup()</code></a>.</li>
2728<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
2729<a href="cli.html#cli_max_http_header_size_size"><code>--max-http-header-size</code></a> for requests received from the server, i.e.
2730the maximum length of response headers in bytes.
2731<strong>Default:</strong> 16384 (16KB).</li>
2732<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>
2733<code>'GET'</code>.</li>
2734<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.
2735E.G. <code>'/index.html?page=12'</code>. An exception is thrown when the request path
2736contains illegal characters. Currently, only spaces are rejected but that
2737may change in the future. <strong>Default:</strong> <code>'/'</code>.</li>
2738<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,
2739else <code>80</code>.</li>
2740<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>
2741<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
2742<code>Host</code> header. Defaults to <code>true</code>.</li>
2743<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>
2744or <code>port</code> is specified, those specify a TCP Socket).</li>
2745<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.
2746This will set the timeout before the socket is connected.</li>
2747<li><code>signal</code> <a href="globals.html#globals_class_abortsignal" class="type">&#x3C;AbortSignal></a>: An AbortSignal that may be used to abort an ongoing
2748request.</li>
2749</ul>
2750</li>
2751<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>
2752<li>Returns: <a href="http.html#http_class_http_clientrequest" class="type">&#x3C;http.ClientRequest></a></li>
2753</ul>
2754<p>Node.js maintains several connections per server to make HTTP requests.
2755This function allows one to transparently issue requests.</p>
2756<p><code>url</code> can be a string or a <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> object. If <code>url</code> is a
2757string, it is automatically parsed with <a href="url.html#url_new_url_input_base"><code>new URL()</code></a>. If it is a <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a>
2758object, it will be automatically converted to an ordinary <code>options</code> object.</p>
2759<p>If both <code>url</code> and <code>options</code> are specified, the objects are merged, with the
2760<code>options</code> properties taking precedence.</p>
2761<p>The optional <code>callback</code> parameter will be added as a one-time listener for
2762the <a href="#http_event_response"><code>'response'</code></a> event.</p>
2763<p><code>http.request()</code> returns an instance of the <a href="#http_class_http_clientrequest"><code>http.ClientRequest</code></a>
2764class. The <code>ClientRequest</code> instance is a writable stream. If one needs to
2765upload a file with a POST request, then write to the <code>ClientRequest</code> object.</p>
2766<pre><code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
2767
2768<span class="hljs-keyword">const</span> postData = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">stringify</span>({
2769  <span class="hljs-string">'msg'</span>: <span class="hljs-string">'Hello World!'</span>
2770});
2771
2772<span class="hljs-keyword">const</span> options = {
2773  <span class="hljs-attr">hostname</span>: <span class="hljs-string">'www.google.com'</span>,
2774  <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
2775  <span class="hljs-attr">path</span>: <span class="hljs-string">'/upload'</span>,
2776  <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>,
2777  <span class="hljs-attr">headers</span>: {
2778    <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>,
2779    <span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(postData)
2780  }
2781};
2782
2783<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> {
2784  <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>);
2785  <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>);
2786  res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
2787  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> {
2788    <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>);
2789  });
2790  res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
2791    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'No more data in response.'</span>);
2792  });
2793});
2794
2795req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
2796  <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>);
2797});
2798
2799<span class="hljs-comment">// Write data to request body</span>
2800req.<span class="hljs-title function_">write</span>(postData);
2801req.<span class="hljs-title function_">end</span>();</code></pre>
2802<p>In the example <code>req.end()</code> was called. With <code>http.request()</code> one
2803must always call <code>req.end()</code> to signify the end of the request -
2804even if there is no data being written to the request body.</p>
2805<p>If any error is encountered during the request (be that with DNS resolution,
2806TCP level errors, or actual HTTP parse errors) an <code>'error'</code> event is emitted
2807on the returned request object. As with all <code>'error'</code> events, if no listeners
2808are registered the error will be thrown.</p>
2809<p>There are a few special headers that should be noted.</p>
2810<ul>
2811<li>
2812<p>Sending a 'Connection: keep-alive' will notify Node.js that the connection to
2813the server should be persisted until the next request.</p>
2814</li>
2815<li>
2816<p>Sending a 'Content-Length' header will disable the default chunked encoding.</p>
2817</li>
2818<li>
2819<p>Sending an 'Expect' header will immediately send the request headers.
2820Usually, when sending 'Expect: 100-continue', both a timeout and a listener
2821for the <code>'continue'</code> event should be set. See RFC 2616 Section 8.2.3 for more
2822information.</p>
2823</li>
2824<li>
2825<p>Sending an Authorization header will override using the <code>auth</code> option
2826to compute basic authentication.</p>
2827</li>
2828</ul>
2829<p>Example using a <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> as <code>options</code>:</p>
2830<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>);
2831
2832<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> {
2833  <span class="hljs-comment">// ...</span>
2834});</code></pre>
2835<p>In a successful request, the following events will be emitted in the following
2836order:</p>
2837<ul>
2838<li><code>'socket'</code></li>
2839<li><code>'response'</code>
2840<ul>
2841<li><code>'data'</code> any number of times, on the <code>res</code> object
2842(<code>'data'</code> will not be emitted at all if the response body is empty, for
2843instance, in most redirects)</li>
2844<li><code>'end'</code> on the <code>res</code> object</li>
2845</ul>
2846</li>
2847<li><code>'close'</code></li>
2848</ul>
2849<p>In the case of a connection error, the following events will be emitted:</p>
2850<ul>
2851<li><code>'socket'</code></li>
2852<li><code>'error'</code></li>
2853<li><code>'close'</code></li>
2854</ul>
2855<p>In the case of a premature connection close before the response is received,
2856the following events will be emitted in the following order:</p>
2857<ul>
2858<li><code>'socket'</code></li>
2859<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code
2860<code>'ECONNRESET'</code></li>
2861<li><code>'close'</code></li>
2862</ul>
2863<p>In the case of a premature connection close after the response is received,
2864the following events will be emitted in the following order:</p>
2865<ul>
2866<li><code>'socket'</code></li>
2867<li><code>'response'</code>
2868<ul>
2869<li><code>'data'</code> any number of times, on the <code>res</code> object</li>
2870</ul>
2871</li>
2872<li>(connection closed here)</li>
2873<li><code>'aborted'</code> on the <code>res</code> object</li>
2874<li><code>'close'</code></li>
2875<li><code>'close'</code> on the <code>res</code> object</li>
2876</ul>
2877<p>If <code>req.destroy()</code> is called before a socket is assigned, the following
2878events will be emitted in the following order:</p>
2879<ul>
2880<li>(<code>req.destroy()</code> called here)</li>
2881<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code
2882<code>'ECONNRESET'</code></li>
2883<li><code>'close'</code></li>
2884</ul>
2885<p>If <code>req.destroy()</code> is called before the connection succeeds, the following
2886events will be emitted in the following order:</p>
2887<ul>
2888<li><code>'socket'</code></li>
2889<li>(<code>req.destroy()</code> called here)</li>
2890<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code
2891<code>'ECONNRESET'</code></li>
2892<li><code>'close'</code></li>
2893</ul>
2894<p>If <code>req.destroy()</code> is called after the response is received, the following
2895events will be emitted in the following order:</p>
2896<ul>
2897<li><code>'socket'</code></li>
2898<li><code>'response'</code>
2899<ul>
2900<li><code>'data'</code> any number of times, on the <code>res</code> object</li>
2901</ul>
2902</li>
2903<li>(<code>req.destroy()</code> called here)</li>
2904<li><code>'aborted'</code> on the <code>res</code> object</li>
2905<li><code>'close'</code></li>
2906<li><code>'close'</code> on the <code>res</code> object</li>
2907</ul>
2908<p>If <code>req.abort()</code> is called before a socket is assigned, the following
2909events will be emitted in the following order:</p>
2910<ul>
2911<li>(<code>req.abort()</code> called here)</li>
2912<li><code>'abort'</code></li>
2913<li><code>'close'</code></li>
2914</ul>
2915<p>If <code>req.abort()</code> is called before the connection succeeds, the following
2916events will be emitted in the following order:</p>
2917<ul>
2918<li><code>'socket'</code></li>
2919<li>(<code>req.abort()</code> called here)</li>
2920<li><code>'abort'</code></li>
2921<li><code>'error'</code> with an error with message <code>'Error: socket hang up'</code> and code
2922<code>'ECONNRESET'</code></li>
2923<li><code>'close'</code></li>
2924</ul>
2925<p>If <code>req.abort()</code> is called after the response is received, the following
2926events will be emitted in the following order:</p>
2927<ul>
2928<li><code>'socket'</code></li>
2929<li><code>'response'</code>
2930<ul>
2931<li><code>'data'</code> any number of times, on the <code>res</code> object</li>
2932</ul>
2933</li>
2934<li>(<code>req.abort()</code> called here)</li>
2935<li><code>'abort'</code></li>
2936<li><code>'aborted'</code> on the <code>res</code> object</li>
2937<li><code>'close'</code></li>
2938<li><code>'close'</code> on the <code>res</code> object</li>
2939</ul>
2940<p>Setting the <code>timeout</code> option or using the <code>setTimeout()</code> function will
2941not abort the request or do anything besides add a <code>'timeout'</code> event.</p>
2942<p>Passing an <code>AbortSignal</code> and then calling <code>abort</code> on the corresponding
2943<code>AbortController</code> will behave the same way as calling <code>.destroy()</code> on the
2944request itself.</p>
2945</section><section><h3><code>http.validateHeaderName(name)</code><span><a class="mark" href="#http_http_validateheadername_name" id="http_http_validateheadername_name">#</a></span></h3>
2946<div class="api_metadata">
2947<span>Added in: v14.3.0</span>
2948</div>
2949<ul>
2950<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>
2951</ul>
2952<p>Performs the low-level validations on the provided <code>name</code> that are done when
2953<code>res.setHeader(name, value)</code> is called.</p>
2954<p>Passing illegal value as <code>name</code> will result in a <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> being thrown,
2955identified by <code>code: 'ERR_INVALID_HTTP_TOKEN'</code>.</p>
2956<p>It is not necessary to use this method before passing headers to an HTTP request
2957or response. The HTTP module will automatically validate such headers.
2958Examples:</p>
2959<p>Example:</p>
2960<pre><code class="language-js"><span class="hljs-keyword">const</span> { validateHeaderName } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
2961
2962<span class="hljs-keyword">try</span> {
2963  <span class="hljs-title function_">validateHeaderName</span>(<span class="hljs-string">''</span>);
2964} <span class="hljs-keyword">catch</span> (err) {
2965  err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>; <span class="hljs-comment">// --> true</span>
2966  err.<span class="hljs-property">code</span>; <span class="hljs-comment">// --> 'ERR_INVALID_HTTP_TOKEN'</span>
2967  err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Header name must be a valid HTTP token [""]'</span>
2968}</code></pre>
2969</section><section><h3><code>http.validateHeaderValue(name, value)</code><span><a class="mark" href="#http_http_validateheadervalue_name_value" id="http_http_validateheadervalue_name_value">#</a></span></h3>
2970<div class="api_metadata">
2971<span>Added in: v14.3.0</span>
2972</div>
2973<ul>
2974<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>
2975<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>
2976</ul>
2977<p>Performs the low-level validations on the provided <code>value</code> that are done when
2978<code>res.setHeader(name, value)</code> is called.</p>
2979<p>Passing illegal value as <code>value</code> will result in a <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> being thrown.</p>
2980<ul>
2981<li>Undefined value error is identified by <code>code: 'ERR_HTTP_INVALID_HEADER_VALUE'</code>.</li>
2982<li>Invalid value character error is identified by <code>code: 'ERR_INVALID_CHAR'</code>.</li>
2983</ul>
2984<p>It is not necessary to use this method before passing headers to an HTTP request
2985or response. The HTTP module will automatically validate such headers.</p>
2986<p>Examples:</p>
2987<pre><code class="language-js"><span class="hljs-keyword">const</span> { validateHeaderValue } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);
2988
2989<span class="hljs-keyword">try</span> {
2990  <span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-literal">undefined</span>);
2991} <span class="hljs-keyword">catch</span> (err) {
2992  err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>; <span class="hljs-comment">// --> true</span>
2993  err.<span class="hljs-property">code</span> === <span class="hljs-string">'ERR_HTTP_INVALID_HEADER_VALUE'</span>; <span class="hljs-comment">// --> true</span>
2994  err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Invalid value "undefined" for header "x-my-header"'</span>
2995}
2996
2997<span class="hljs-keyword">try</span> {
2998  <span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-header'</span>, <span class="hljs-string">'oʊmɪɡə'</span>);
2999} <span class="hljs-keyword">catch</span> (err) {
3000  err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>; <span class="hljs-comment">// --> true</span>
3001  err.<span class="hljs-property">code</span> === <span class="hljs-string">'ERR_INVALID_CHAR'</span>; <span class="hljs-comment">// --> true</span>
3002  err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Invalid character in header content ["x-my-header"]'</span>
3003}</code></pre></section>
3004        <!-- API END -->
3005      </div>
3006    </div>
3007  </div>
3008  <script>
3009    'use strict';
3010    {
3011      const kCustomPreference = 'customDarkTheme';
3012      const userSettings = sessionStorage.getItem(kCustomPreference);
3013      const themeToggleButton = document.getElementById('theme-toggle-btn');
3014      if (userSettings === null && window.matchMedia) {
3015        const mq = window.matchMedia('(prefers-color-scheme: dark)');
3016        if ('onchange' in mq) {
3017          function mqChangeListener(e) {
3018            document.body.classList.toggle('dark-mode', e.matches);
3019          }
3020          mq.addEventListener('change', mqChangeListener);
3021          if (themeToggleButton) {
3022            themeToggleButton.addEventListener('click', function() {
3023              mq.removeEventListener('change', mqChangeListener);
3024            }, { once: true });
3025          }
3026        }
3027        if (mq.matches) {
3028          document.body.classList.add('dark-mode');
3029        }
3030      } else if (userSettings === 'true') {
3031        document.body.classList.add('dark-mode');
3032      }
3033      if (themeToggleButton) {
3034        themeToggleButton.hidden = false;
3035        themeToggleButton.addEventListener('click', function() {
3036          sessionStorage.setItem(
3037            kCustomPreference,
3038            document.body.classList.toggle('dark-mode')
3039          );
3040        });
3041      }
3042    }
3043  </script>
3044</body>
3045</html>
3046