-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.html
More file actions
2047 lines (2016 loc) · 132 KB
/
http.html
File metadata and controls
2047 lines (2016 loc) · 132 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>HTTP | Node.js v12.0.0 Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/http.html">
</head>
<body class="alt apidoc" id="api-section-http">
<div id="content" class="clearfix">
<div id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a href="documentation.html" class="nav-documentation">About these Docs</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage & Example</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="assert.html" class="nav-assert">Assertion Testing</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async Hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ Addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ Addons - N-API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child Processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command Line Options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="esm.html" class="nav-esm">ECMAScript Modules</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File System</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http active">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance Hooks</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query Strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String Decoder</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace Events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/Datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker Threads</a></li>
<li><a href="zlib.html" class="nav-zlib">ZLIB</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">GitHub Repo & Issue Tracker</a></li>
</ul>
</div>
<div id="column1" data-id="http" class="interior">
<header>
<h1>Node.js v12.0.0 Documentation</h1>
<div id="gtoc">
<ul>
<li>
<a href="index.html" name="toc">Index</a>
</li>
<li>
<a href="all.html">View on single page</a>
</li>
<li>
<a href="http.json">View as JSON</a>
</li>
<li class="version-picker">
<a href="#">View another version <span>▼</span></a>
<ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v11.x/api/http.html">11.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v10.x/api/http.html">10.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v9.x/api/http.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/http.html">8.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v7.x/api/http.html">7.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v6.x/api/http.html">6.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v5.x/api/http.html">5.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v4.x/api/http.html">4.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/http.html">0.12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/http.html">0.10.x</a></li></ol>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/http.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"/></svg></span>Edit on GitHub</a></li>
</ul>
</div>
<hr>
</header>
<div id="toc">
<h2>Table of Contents</h2>
<ul>
<li>
<p><span class="stability_2"><a href="#http_http">HTTP</a></span></p>
<ul>
<li>
<p><a href="#http_class_http_agent">Class: http.Agent</a></p>
<ul>
<li><a href="#http_new_agent_options">new Agent([options])</a></li>
<li><a href="#http_agent_createconnection_options_callback">agent.createConnection(options[, callback])</a></li>
<li><a href="#http_agent_keepsocketalive_socket">agent.keepSocketAlive(socket)</a></li>
<li><a href="#http_agent_reusesocket_socket_request">agent.reuseSocket(socket, request)</a></li>
<li><a href="#http_agent_destroy">agent.destroy()</a></li>
<li><a href="#http_agent_freesockets">agent.freeSockets</a></li>
<li><a href="#http_agent_getname_options">agent.getName(options)</a></li>
<li><a href="#http_agent_maxfreesockets">agent.maxFreeSockets</a></li>
<li><a href="#http_agent_maxsockets">agent.maxSockets</a></li>
<li><a href="#http_agent_requests">agent.requests</a></li>
<li><a href="#http_agent_sockets">agent.sockets</a></li>
</ul>
</li>
<li>
<p><a href="#http_class_http_clientrequest">Class: http.ClientRequest</a></p>
<ul>
<li><a href="#http_event_abort">Event: 'abort'</a></li>
<li><a href="#http_event_connect">Event: 'connect'</a></li>
<li><a href="#http_event_continue">Event: 'continue'</a></li>
<li><a href="#http_event_information">Event: 'information'</a></li>
<li><a href="#http_event_response">Event: 'response'</a></li>
<li><a href="#http_event_socket">Event: 'socket'</a></li>
<li><a href="#http_event_timeout">Event: 'timeout'</a></li>
<li><a href="#http_event_upgrade">Event: 'upgrade'</a></li>
<li><a href="#http_request_abort">request.abort()</a></li>
<li><a href="#http_request_aborted">request.aborted</a></li>
<li><a href="#http_request_connection">request.connection</a></li>
<li><a href="#http_request_end_data_encoding_callback">request.end([data[, encoding]][, callback])</a></li>
<li><a href="#http_request_finished">request.finished</a></li>
<li><a href="#http_request_flushheaders">request.flushHeaders()</a></li>
<li><a href="#http_request_getheader_name">request.getHeader(name)</a></li>
<li><a href="#http_request_maxheaderscount">request.maxHeadersCount</a></li>
<li><a href="#http_request_removeheader_name">request.removeHeader(name)</a></li>
<li><a href="#http_request_setheader_name_value">request.setHeader(name, value)</a></li>
<li><a href="#http_request_setnodelay_nodelay">request.setNoDelay([noDelay])</a></li>
<li><a href="#http_request_setsocketkeepalive_enable_initialdelay">request.setSocketKeepAlive([enable][, initialDelay])</a></li>
<li><a href="#http_request_settimeout_timeout_callback">request.setTimeout(timeout[, callback])</a></li>
<li><a href="#http_request_socket">request.socket</a></li>
<li><a href="#http_request_write_chunk_encoding_callback">request.write(chunk[, encoding][, callback])</a></li>
</ul>
</li>
<li>
<p><a href="#http_class_http_server">Class: http.Server</a></p>
<ul>
<li><a href="#http_event_checkcontinue">Event: 'checkContinue'</a></li>
<li><a href="#http_event_checkexpectation">Event: 'checkExpectation'</a></li>
<li><a href="#http_event_clienterror">Event: 'clientError'</a></li>
<li><a href="#http_event_close">Event: 'close'</a></li>
<li><a href="#http_event_connect_1">Event: 'connect'</a></li>
<li><a href="#http_event_connection">Event: 'connection'</a></li>
<li><a href="#http_event_request">Event: 'request'</a></li>
<li><a href="#http_event_upgrade_1">Event: 'upgrade'</a></li>
<li><a href="#http_server_close_callback">server.close([callback])</a></li>
<li><a href="#http_server_listen">server.listen()</a></li>
<li><a href="#http_server_listening">server.listening</a></li>
<li><a href="#http_server_maxheaderscount">server.maxHeadersCount</a></li>
<li><a href="#http_server_settimeout_msecs_callback">server.setTimeout([msecs][, callback])</a></li>
<li><a href="#http_server_timeout">server.timeout</a></li>
<li><a href="#http_server_keepalivetimeout">server.keepAliveTimeout</a></li>
</ul>
</li>
<li>
<p><a href="#http_class_http_serverresponse">Class: http.ServerResponse</a></p>
<ul>
<li><a href="#http_event_close_1">Event: 'close'</a></li>
<li><a href="#http_event_finish">Event: 'finish'</a></li>
<li><a href="#http_response_addtrailers_headers">response.addTrailers(headers)</a></li>
<li><a href="#http_response_connection">response.connection</a></li>
<li><a href="#http_response_end_data_encoding_callback">response.end([data][, encoding][, callback])</a></li>
<li><a href="#http_response_finished">response.finished</a></li>
<li><a href="#http_response_getheader_name">response.getHeader(name)</a></li>
<li><a href="#http_response_getheadernames">response.getHeaderNames()</a></li>
<li><a href="#http_response_getheaders">response.getHeaders()</a></li>
<li><a href="#http_response_hasheader_name">response.hasHeader(name)</a></li>
<li><a href="#http_response_headerssent">response.headersSent</a></li>
<li><a href="#http_response_removeheader_name">response.removeHeader(name)</a></li>
<li><a href="#http_response_senddate">response.sendDate</a></li>
<li><a href="#http_response_setheader_name_value">response.setHeader(name, value)</a></li>
<li><a href="#http_response_settimeout_msecs_callback">response.setTimeout(msecs[, callback])</a></li>
<li><a href="#http_response_socket">response.socket</a></li>
<li><a href="#http_response_statuscode">response.statusCode</a></li>
<li><a href="#http_response_statusmessage">response.statusMessage</a></li>
<li><a href="#http_response_write_chunk_encoding_callback">response.write(chunk[, encoding][, callback])</a></li>
<li><a href="#http_response_writecontinue">response.writeContinue()</a></li>
<li><a href="#http_response_writehead_statuscode_statusmessage_headers">response.writeHead(statusCode[, statusMessage][, headers])</a></li>
<li><a href="#http_response_writeprocessing">response.writeProcessing()</a></li>
</ul>
</li>
<li>
<p><a href="#http_class_http_incomingmessage">Class: http.IncomingMessage</a></p>
<ul>
<li><a href="#http_event_aborted">Event: 'aborted'</a></li>
<li><a href="#http_event_close_2">Event: 'close'</a></li>
<li><a href="#http_message_aborted">message.aborted</a></li>
<li><a href="#http_message_complete">message.complete</a></li>
<li><a href="#http_message_destroy_error">message.destroy([error])</a></li>
<li><a href="#http_message_headers">message.headers</a></li>
<li><a href="#http_message_httpversion">message.httpVersion</a></li>
<li><a href="#http_message_method">message.method</a></li>
<li><a href="#http_message_rawheaders">message.rawHeaders</a></li>
<li><a href="#http_message_rawtrailers">message.rawTrailers</a></li>
<li><a href="#http_message_settimeout_msecs_callback">message.setTimeout(msecs, callback)</a></li>
<li><a href="#http_message_socket">message.socket</a></li>
<li><a href="#http_message_statuscode">message.statusCode</a></li>
<li><a href="#http_message_statusmessage">message.statusMessage</a></li>
<li><a href="#http_message_trailers">message.trailers</a></li>
<li><a href="#http_message_url">message.url</a></li>
</ul>
</li>
<li><a href="#http_http_methods">http.METHODS</a></li>
<li><a href="#http_http_status_codes">http.STATUS_CODES</a></li>
<li><a href="#http_http_createserver_options_requestlistener">http.createServer([options][, requestListener])</a></li>
<li><a href="#http_http_get_options_callback">http.get(options[, callback])</a></li>
<li><a href="#http_http_get_url_options_callback">http.get(url[, options][, callback])</a></li>
<li><a href="#http_http_globalagent">http.globalAgent</a></li>
<li><a href="#http_http_request_options_callback">http.request(options[, callback])</a></li>
<li><a href="#http_http_request_url_options_callback">http.request(url[, options][, callback])</a></li>
</ul>
</li>
</ul>
</div>
<div id="apicontent">
<h1>HTTP<span><a class="mark" href="#http_http" id="http_http">#</a></span></h1>
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
<p>To use the HTTP server and client one must <code>require('http')</code>.</p>
<p>The HTTP interfaces in Node.js are designed to support many features
of the protocol which have been traditionally difficult to use.
In particular, large, possibly chunk-encoded, messages. The interface is
careful to never buffer entire requests or responses — the
user is able to stream data.</p>
<p>HTTP message headers are represented by an object like this:</p>
<!-- eslint-skip -->
<pre><code class="language-js">{ <span class="hljs-string">'content-length'</span>: <span class="hljs-string">'123'</span>,
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain'</span>,
<span class="hljs-string">'connection'</span>: <span class="hljs-string">'keep-alive'</span>,
<span class="hljs-string">'host'</span>: <span class="hljs-string">'mysite.com'</span>,
<span class="hljs-string">'accept'</span>: <span class="hljs-string">'*/*'</span> }</code></pre>
<p>Keys are lowercased. Values are not modified.</p>
<p>In order to support the full spectrum of possible HTTP applications, Node.js's
HTTP API is very low-level. It deals with stream handling and message
parsing only. It parses a message into headers and body but it does not
parse the actual headers or the body.</p>
<p>See <a href="#http_message_headers"><code>message.headers</code></a> for details on how duplicate headers are handled.</p>
<p>The raw headers as they were received are retained in the <code>rawHeaders</code>
property, which is an array of <code>[key, value, key2, value2, ...]</code>. For
example, the previous message header object might have a <code>rawHeaders</code>
list like the following:</p>
<!-- eslint-disable semi -->
<pre><code class="language-js">[ <span class="hljs-string">'ConTent-Length'</span>, <span class="hljs-string">'123456'</span>,
<span class="hljs-string">'content-LENGTH'</span>, <span class="hljs-string">'123'</span>,
<span class="hljs-string">'content-type'</span>, <span class="hljs-string">'text/plain'</span>,
<span class="hljs-string">'CONNECTION'</span>, <span class="hljs-string">'keep-alive'</span>,
<span class="hljs-string">'Host'</span>, <span class="hljs-string">'mysite.com'</span>,
<span class="hljs-string">'accepT'</span>, <span class="hljs-string">'*/*'</span> ]</code></pre>
<h2>Class: http.Agent<span><a class="mark" href="#http_class_http_agent" id="http_class_http_agent">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.3.4</span>
</div>
<p>An <code>Agent</code> is responsible for managing connection persistence
and reuse for HTTP clients. It maintains a queue of pending requests
for a given host and port, reusing a single socket connection for each
until the queue is empty, at which time the socket is either destroyed
or put into a pool where it is kept to be used again for requests to the
same host and port. Whether it is destroyed or pooled depends on the
<code>keepAlive</code> <a href="#http_new_agent_options">option</a>.</p>
<p>Pooled connections have TCP Keep-Alive enabled for them, but servers may
still close idle connections, in which case they will be removed from the
pool and a new connection will be made when a new HTTP request is made for
that host and port. Servers may also refuse to allow multiple requests
over the same connection, in which case the connection will have to be
remade for every request and cannot be pooled. The <code>Agent</code> will still make
the requests to that server, but each one will occur over a new connection.</p>
<p>When a connection is closed by the client or the server, it is removed
from the pool. Any unused sockets in the pool will be unrefed so as not
to keep the Node.js process running when there are no outstanding requests.
(see <a href="net.html#net_socket_unref"><code>socket.unref()</code></a>).</p>
<p>It is good practice, to <a href="#http_agent_destroy"><code>destroy()</code></a> an <code>Agent</code> instance when it is no
longer in use, because unused sockets consume OS resources.</p>
<p>Sockets are removed from an agent when the socket emits either
a <code>'close'</code> event or an <code>'agentRemove'</code> event. When intending to keep one
HTTP request open for a long time without keeping it in the agent, something
like the following may be done:</p>
<pre><code class="language-js">http.get(options, (res) => {
<span class="hljs-comment">// Do stuff</span>
}).on(<span class="hljs-string">'socket'</span>, (socket) => {
socket.emit(<span class="hljs-string">'agentRemove'</span>);
});</code></pre>
<p>An agent may also be used for an individual request. By providing
<code>{agent: false}</code> as an option to the <code>http.get()</code> or <code>http.request()</code>
functions, a one-time use <code>Agent</code> with default options will be used
for the client connection.</p>
<p><code>agent:false</code>:</p>
<pre><code class="language-js">http.get({
<span class="hljs-attr">hostname</span>: <span class="hljs-string">'localhost'</span>,
<span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
<span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
<span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span> <span class="hljs-comment">// create a new agent just for this one request</span>
}, (res) => {
<span class="hljs-comment">// Do stuff with response</span>
});</code></pre>
<h3>new Agent([options])<span><a class="mark" href="#http_new_agent_options" id="http_new_agent_options">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.3.4</span>
</div>
<ul>
<li>
<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Set of configurable options to set on the agent.
Can have the following fields:</p>
<ul>
<li><code>keepAlive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Keep sockets around even when there are no
outstanding requests, so they can be used for future requests without
having to reestablish a TCP connection. <strong>Default:</strong> <code>false</code>.</li>
<li><code>keepAliveMsecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> When using the <code>keepAlive</code> option, specifies
the <a href="net.html#net_socket_setkeepalive_enable_initialdelay">initial delay</a>
for TCP Keep-Alive packets. Ignored when the
<code>keepAlive</code> option is <code>false</code> or <code>undefined</code>. <strong>Default:</strong> <code>1000</code>.</li>
<li><code>maxSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Maximum number of sockets to allow per
host. <strong>Default:</strong> <code>Infinity</code>.</li>
<li><code>maxFreeSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Maximum number of sockets to leave open
in a free state. Only relevant if <code>keepAlive</code> is set to <code>true</code>.
<strong>Default:</strong> <code>256</code>.</li>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Socket timeout in milliseconds.
This will set the timeout after the socket is connected.</li>
</ul>
</li>
</ul>
<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
of these values set to their respective defaults.</p>
<p>To configure any of them, a custom <a href="#http_class_http_agent"><code>http.Agent</code></a> instance must be created.</p>
<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>);
<span class="hljs-keyword">const</span> keepAliveAgent = <span class="hljs-keyword">new</span> http.Agent({ <span class="hljs-attr">keepAlive</span>: <span class="hljs-literal">true</span> });
options.agent = keepAliveAgent;
http.request(options, onResponseCallback);</code></pre>
<h3>agent.createConnection(options[, callback])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/https.js#L89">[src]</a><span><a class="mark" href="#http_agent_createconnection_options_callback" id="http_agent_createconnection_options_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.11.4</span>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Options containing connection details. Check
<a href="net.html#net_net_createconnection_options_connectlistener"><code>net.createConnection()</code></a> for the format of the options</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Callback function that receives the created socket</li>
<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
</ul>
<p>Produces a socket/stream to be used for HTTP requests.</p>
<p>By default, this function is the same as <a href="net.html#net_net_createconnection_options_connectlistener"><code>net.createConnection()</code></a>. However,
custom agents may override this method in case greater flexibility is desired.</p>
<p>A socket/stream can be supplied in one of two ways: by returning the
socket/stream from this function, or by passing the socket/stream to <code>callback</code>.</p>
<p><code>callback</code> has a signature of <code>(err, stream)</code>.</p>
<h3>agent.keepSocketAlive(socket)<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/_http_agent.js#L316">[src]</a><span><a class="mark" href="#http_agent_keepsocketalive_socket" id="http_agent_keepsocketalive_socket">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.1.0</span>
</div>
<ul>
<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
</ul>
<p>Called when <code>socket</code> is detached from a request and could be persisted by the
<code>Agent</code>. Default behavior is to:</p>
<pre><code class="language-js">socket.setKeepAlive(<span class="hljs-literal">true</span>, <span class="hljs-keyword">this</span>.keepAliveMsecs);
socket.unref();
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;</code></pre>
<p>This method can be overridden by a particular <code>Agent</code> subclass. If this
method returns a falsy value, the socket will be destroyed instead of persisting
it for use with the next request.</p>
<h3>agent.reuseSocket(socket, request)<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/_http_agent.js#L323">[src]</a><span><a class="mark" href="#http_agent_reusesocket_socket_request" id="http_agent_reusesocket_socket_request">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.1.0</span>
</div>
<ul>
<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
<li><code>request</code> <a href="http.html#http_class_http_clientrequest" class="type"><http.ClientRequest></a></li>
</ul>
<p>Called when <code>socket</code> is attached to <code>request</code> after being persisted because of
the keep-alive options. Default behavior is to:</p>
<pre><code class="language-js">socket.ref();</code></pre>
<p>This method can be overridden by a particular <code>Agent</code> subclass.</p>
<h3>agent.destroy()<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/_http_agent.js#L328">[src]</a><span><a class="mark" href="#http_agent_destroy" id="http_agent_destroy">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.11.4</span>
</div>
<p>Destroy any sockets that are currently in use by the agent.</p>
<p>It is usually not necessary to do this. However, if using an
agent with <code>keepAlive</code> enabled, then it is best to explicitly shut down
the agent when it will no longer be used. Otherwise,
sockets may hang open for quite a long time before the server
terminates them.</p>
<h3>agent.freeSockets<span><a class="mark" href="#http_agent_freesockets" id="http_agent_freesockets">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.11.4</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
</ul>
<p>An object which contains arrays of sockets currently awaiting use by
the agent when <code>keepAlive</code> is enabled. Do not modify.</p>
<h3>agent.getName(options)<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/https.js#L154">[src]</a><span><a class="mark" href="#http_agent_getname_options" id="http_agent_getname_options">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.11.4</span>
</div>
<ul>
<li>
<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> A set of options providing information for name generation</p>
<ul>
<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A domain name or IP address of the server to issue the
request to</li>
<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Port of remote server</li>
<li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Local interface to bind for network connections
when issuing the request</li>
<li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Must be 4 or 6 if this doesn't equal <code>undefined</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Get a unique name for a set of request options, to determine whether a
connection can be reused. For an HTTP agent, this returns
<code>host:port:localAddress</code> or <code>host:port:localAddress:family</code>. For an HTTPS agent,
the name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options
that determine socket reusability.</p>
<h3>agent.maxFreeSockets<span><a class="mark" href="#http_agent_maxfreesockets" id="http_agent_maxfreesockets">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.11.7</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>By default set to 256. For agents with <code>keepAlive</code> enabled, this
sets the maximum number of sockets that will be left open in the free
state.</p>
<h3>agent.maxSockets<span><a class="mark" href="#http_agent_maxsockets" id="http_agent_maxsockets">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.3.6</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>By default set to <code>Infinity</code>. Determines how many concurrent sockets the agent
can have open per origin. Origin is the returned value of <a href="#http_agent_getname_options"><code>agent.getName()</code></a>.</p>
<h3>agent.requests<span><a class="mark" href="#http_agent_requests" id="http_agent_requests">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.9</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
</ul>
<p>An object which contains queues of requests that have not yet been assigned to
sockets. Do not modify.</p>
<h3>agent.sockets<span><a class="mark" href="#http_agent_sockets" id="http_agent_sockets">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.3.6</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
</ul>
<p>An object which contains arrays of sockets currently in use by the
agent. Do not modify.</p>
<h2>Class: http.ClientRequest<span><a class="mark" href="#http_class_http_clientrequest" id="http_class_http_clientrequest">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.17</span>
</div>
<p>This object is created internally and returned from <a href="#http_http_request_options_callback"><code>http.request()</code></a>. It
represents an <em>in-progress</em> request whose header has already been queued. The
header is still mutable using the <a href="#http_request_setheader_name_value"><code>setHeader(name, value)</code></a>,
<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
be sent along with the first data chunk or when calling <a href="#http_request_end_data_encoding_callback"><code>request.end()</code></a>.</p>
<p>To get the response, add a listener for <a href="#http_event_response"><code>'response'</code></a> to the request object.
<a href="#http_event_response"><code>'response'</code></a> will be emitted from the request object when the response
headers have been received. The <a href="#http_event_response"><code>'response'</code></a> event is executed with one
argument which is an instance of <a href="#http_class_http_incomingmessage"><code>http.IncomingMessage</code></a>.</p>
<p>During the <a href="#http_event_response"><code>'response'</code></a> event, one can add listeners to the
response object; particularly to listen for the <code>'data'</code> event.</p>
<p>If no <a href="#http_event_response"><code>'response'</code></a> handler is added, then the response will be
entirely discarded. However, if a <a href="#http_event_response"><code>'response'</code></a> event handler is added,
then the data from the response object <strong>must</strong> be consumed, either by
calling <code>response.read()</code> whenever there is a <code>'readable'</code> event, or
by adding a <code>'data'</code> handler, or by calling the <code>.resume()</code> method.
Until the data is consumed, the <code>'end'</code> event will not fire. Also, until
the data is read it will consume memory that can eventually lead to a
'process out of memory' error.</p>
<p>Node.js does not check whether Content-Length and the length of the
body which has been transmitted are equal or not.</p>
<p>The request inherits from <a href="stream.html#stream_stream">Stream</a>, and additionally implements the
following:</p>
<h3>Event: 'abort'<span><a class="mark" href="#http_event_abort" id="http_event_abort">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v1.4.1</span>
</div>
<p>Emitted when the request has been aborted by the client. This event is only
emitted on the first call to <code>abort()</code>.</p>
<h3>Event: 'connect'<span><a class="mark" href="#http_event_connect" id="http_event_connect">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.7.0</span>
</div>
<ul>
<li><code>response</code> <a href="http.html#http_class_http_incomingmessage" class="type"><http.IncomingMessage></a></li>
<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
<li><code>head</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li>
</ul>
<p>Emitted each time a server responds to a request with a <code>CONNECT</code> method. If
this event is not being listened for, clients receiving a <code>CONNECT</code> method will
have their connections closed.</p>
<p>A client and server pair demonstrating how to listen for the <code>'connect'</code> event:</p>
<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>);
<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>);
<span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
<span class="hljs-comment">// Create an HTTP tunneling proxy</span>
<span class="hljs-keyword">const</span> proxy = http.createServer(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
res.writeHead(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span> });
res.end(<span class="hljs-string">'okay'</span>);
});
proxy.on(<span class="hljs-string">'connect'</span>, (req, cltSocket, head) => {
<span class="hljs-comment">// connect to an origin server</span>
<span class="hljs-keyword">const</span> srvUrl = url.parse(<span class="hljs-string">`http://<span class="hljs-subst">${req.url}</span>`</span>);
<span class="hljs-keyword">const</span> srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
cltSocket.write(<span class="hljs-string">'HTTP/1.1 200 Connection Established\r\n'</span> +
<span class="hljs-string">'Proxy-agent: Node.js-Proxy\r\n'</span> +
<span class="hljs-string">'\r\n'</span>);
srvSocket.write(head);
srvSocket.pipe(cltSocket);
cltSocket.pipe(srvSocket);
});
});
<span class="hljs-comment">// now that proxy is running</span>
proxy.listen(<span class="hljs-number">1337</span>, <span class="hljs-string">'127.0.0.1'</span>, () => {
<span class="hljs-comment">// make a request to a tunneling proxy</span>
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
<span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">method</span>: <span class="hljs-string">'CONNECT'</span>,
<span class="hljs-attr">path</span>: <span class="hljs-string">'www.google.com:80'</span>
};
<span class="hljs-keyword">const</span> req = http.request(options);
req.end();
req.on(<span class="hljs-string">'connect'</span>, (res, socket, head) => {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'got connected!'</span>);
<span class="hljs-comment">// make a request over an HTTP tunnel</span>
socket.write(<span class="hljs-string">'GET / HTTP/1.1\r\n'</span> +
<span class="hljs-string">'Host: www.google.com:80\r\n'</span> +
<span class="hljs-string">'Connection: close\r\n'</span> +
<span class="hljs-string">'\r\n'</span>);
socket.on(<span class="hljs-string">'data'</span>, (chunk) => {
<span class="hljs-built_in">console</span>.log(chunk.toString());
});
socket.on(<span class="hljs-string">'end'</span>, () => {
proxy.close();
});
});
});</code></pre>
<h3>Event: 'continue'<span><a class="mark" href="#http_event_continue" id="http_event_continue">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.3.2</span>
</div>
<p>Emitted when the server sends a '100 Continue' HTTP response, usually because
the request contained 'Expect: 100-continue'. This is an instruction that
the client should send the request body.</p>
<h3>Event: 'information'<span><a class="mark" href="#http_event_information" id="http_event_information">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<p>Emitted when the server sends a 1xx response (excluding 101 Upgrade). This
event is emitted with a callback containing an object with a status code.</p>
<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>);
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
<span class="hljs-attr">path</span>: <span class="hljs-string">'/length_request'</span>
};
<span class="hljs-comment">// Make a request</span>
<span class="hljs-keyword">const</span> req = http.request(options);
req.end();
req.on(<span class="hljs-string">'information'</span>, (res) => {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Got information prior to main response: <span class="hljs-subst">${res.statusCode}</span>`</span>);
});</code></pre>
<p>101 Upgrade statuses do not fire this event due to their break from the
traditional HTTP request/response chain, such as web sockets, in-place TLS
upgrades, or HTTP 2.0. To be notified of 101 Upgrade notices, listen for the
<a href="#http_event_upgrade"><code>'upgrade'</code></a> event instead.</p>
<h3>Event: 'response'<span><a class="mark" href="#http_event_response" id="http_event_response">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.0</span>
</div>
<ul>
<li><code>response</code> <a href="http.html#http_class_http_incomingmessage" class="type"><http.IncomingMessage></a></li>
</ul>
<p>Emitted when a response is received to this request. This event is emitted only
once.</p>
<h3>Event: 'socket'<span><a class="mark" href="#http_event_socket" id="http_event_socket">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.3</span>
</div>
<ul>
<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
</ul>
<p>Emitted after a socket is assigned to this request.</p>
<h3>Event: 'timeout'<span><a class="mark" href="#http_event_timeout" id="http_event_timeout">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.7.8</span>
</div>
<p>Emitted when the underlying socket times out from inactivity. This only notifies
that the socket has been idle. The request must be aborted manually.</p>
<p>See also: <a href="#http_request_settimeout_timeout_callback"><code>request.setTimeout()</code></a>.</p>
<h3>Event: 'upgrade'<span><a class="mark" href="#http_event_upgrade" id="http_event_upgrade">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.94</span>
</div>
<ul>
<li><code>response</code> <a href="http.html#http_class_http_incomingmessage" class="type"><http.IncomingMessage></a></li>
<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
<li><code>head</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li>
</ul>
<p>Emitted each time a server responds to a request with an upgrade. If this
event is not being listened for and the response status code is 101 Switching
Protocols, clients receiving an upgrade header will have their connections
closed.</p>
<p>A client server pair demonstrating how to listen for the <code>'upgrade'</code> event.</p>
<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>);
<span class="hljs-comment">// Create an HTTP server</span>
<span class="hljs-keyword">const</span> srv = http.createServer(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
res.writeHead(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span> });
res.end(<span class="hljs-string">'okay'</span>);
});
srv.on(<span class="hljs-string">'upgrade'</span>, (req, socket, head) => {
socket.write(<span class="hljs-string">'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'</span> +
<span class="hljs-string">'Upgrade: WebSocket\r\n'</span> +
<span class="hljs-string">'Connection: Upgrade\r\n'</span> +
<span class="hljs-string">'\r\n'</span>);
socket.pipe(socket); <span class="hljs-comment">// echo back</span>
});
<span class="hljs-comment">// now that server is running</span>
srv.listen(<span class="hljs-number">1337</span>, <span class="hljs-string">'127.0.0.1'</span>, () => {
<span class="hljs-comment">// make a request</span>
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
<span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">headers</span>: {
<span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Upgrade'</span>,
<span class="hljs-string">'Upgrade'</span>: <span class="hljs-string">'websocket'</span>
}
};
<span class="hljs-keyword">const</span> req = http.request(options);
req.end();
req.on(<span class="hljs-string">'upgrade'</span>, (res, socket, upgradeHead) => {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'got upgraded!'</span>);
socket.end();
process.exit(<span class="hljs-number">0</span>);
});
});</code></pre>
<h3>request.abort()<span><a class="mark" href="#http_request_abort" id="http_request_abort">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.3.8</span>
</div>
<p>Marks the request as aborting. Calling this will cause remaining data
in the response to be dropped and the socket to be destroyed.</p>
<h3>request.aborted<span><a class="mark" href="#http_request_aborted" id="http_request_aborted">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v11.0.0</td>
<td><p>The <code>aborted</code> property is no longer a timestamp number.</p></td></tr>
<tr><td>v0.11.14</td>
<td><p><span>Added in: v0.11.14</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>The <code>request.aborted</code> property will be <code>true</code> if the request has
been aborted.</p>
<h3>request.connection<span><a class="mark" href="#http_request_connection" id="http_request_connection">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div>
<ul>
<li><a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
</ul>
<p>See <a href="#http_request_socket"><code>request.socket</code></a>.</p>
<h3>request.end([data[, encoding]][, callback])<span><a class="mark" href="#http_request_end_data_encoding_callback" id="http_request_end_data_encoding_callback">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>This method now returns a reference to <code>ClientRequest</code>.</p></td></tr>
<tr><td>v0.1.90</td>
<td><p><span>Added in: v0.1.90</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type"><this></a></li>
</ul>
<p>Finishes sending the request. If any parts of the body are
unsent, it will flush them to the stream. If the request is
chunked, this will send the terminating <code>'0\r\n\r\n'</code>.</p>
<p>If <code>data</code> is specified, it is equivalent to calling
<a href="#http_request_write_chunk_encoding_callback"><code>request.write(data, encoding)</code></a> followed by <code>request.end(callback)</code>.</p>
<p>If <code>callback</code> is specified, it will be called when the request stream
is finished.</p>
<h3>request.finished<span><a class="mark" href="#http_request_finished" id="http_request_finished">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.0.1</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>The <code>request.finished</code> property will be <code>true</code> if <a href="#http_request_end_data_encoding_callback"><code>request.end()</code></a>
has been called. <code>request.end()</code> will automatically be called if the
request was initiated via <a href="#http_http_get_options_callback"><code>http.get()</code></a>.</p>
<h3>request.flushHeaders()<span><a class="mark" href="#http_request_flushheaders" id="http_request_flushheaders">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v1.6.0</span>
</div>
<p>Flush the request headers.</p>
<p>For efficiency reasons, Node.js normally buffers the request headers until
<code>request.end()</code> is called or the first chunk of request data is written. It
then tries to pack the request headers and data into a single TCP packet.</p>
<p>That's usually desired (it saves a TCP round-trip), but not when the first
data is not sent until possibly much later. <code>request.flushHeaders()</code> bypasses
the optimization and kickstarts the request.</p>
<h3>request.getHeader(name)<span><a class="mark" href="#http_request_getheader_name" id="http_request_getheader_name">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v1.6.0</span>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
</ul>
<p>Reads out a header on the request. Note that the name is case insensitive.
The type of the return value depends on the arguments provided to
<a href="#http_request_setheader_name_value"><code>request.setHeader()</code></a>.</p>
<pre><code class="language-js">request.setHeader(<span class="hljs-string">'content-type'</span>, <span class="hljs-string">'text/html'</span>);
request.setHeader(<span class="hljs-string">'Content-Length'</span>, Buffer.byteLength(body));
request.setHeader(<span class="hljs-string">'Cookie'</span>, [<span class="hljs-string">'type=ninja'</span>, <span class="hljs-string">'language=javascript'</span>]);
<span class="hljs-keyword">const</span> contentType = request.getHeader(<span class="hljs-string">'Content-Type'</span>);
<span class="hljs-comment">// contentType is 'text/html'</span>
<span class="hljs-keyword">const</span> contentLength = request.getHeader(<span class="hljs-string">'Content-Length'</span>);
<span class="hljs-comment">// contentLength is of type number</span>
<span class="hljs-keyword">const</span> cookie = request.getHeader(<span class="hljs-string">'Cookie'</span>);
<span class="hljs-comment">// cookie is of type string[]</span></code></pre>
<h3>request.maxHeadersCount<span><a class="mark" href="#http_request_maxheaderscount" id="http_request_maxheaderscount">#</a></span></h3>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>2000</code></li>
</ul>
<p>Limits maximum response headers count. If set to 0, no limit will be applied.</p>
<h3>request.removeHeader(name)<span><a class="mark" href="#http_request_removeheader_name" id="http_request_removeheader_name">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v1.6.0</span>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Removes a header that's already defined into headers object.</p>
<pre><code class="language-js">request.removeHeader(<span class="hljs-string">'Content-Type'</span>);</code></pre>
<h3>request.setHeader(name, value)<span><a class="mark" href="#http_request_setheader_name_value" id="http_request_setheader_name_value">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v1.6.0</span>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
</ul>
<p>Sets a single header value for headers object. If this header already exists in
the to-be-sent headers, its value will be replaced. Use an array of strings
here to send multiple headers with the same name. Non-string values will be
stored without modification. Therefore, <a href="#http_request_getheader_name"><code>request.getHeader()</code></a> may return
non-string values. However, the non-string values will be converted to strings
for network transmission.</p>
<pre><code class="language-js">request.setHeader(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'application/json'</span>);</code></pre>
<p>or</p>
<pre><code class="language-js">request.setHeader(<span class="hljs-string">'Cookie'</span>, [<span class="hljs-string">'type=ninja'</span>, <span class="hljs-string">'language=javascript'</span>]);</code></pre>
<h3>request.setNoDelay([noDelay])<span><a class="mark" href="#http_request_setnodelay_nodelay" id="http_request_setnodelay_nodelay">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.9</span>
</div>
<ul>
<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Once a socket is assigned to this request and is connected
<a href="net.html#net_socket_setnodelay_nodelay"><code>socket.setNoDelay()</code></a> will be called.</p>
<h3>request.setSocketKeepAlive([enable][, initialDelay])<span><a class="mark" href="#http_request_setsocketkeepalive_enable_initialdelay" id="http_request_setsocketkeepalive_enable_initialdelay">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.9</span>
</div>
<ul>
<li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
<li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>Once a socket is assigned to this request and is connected
<a href="net.html#net_socket_setkeepalive_enable_initialdelay"><code>socket.setKeepAlive()</code></a> will be called.</p>
<h3>request.setTimeout(timeout[, callback])<span><a class="mark" href="#http_request_settimeout_timeout_callback" id="http_request_settimeout_timeout_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.9</span>
</div>
<ul>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Milliseconds before a request times out.</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Optional function to be called when a timeout occurs.
Same as binding to the <code>'timeout'</code> event.</li>
<li>Returns: <a href="http.html#http_class_http_clientrequest" class="type"><http.ClientRequest></a></li>
</ul>
<p>Once a socket is assigned to this request and is connected
<a href="net.html#net_socket_settimeout_timeout_callback"><code>socket.setTimeout()</code></a> will be called.</p>
<h3>request.socket<span><a class="mark" href="#http_request_socket" id="http_request_socket">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div>
<ul>
<li><a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
</ul>
<p>Reference to the underlying socket. Usually users will not want to access
this property. In particular, the socket will not emit <code>'readable'</code> events
because of how the protocol parser attaches to the socket. The <code>socket</code>
may also be accessed via <code>request.connection</code>.</p>
<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>);
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">host</span>: <span class="hljs-string">'www.google.com'</span>,
};
<span class="hljs-keyword">const</span> req = http.get(options);
req.end();
req.once(<span class="hljs-string">'response'</span>, (res) => {
<span class="hljs-keyword">const</span> ip = req.socket.localAddress;
<span class="hljs-keyword">const</span> port = req.socket.localPort;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Your IP address is <span class="hljs-subst">${ip}</span> and your source port is <span class="hljs-subst">${port}</span>.`</span>);
<span class="hljs-comment">// consume response object</span>
});</code></pre>
<h3>request.write(chunk[, encoding][, callback])<span><a class="mark" href="#http_request_write_chunk_encoding_callback" id="http_request_write_chunk_encoding_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.29</span>
</div>
<ul>
<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Sends a chunk of the body. By calling this method
many times, a request body can be sent to a
server — in that case it is suggested to use the
<code>['Transfer-Encoding', 'chunked']</code> header line when
creating the request.</p>
<p>The <code>encoding</code> argument is optional and only applies when <code>chunk</code> is a string.
Defaults to <code>'utf8'</code>.</p>
<p>The <code>callback</code> argument is optional and will be called when this chunk of data
is flushed, but only if the chunk is non-empty.</p>
<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel
buffer. Returns <code>false</code> if all or part of the data was queued in user memory.
<code>'drain'</code> will be emitted when the buffer is free again.</p>
<p>When <code>write</code> function is called with empty string or buffer, it does
nothing and waits for more input.</p>
<h2>Class: http.Server<span><a class="mark" href="#http_class_http_server" id="http_class_http_server">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.17</span>
</div>
<p>This class inherits from <a href="net.html#net_class_net_server"><code>net.Server</code></a> and has the following additional
events:</p>
<h3>Event: 'checkContinue'<span><a class="mark" href="#http_event_checkcontinue" id="http_event_checkcontinue">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div>
<ul>
<li><code>request</code> <a href="http.html#http_class_http_incomingmessage" class="type"><http.IncomingMessage></a></li>
<li><code>response</code> <a href="http.html#http_class_http_serverresponse" class="type"><http.ServerResponse></a></li>
</ul>
<p>Emitted each time a request with an HTTP <code>Expect: 100-continue</code> is received.
If this event is not listened for, the server will automatically respond
with a <code>100 Continue</code> as appropriate.</p>
<p>Handling this event involves calling <a href="#http_response_writecontinue"><code>response.writeContinue()</code></a> if the
client should continue to send the request body, or generating an appropriate
HTTP response (e.g. 400 Bad Request) if the client should not continue to send
the request body.</p>
<p>Note that when this event is emitted and handled, the <a href="#http_event_request"><code>'request'</code></a> event will
not be emitted.</p>
<h3>Event: 'checkExpectation'<span><a class="mark" href="#http_event_checkexpectation" id="http_event_checkexpectation">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v5.5.0</span>
</div>
<ul>
<li><code>request</code> <a href="http.html#http_class_http_incomingmessage" class="type"><http.IncomingMessage></a></li>
<li><code>response</code> <a href="http.html#http_class_http_serverresponse" class="type"><http.ServerResponse></a></li>
</ul>
<p>Emitted each time a request with an HTTP <code>Expect</code> header is received, where the
value is not <code>100-continue</code>. If this event is not listened for, the server will
automatically respond with a <code>417 Expectation Failed</code> as appropriate.</p>
<p>Note that when this event is emitted and handled, the <a href="#http_event_request"><code>'request'</code></a> event will
not be emitted.</p>
<h3>Event: 'clientError'<span><a class="mark" href="#http_event_clienterror" id="http_event_clienterror">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v9.4.0</td>
<td><p>The <code>rawPacket</code> is the current buffer that just parsed. Adding this buffer to the error object of <code>'clientError'</code> event is to make it possible that developers can log the broken packet.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The default action of calling <code>.destroy()</code> on the <code>socket</code> will no longer take place if there are listeners attached for <code>'clientError'</code>.</p></td></tr>
<tr><td>v0.1.94</td>
<td><p><span>Added in: v0.1.94</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
</ul>
<p>If a client connection emits an <code>'error'</code> event, it will be forwarded here.
Listener of this event is responsible for closing/destroying the underlying
socket. For example, one may wish to more gracefully close the socket with a
custom HTTP response instead of abruptly severing the connection.</p>
<p>Default behavior is to close the socket with an HTTP '400 Bad Request' response
if possible, otherwise the socket is immediately destroyed.</p>
<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>
<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>);
<span class="hljs-keyword">const</span> server = http.createServer(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
res.end();
});
server.on(<span class="hljs-string">'clientError'</span>, (err, socket) => {
socket.end(<span class="hljs-string">'HTTP/1.1 400 Bad Request\r\n\r\n'</span>);
});
server.listen(<span class="hljs-number">8000</span>);</code></pre>
<p>When the <code>'clientError'</code> event occurs, there is no <code>request</code> or <code>response</code>
object, so any HTTP response sent, including response headers and payload,
<em>must</em> be written directly to the <code>socket</code> object. Care must be taken to
ensure the response is a properly formatted HTTP response message.</p>
<p><code>err</code> is an instance of <code>Error</code> with two extra columns:</p>
<ul>
<li><code>bytesParsed</code>: the bytes count of request packet that Node.js may have parsed
correctly;</li>
<li><code>rawPacket</code>: the raw packet of current request.</li>
</ul>
<h3>Event: 'close'<span><a class="mark" href="#http_event_close" id="http_event_close">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.4</span>
</div>
<p>Emitted when the server closes.</p>
<h3>Event: 'connect'<span><a class="mark" href="#http_event_connect_1" id="http_event_connect_1">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.7.0</span>
</div>
<ul>
<li><code>request</code> <a href="http.html#http_class_http_incomingmessage" class="type"><http.IncomingMessage></a> Arguments for the HTTP request, as it is in
the <a href="#http_event_request"><code>'request'</code></a> event</li>
<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> Network socket between the server and client</li>
<li><code>head</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The first packet of the tunneling stream (may be empty)</li>
</ul>
<p>Emitted each time a client requests an HTTP <code>CONNECT</code> method. If this event is
not listened for, then clients requesting a <code>CONNECT</code> method will have their
connections closed.</p>
<p>After this event is emitted, the request's socket will not have a <code>'data'</code>
event listener, meaning it will need to be bound in order to handle data
sent to the server on that socket.</p>
<h3>Event: 'connection'<span><a class="mark" href="#http_event_connection" id="http_event_connection">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.0</span>
</div>
<ul>
<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
</ul>