forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.1
1887 lines (1879 loc) · 40.7 KB
/
node.1
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
.\" Title: node
.\" Author:
.\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
.\" Date: 09/28/2009
.\" Manual:
.\" Source:
.\"
.TH "NODE" "1" "09/28/2009" "" ""
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.SH "NAME"
node - evented I/O for V8 javascript
.SH "SYNOPSIS"
An example of a web server written with Node which responds with "Hello World":
.sp
.sp
.RS 4
.nf
include("/utils\.js");
include("/http\.js");
createServer(function (request, response) {
response\.sendHeader(200, {"Content\-Type": "text/plain"});
response\.sendBody("Hello World\en");
response\.finish();
})\.listen(8000);
puts("Server running at http://127\.0\.0\.1:8000/");
.fi
.RE
To run the server, put the code into a file called example\.js and execute it with the node program
.sp
.sp
.RS 4
.nf
> node example\.js
Server running at http://127\.0\.0\.1:8000/
.fi
.RE
.SH "API"
Node supports 3 string encodings\. UTF\-8 ("utf8"), ASCII ("ascii"), and Binary ("binary")\. "ascii" and "binary" only look at the first 8 bits of the 16bit javascript string characters\. Both are relatively fast\(emuse them if you can\. "utf8" is slower and should be avoided when possible\.
.sp
Unless otherwise noted, functions are all asynchronous and do not block execution\.
.sp
.SS "Helpers"
These objects are available to all programs\.
.PP
node\.cwd()
.RS 4
Returns the current working directory of the process\.
.RE
.PP
__filename
.RS 4
The filename of the script being executed\.
.RE
.SS "The process Object"
process is the equivalent of window in browser\-side javascript\. It is the global scope\. process is an instance of node\.EventEmitter\.
.sp
.TS
allbox tab(:);
ltB ltB ltB.
T{
Event
T}:T{
Parameters
T}:T{
Notes
T}
.T&
lt lt lt.
T{
"exit"
.sp
T}:T{
code
.sp
T}:T{
Made when the process exits\. A listener on this event should not try to perform I/O since the process will forcibly exit in less than microsecond\. However, it is a good hook to perform constant time checks of the module\(cqs state (like for unit tests)\. The parameter code is the integer exit code passed to process\.exit()\.
.sp
T}
.TE
.PP
process\.exit(code=0)
.RS 4
Ends the process with the specified code\. By default it exits with the success code 0\.
.RE
.PP
process\.ARGV
.RS 4
An array containing the command line arguments\.
.RE
.PP
process\.ENV
.RS 4
An object containing the user environment\. See environ(7)\.
.RE
.SS "Utilities"
These function are in "/utils\.js"\. Use require("/utils\.js") to access them\.
.PP
puts(string)
.RS 4
Outputs the
string
and a trailing new\-line to
stdout\.
.RE
.PP
print(string)
.RS 4
Like
puts()
but without the trailing new\-line\.
.RE
.PP
debug(string)
.RS 4
A synchronous output function\. Will block the process and output the string immediately to stdout\.
.RE
.PP
inspect(object)
.RS 4
Return a string representation of the
object\. (For debugging\.)
.RE
.PP
exec(command)
.RS 4
Executes the command as a child process, buffers the output and returns it in a promise callback\.
.sp
.RS 4
.nf
include("/utils\.js");
exec("ls /")\.addCallback(function (stdout, stderr) {
puts(stdout);
});
.fi
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: stdout buffer, stderr buffer
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: exit code, stdout buffer, stderr buffer
.RE
.RE
.SS "Events"
Many objects in Node emit events: a TCP server emits an event each time there is a connection, a child process emits an event when it exits\. All objects which emit events are are instances of node\.EventEmitter\.
.sp
Events are represented by a snakecased string\. Here are some examples: "connection", "receive", "message_begin"\.
.sp
Functions can be then be attached to objects, to be executed when an event is emitted\. These functions are called \fIlisteners\fR\.
.sp
Some asynchronous file operations return an EventEmitter called a \fIpromise\fR\. A promise emits just a single event when the operation is complete\.
.sp
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
node.EventEmitter
.RS
All EventEmitters emit the event "newListener" when new listeners are added\.
.sp
.TS
allbox tab(:);
ltB ltB ltB.
T{
Event
T}:T{
Parameters
T}:T{
Notes
T}
.T&
lt lt lt.
T{
"newListener"
.sp
T}:T{
event, listener
.sp
T}:T{
This event is made any time someone adds a new listener\.
.sp
T}
.TE
.PP
emitter\.addListener(event, listener)
.RS 4
Adds a listener to the end of the listeners array for the specified event\.
.sp
.RS 4
.nf
server\.addListener("connection", function (socket) {
puts("someone connected!");
});
.fi
.RE
.RE
.PP
emitter\.listeners(event)
.RS 4
Returns an array of listeners for the specified event\. This array can be manipulated, e\.g\. to remove listeners\.
.RE
.PP
emitter\.emit(event, arg1, arg2, \&...)
.RS 4
Execute each of the listeners in order with the supplied arguments\.
.RE
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
node.Promise
.RS
node\.Promise inherits from node\.eventEmitter\. A promise emits one of two events: "success" or "error"\. After emitting its event, it will not emit anymore events\.
.sp
.TS
allbox tab(:);
ltB ltB ltB.
T{
Event
T}:T{
Parameters
T}:T{
Notes
T}
.T&
lt lt lt
lt lt lt.
T{
"success"
.sp
T}:T{
(depends)
.sp
T}:T{
.sp
T}
T{
"error"
.sp
T}:T{
(depends)
.sp
T}:T{
.sp
T}
.TE
.PP
promise\.addCallback(listener)
.RS 4
Adds a listener for the
"success"
event\. Returns the same promise object\.
.RE
.PP
promise\.addErrback(listener)
.RS 4
Adds a listener for the
"error"
event\. Returns the same promise object\.
.RE
.PP
promise\.emitSuccess(arg1, arg2, \&...)
.RS 4
If you created the promise (by doing
new node\.Promise()) then call
emitSuccess
to emit the
"success"
event with the given arguments\.
.sp
(promise\.emit("success", arg1, arg2, \&...)
should also work, but doesn\(cqt at the moment due to a bug; use
emitSuccess
instead\.)
.RE
.PP
promise\.emitError(arg1, arg2, \&...)
.RS 4
Emits the
"error"
event\.
.RE
.PP
promise\.wait()
.RS 4
Blocks futher execution until the promise emits a success or error event\. Events setup before the call to
promise\.wait()
was made may still be emitted and executed while
promise\.wait()
is blocking\.
.sp
If there was a single argument to the
"success"
event then it is returned\. If there were multiple arguments to
"success"
then they are returned as an array\.
.sp
If
"error"
was emitted instead,
wait()
throws an error\.
.sp
\fBIMPORTANT\fR
promise\.wait()
is not a true fiber/coroutine\. If any other promises are created and made to wait while the first promise waits, the first promise\(cqs wait will not return until all others return\. The benefit of this is a simple implementation and the event loop does not get blocked\. Disadvantage is the possibility of situations where the promise stack grows infinitely large because promises keep getting created and keep being told to wait()\. Use
promise\.wait()
sparingly\(emprobably best used only during program setup, not during busy server activity\.
.RE
.RE
.SS "Standard I/O"
Standard I/O is handled through a special object node\.stdio\. stdout and stdin are fully non\-blocking (even when piping to files)\. stderr is synchronous\.
.sp
.TS
allbox tab(:);
ltB ltB ltB.
T{
Event
T}:T{
Parameters
T}:T{
Notes
T}
.T&
lt lt lt
lt lt lt.
T{
"data"
.sp
T}:T{
data
.sp
T}:T{
Made when stdin has received a chunk of data\. Depending on the encoding that stdin was opened with, data will be a string\. This event will only be emited after node\.stdio\.open() has been called\.
.sp
T}
T{
"close"
.sp
T}:T{
.sp
T}:T{
Made when stdin has been closed\.
.sp
T}
.TE
.PP
node\.stdio\.open(encoding="utf8")
.RS 4
Open stdin\. The program will not exit until
node\.stdio\.close()
has been called or the
"close"
event has been emitted\.
.RE
.PP
node\.stdio\.write(data)
.RS 4
Write data to stdout\.
.RE
.PP
node\.stdio\.writeError(data)
.RS 4
Write data to stderr\. Synchronous\.
.RE
.PP
node\.stdio\.close()
.RS 4
Close stdin\.
.RE
.SS "Modules"
Node has a simple module loading system\. In Node, files and modules are in one\-to\-one correspondence\. As an example, foo\.js loads the module circle\.js\.
.sp
The contents of foo\.js:
.sp
.sp
.RS 4
.nf
var circle = require("circle\.js");
puts("The area of a circle of radius 4 is " + circle\.area(4));
.fi
.RE
The contents of circle\.js:
.sp
.sp
.RS 4
.nf
var PI = 3\.14;
exports\.area = function (r) {
return PI * r * r;
};
exports\.circumference = function (r) {
return 2 * PI * r;
};
.fi
.RE
The module circle\.js has exported the functions area() and circumference()\. To export an object, add to the special exports object\. (Alternatively, one can use this instead of exports\.) Variables local to the module will be private\. In this example the variable PI is private to circle\.js\.
.sp
The module path is relative to the file calling require()\. That is, circle\.js must be in the same directory as foo\.js for require() to find it\.
.sp
Like require() the function include() also loads a module\. Instead of returning a namespace object, include() will add the module\(cqs exports into the global namespace\. For example:
.sp
.sp
.RS 4
.nf
include("circle\.js");
puts("The area of a cirlce of radius 4 is " + area(4));
.fi
.RE
When an absolute path is given to require() or include(), like require("/mjsunit\.js") the module is searched for in the node\.libraryPaths array\. node\.libraryPaths on my system looks like this:
.sp
.sp
.RS 4
.nf
[ "/home/ryan/\.node_libraries"
, "/home/ryan/local/node/lib/node_libraries"
, "/"
]
.fi
.RE
That is, first Node looks for "/home/ryan/\.node_libraries/mjsunit\.js" and then for "/home/ryan/local/node/lib/node_libraries/mjsunit\.js"\. If not found, it finally looks for "/mjsunit\.js" (in the root directory)\.
.sp
node\.libraryPaths can be modified at runtime by simply unshifting new paths on to it and at startup with the NODE_LIBRARY_PATHS environmental variable (which should be a list of paths, colon separated)\.
.sp
Node comes with several libraries which are installed when "make install" is run\. These are currently undocumented, but do look them up in your system\.
.sp
(Functions require_async() and include_async() also exist\.)
.sp
.SS "Timers"
.PP
setTimeout(callback, delay)
.RS 4
To schedule execution of callback after delay milliseconds\. Returns a
timeoutId
for possible use with
clearTimeout()\.
.RE
.PP
clearTimeout(timeoutId)
.RS 4
Prevents said timeout from triggering\.
.RE
.PP
setInterval(callback, delay)
.RS 4
To schedule the repeated execution of callback every
delay
milliseconds\. Returns a
intervalId
for possible use with
clearInterval()\.
.RE
.PP
clearInterval(intervalId)
.RS 4
Stops a interval from triggering\.
.RE
.SS "Child Processes"
Node provides a tridirectional popen(3) facility through the class node\.ChildProcess\. It is possible to stream data through the child\(cqs stdin, stdout, and stderr in a fully non\-blocking way\.
.sp
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
node.ChildProcess
.RS
.TS
allbox tab(:);
ltB ltB ltB.
T{
Event
T}:T{
Parameters
T}:T{
Notes
T}
.T&
lt lt lt
lt lt lt
lt lt lt.
T{
"output"
.sp
T}:T{
data
.sp
T}:T{
Each time the child process sends data to its stdout, this event is emitted\. data is a string\. If the child process closes its stdout stream (a common thing to do on exit), this event will be emitted with data === null\.
.sp
T}
T{
"error"
.sp
T}:T{
data
.sp
T}:T{
Identical to the "output" event except for stderr instead of stdout\.
.sp
T}
T{
"exit"
.sp
T}:T{
code
.sp
T}:T{
This event is emitted after the child process ends\. code is the final exit code of the process\. One can be assured that after this event is emitted that the "output" and "error" callbacks will no longer be made\.
.sp
T}
.TE
.PP
node\.createChildProcess(command)
.RS 4
Launches a new process with the given
command\. For example:
.sp
.RS 4
.nf
var ls = node\.createChildProcess("ls \-lh /usr");
ls\.addListener("output", function (data) {
puts(data);
});
.fi
.RE
.RE
.PP
child\.pid
.RS 4
The PID of the child process\.
.RE
.PP
child\.write(data, encoding="ascii")
.RS 4
Write data to the child process\(cqs
stdin\. The second argument is optional and specifies the encoding: possible values are
"utf8",
"ascii", and
"binary"\.
.RE
.PP
child\.close()
.RS 4
Closes the process\(cqs
stdin
stream\.
.RE
.PP
child\.kill(signal=node\.SIGTERM)
.RS 4
Send a single to the child process\. If no argument is given, the process will be sent
node\.SIGTERM\. The standard POSIX signals are defined under the
node
namespace (node\.SIGINT,
node\.SIGUSR1, \&...)\.
.RE
.RE
.SS "File I/O"
File I/O is provided by simple wrappers around standard POSIX functions\. All POSIX wrappers have a similar form\. They return a promise (node\.Promise)\. Example:
.sp
.sp
.RS 4
.nf
var promise = node\.fs\.unlink("/tmp/hello");
promise\.addCallback(function () {
puts("successfully deleted /tmp/hello");
});
.fi
.RE
There is no guaranteed ordering to the POSIX wrappers\. The following is very much prone to error
.sp
.sp
.RS 4
.nf
node\.fs\.rename("/tmp/hello", "/tmp/world");
node\.fs\.stat("/tmp/world")\.addCallback(function (stats) {
puts("stats: " + JSON\.stringify(stats));
});
.fi
.RE
It could be that stat() is executed before the rename()\. The correct way to do this is to chain the promises\.
.sp
.sp
.RS 4
.nf
node\.fs\.rename("/tmp/hello", "/tmp/world")\.addCallback(function () {
node\.fs\.stat("/tmp/world")\.addCallback(function (stats) {
puts("stats: " + JSON\.stringify(stats));
});
});
.fi
.RE
Or use the promise\.wait() functionality:
.sp
.sp
.RS 4
.nf
node\.fs\.rename("/tmp/hello", "/tmp/world")\.wait();
node\.fs\.stat("/tmp/world")\.addCallback(function (stats) {
puts("stats: " + JSON\.stringify(stats));
});
.fi
.RE
.PP
node\.fs\.rename(path1, path2)
.RS 4
See rename(2)\.
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: no parameters\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.PP
node\.fs\.stat(path)
.RS 4
See stat(2)\.
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: Returns
node\.fs\.Stats
object\. It looks like this:
{ dev: 2049, ino: 305352, mode: 16877, nlink: 12, uid: 1000, gid: 1000, rdev: 0, size: 4096, blksize: 4096, blocks: 8, atime: "2009\-06\-29T11:11:55Z", mtime: "2009\-06\-29T11:11:40Z", ctime: "2009\-06\-29T11:11:40Z" }
See the
node\.fs\.Stats
section below for more information\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.PP
node\.fs\.unlink(path)
.RS 4
See unlink(2)
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: no parameters\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.PP
node\.fs\.rmdir(path)
.RS 4
See rmdir(2)
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: no parameters\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.PP
node\.fs\.mkdir(path, mode)
.RS 4
See mkdir(2)
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: no parameters\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.PP
node\.fs\.readdir(path)
.RS 4
Reads the contents of a directory\.
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: One argument, an array containing the names (strings) of the files in the directory (excluding "\." and "\.\.")\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.PP
node\.fs\.close(fd)
.RS 4
See close(2)
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: no parameters\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.PP
node\.fs\.open(path, flags, mode)
.RS 4
See open(2)\. The constants like
O_CREAT
are defined at
node\.O_CREAT\.
.sp
.RS 4
\h'-04'\(bu\h'+03'on success:
fd
is given as the parameter\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.PP
node\.fs\.write(fd, data, position, encoding)
.RS 4
Write data to the file specified by
fd\.
position
refers to the offset from the beginning of the file where this data should be written\. If
position
is
null, the data will be written at the current position\. See pwrite(2)\.
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: returns an integer
written
which specifies how many
\fIbytes\fR
were written\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.PP
node\.fs\.read(fd, length, position, encoding)
.RS 4
Read data from the file specified by
fd\.
.sp
length
is an integer specifying the number of bytes to read\.
.sp
position
is an integer specifying where to begin reading from in the file\.
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: returns
data, bytes_read, what was read from the file\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.PP
node\.fs\.cat(filename, encoding="utf8")
.RS 4
Outputs the entire contents of a file\. Example:
.sp
.RS 4
.nf
node\.fs\.cat("/etc/passwd")\.addCallback(function (content) {
puts(content);
});
.fi
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: returns
data, what was read from the file\.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: no parameters\.
.RE
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
node.fs.Stats
.RS
Objects returned from node\.fs\.stat() are of this type\.
.PP
stats\.isFile(), stats\.isDirectory(), stats\.isBlockDevice(), stats\.isCharacterDevice(), stats\.isSymbolicLink(), stats\.isFIFO(), stats\.isSocket()
.RS 4
\&...
.RE
.RE
.SS "HTTP"
To use the HTTP server and client one must require("/http\.js") or include("/http\.js")\.
.sp
The HTTP interfaces in Node 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\(emthe user is able to stream data\.
.sp
HTTP message headers are represented by an object like this
.sp
.sp
.RS 4
.nf
{ "Content\-Length": "123"
, "Content\-Type": "text/plain"
, "Connection": "keep\-alive"
, "Accept": "*/*"
}
.fi
.RE
In order to support the full spectrum of possible HTTP applications, Node\(cqs HTTP API is very low\-level\. It deals with connection handling and message parsing only\. It parses a message into headers and body but it does not parse the actual headers or the body\. That means, for example, that Node does not, and will never, provide API to access or manipulate Cookies or multi\-part bodies\. \fIThis is left to the user\.\fR
.sp
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
http.Server
.RS
.TS
allbox tab(:);
ltB ltB ltB.
T{
Event
T}:T{
Parameters
T}:T{
Notes
T}
.T&
lt lt lt
lt lt lt
lt lt lt.
T{
"request"
.sp
T}:T{
request, response
.sp
T}:T{
request is an instance of http\.ServerRequest response is an instance of http\.ServerResponse
.sp
T}
T{
"connection"
.sp
T}:T{
connection
.sp
T}:T{
When a new TCP connection is established\. connection is an object of type http\.Connection\. Usually users will not want to access this event\. The connection can also be accessed at request\.connection\.
.sp
T}
T{
"close"
.sp
T}:T{
errorno
.sp
T}:T{
Emitted when the server closes\. errorno is an integer which indicates what, if any, error caused the server to close\. If no error occured errorno will be 0\.
.sp
T}
.TE
.PP
http\.createServer(request_listener, options);
.RS 4
Returns a new web server object\.
.sp
The
options
argument is optional\. The
options
argument accepts the same values as the options argument for
tcp\.Server
does\.
.sp
The
request_listener
is a function which is automatically added to the
"request"
event\.
.RE
.PP
server\.listen(port, hostname)
.RS 4
Begin accepting connections on the specified port and hostname\. If the hostname is omitted, the server will accept connections directed to any address\. This function is synchronous\.
.RE
.PP
server\.close()
.RS 4
Stops the server from accepting new connections\.
.RE
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
http.ServerRequest
.RS
This object is created internally by a HTTP server\(emnot by the user\(emand passed as the first argument to a "request" listener\.
.sp
.TS
allbox tab(:);
ltB ltB ltB.
T{
Event
T}:T{
Parameters
T}:T{
Notes
T}
.T&
lt lt lt
lt lt lt.
T{
"body"
.sp
T}:T{
chunk
.sp
T}:T{
Emitted when a piece of the message body is received\. Example: A chunk of the body is given as the single argument\. The transfer\-encoding has been decoded\. The body chunk is a String\. The body encoding is set with request\.setBodyEncoding()\.
.sp
T}
T{
"complete"
.sp
T}:T{
.sp
T}:T{
Emitted exactly once for each message\. No arguments\. After emitted no other events will be emitted on the request\.
.sp
T}
.TE
.PP
request\.method
.RS 4
The request method as a string\. Read only\. Example:
"GET",
"DELETE"\.
.RE
.PP
request\.uri
.RS 4
Request URI Object\. This contains only the parameters that are present in the actual HTTP request\. That is, if the request is
.sp
.RS 4
.nf
GET /status?name=ryan HTTP/1\.1\er\en
Accept: */*\er\en
\er\en
.fi
.RE
Then
request\.uri
will be
.sp
.RS 4
.nf
{ path: "/status",
file: "status",
directory: "/",
params: { "name" : "ryan" }
}
.fi
.RE
In particular, note that
request\.uri\.protocol
is
undefined\. This is because there was no URI protocol given in the actual HTTP Request\.
.sp
request\.uri\.anchor,
request\.uri\.query,
request\.uri\.file,
request\.uri\.directory,
request\.uri\.path,
request\.uri\.relative,
request\.uri\.port,
request\.uri\.host,
request\.uri\.password,
request\.uri\.user,
request\.uri\.authority,
request\.uri\.protocol,