-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapi.html
3305 lines (2250 loc) · 105 KB
/
api.html
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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name='generator' value='Ronn/v0.4.1'>
<title>node(1) -- evented I/O for V8 JavaScript</title>
<style type='text/css'>
*{
margin: 0;padding: 0;
}
html,body
{
height: 100%;
}
body
{
font-family:helvetica, arial, sans serif;
background:#22252a;
color:#eee;
font-size:16px;
line-height:1.3;
position:relative;
min-width: 690px;
}
a
{
color:#CD5;
}
a:focus
{
outline: none;
-moz-outline: none;
}
pre
{
overflow: hidden;
}
li
{
list-style: inside;
}
#man,#man code,#man pre,#man tt,#man kbd,#man samp
{
line-height:1.6;
color:#eee;
background:#22252a;
}
#man
{
margin: 0;
position: absolute;
top:0;
bottom:0;
left: 225px;
right: 0;
overflow: auto;
}
#man-content
{
padding: 0 20px;
max-width: 650px;
}
#man h1,#man h2,#man h3
{
color:#DCDDDE;
clear:left;
}
#man h1
{
background:url("logo.png") no-repeat scroll center 0 transparent;
height:111px;
margin:15px 0 20px;
text-align:center;
text-indent:-2000px;
}
#man h2
{
font-size:18px;
background:#000;
color:#CD5;
margin:10px 0;
padding:5px 10px;
}
#man h3
{
font-size:16px;
margin:0 0 0 0ex;
}
#man p,#man ul,#man ol,#man dl,#man pre
{
margin:0 0 18px;
}
#man pre
{
color:#CCCDCE;
background:#121314;
border-left:2ex solid #222;
margin:0 0 20px;
padding:5px 7px;
}
#man pre + h2,#man pre + h3
{
margin-top:22px;
}
#man h2 + pre,#man h3 + pre
{
margin-top:5px;
}
#man > p,#man > ul,#man > ol,#man > dl,#man > pre
{
margin-left:5%;
}
#man dt
{
clear:left;
margin:0;
}
#man dt.flush
{
float:left;
width:8ex;
}
#man dd
{
margin:0 0 0 9ex;
}
#man code,#man strong,#man b
{
font-weight:bold;
color:#ECEDEE;
}
#man pre code
{
font-weight:normal;
color:#DCDDDE;
background:inherit;
}
#man em,var,u
{
font-style:normal;
color:#CCCDCE;
border-bottom:1px solid #999;
}
#man ol.man,#man ol.man li
{
float:left;
width:33%;
list-style-type:none;
text-transform:uppercase;
font-size:18px;
color:#666;
letter-spacing:1px;
margin:2px 0 10px;
padding:0;
}
#man ol.man
{
width:100%;
}
#man ol.man li.tl
{
text-align:left;
}
#man ol.man li.tc
{
text-align:center;
letter-spacing:4px;
}
#man ol.man li.tr
{
text-align:right;
}
#man ol.man a
{
color:#666;
}
#man ol.man a:hover
{
color:#CCCDCE;
}
#toc
{
position: absolute;
top:0;
bottom:0;
left: 0;
padding-left: 30px;
width: 195px;
overflow: auto;
overflow-x: hidden;
font-size: 15px;
}
#toc li
{
text-wrap: word-wrap;
}
#toc a
{
display: inline-block;
width: 100%;
color: #fff;
text-decoration:none;
}
#toc > a:hover
{
color: rgba(255,255,255,0.7);
}
#toc > ul > li
{
border-bottom:1px solid #0f1214;
padding:5px 0 5px 5px;
list-style: none;
line-height: 1.3;
}
#toc ul ul
{
display: none;
}
#toc ul ul > li
{
border-top:1px solid rgba(0, 0, 0, 0.1);
color:#FFFFFF;
font-size:85%;
line-height:1.3;
list-style:disc outside none;
margin-left:25px;
max-width:165px;
padding:3px 0 5px 5px;
}
#toc li.active > a
{
color:#CD5;
}
.sh_sourceCode
{
font-family: monospace;
overflow:hidden;
}
#toc .toggler
{
-moz-user-select:none;
background:none repeat scroll 0 0 #000000;
color:#FFFFFF;
display:inline-block;
font-weight:bold;
height:31px;
line-height:32px;
margin:-5px 8px -18px -33px;
outline:medium none;
padding:0;
text-align:center;
width:25px;
}
.current-section
{
position: fixed;
top: 0;
margin: 0 !important;
}
#toctitle
{
background:none repeat scroll 0 0 #000000;
color:#CCDD55;
font-size:18px;
margin: 0 0 10px -30px;
padding: 10px;
}
</style>
<link rel="stylesheet" href="sh_vim-dark.css" type="text/css" />
</head>
<body>
<div id="toc">
<div id="toctitle">Node v0.1.100</div>
<noscript>JavaScript must be enabled in your browser to display the table of contents.</noscript>
</div>
<div id='man'>
<div id="man-content">
<h1 class='man-title'>node(1)</h1>
<ol class='head man'>
<li class='tl'>node(1)</li>
<li class='tc'></li>
<li class='tr'>node(1)</li>
</ol>
<h2 id='NAME'>NAME</h2>
<p><code>node</code> -- evented I/O for V8 JavaScript</p>
<h2 id="Synopsis">Synopsis</h2>
<p>An example of a web server written with Node which responds with 'Hello
World':</p>
<pre><code>var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
</code></pre>
<p>To run the server, put the code into a file called <code>example.js</code> and execute
it with the node program</p>
<pre><code>> node example.js
Server running at http://127.0.0.1:8124/
</code></pre>
<p>All of the examples in the documentation can be run similarly.</p>
<h2 id="Standard Modules">Standard Modules</h2>
<p>Node comes with a number of modules that are compiled in to the process,
most of which are documented below. The most common way to use these modules
is with <code>require('name')</code> and then assigning the return value to a local
variable with the same name as the module.</p>
<p>Example:</p>
<pre><code>var sys = require('sys');
</code></pre>
<p>It is possible to extend node with other modules. See <code>'Modules'</code></p>
<h2 id="Buffers">Buffers</h2>
<p>Pure Javascript is Unicode friendly but not nice to binary data. When
dealing with TCP streams or the file system, it's necessary to handle octet
streams. Node has several strategies for manipulating, creating, and
consuming octet streams.</p>
<p>Raw data is stored in instances of the <code>Buffer</code> class. A <code>Buffer</code> is similar
to an array of integers but corresponds to a raw memory allocation outside
the V8 heap. A <code>Buffer</code> cannot be resized.
Access the class with <code>require('buffer').Buffer</code>.</p>
<p>Converting between Buffers and JavaScript string objects requires an explicit encoding
method. Node supports 3 string encodings: UTF-8 (<code>'utf8'</code>), ASCII (<code>'ascii'</code>), and
Binary (<code>'binary'</code>).</p>
<ul>
<li><p><code>'ascii'</code> - for 7 bit ASCII data only. This encoding method is very fast, and will
strip the high bit if set.</p></li>
<li><p><code>'utf8'</code> - Unicode characters. Many web pages and other document formats use UTF-8.</p></li>
<li><p><code>'binary'</code> - A legacy encoding. Used to store raw binary data in a string
by only using the first 8 bits of every character. Don't use this.</p></li>
</ul>
<h3>new Buffer(size)</h3>
<p>Allocates a new buffer of <code>size</code> octets.</p>
<h3>new Buffer(array)</h3>
<p>Allocates a new buffer using an <code>array</code> of octets.</p>
<h3>new Buffer(str, encoding = 'utf8')</h3>
<p>Allocates a new buffer containing the given <code>str</code>.</p>
<h3>buffer.write(string, offset=0, encoding='utf8')</h3>
<p>Writes <code>string</code> to the buffer at <code>offset</code> using the given encoding. Returns
number of octets written. If <code>buffer</code> did not contain enough space to fit
the entire string it will write a partial amount of the string. In the case
of <code>'utf8'</code> encoding, the method will not write partial characters.</p>
<p>Example: write a utf8 string into a buffer, then print it</p>
<pre><code>Buffer = require('buffer').Buffer;
buf = new Buffer(256);
len = buf.write('\u00bd + \u00bc = \u00be', 0);
console.log(len + " bytes: " + buf.toString('utf8', 0, len));
// 12 bytes: ½ + ¼ = ¾
</code></pre>
<h3>buffer.toString(encoding, start, end)</h3>
<p>Decodes and returns a string from buffer data encoded with <code>encoding</code>
beginning at <code>start</code> and ending at <code>end</code>.</p>
<p>See <code>buffer.write()</code> example, above.</p>
<h3>buffer[index]</h3>
<p>Get and set the octet at <code>index</code>. The values refer to individual bytes,
so the legal range is between <code>0x00</code> and <code>0xFF</code> hex or <code>0</code> and <code>255</code>.</p>
<p>Example: copy an ASCII string into a buffer, one byte at a time:</p>
<pre><code>var Buffer = require('buffer').Buffer,
str = "node.js",
buf = new Buffer(str.length),
i;
for (i = 0; i < str.length ; i += 1) {
buf[i] = str.charCodeAt(i);
}
console.log(buf);
// node.js
</code></pre>
<h3>Buffer.byteLength(string, encoding)</h3>
<p>Gives the actual byte length of a string. This is not the same as
<code>String.prototype.length</code> since that returns the number of <em>characters</em> in a
string.</p>
<p>Example:</p>
<pre><code>var Buffer = require('buffer').Buffer,
str = '\u00bd + \u00bc = \u00be';
console.log(str + ": " + str.length + " characters, " +
Buffer.byteLength(str, 'utf8') + " bytes");
// ½ + ¼ = ¾: 9 characters, 12 bytes
</code></pre>
<h3>buffer.length</h3>
<p>The size of the buffer in bytes. Note that this is not necessarily the size
of the contents. <code>length</code> refers to the amount of memory allocated for the
buffer object. It does not change when the contents of the buffer are changed.</p>
<pre><code>var Buffer = require('buffer').Buffer,
buf = new Buffer(1234);
console.log(buf.length);
buf.write("some string", "ascii", 0);
console.log(buf.length);
// 1234
// 1234
</code></pre>
<h3>buffer.copy(targetBuffer, targetStart, sourceStart, sourceEnd)</h3>
<p>Does a memcpy() between buffers.</p>
<p>Example: build two Buffers, then copy <code>buf1</code> from byte 16 through byte 19
into <code>buf2</code>, starting at the 8th byte in <code>buf2</code>.</p>
<pre><code>var Buffer = require('buffer').Buffer,
buf1 = new Buffer(26),
buf2 = new Buffer(26),
i;
for (i = 0 ; i < 26 ; i += 1) {
buf1[i] = i + 97; // 97 is ASCII a
buf2[i] = 33; // ASCII !
}
buf1.copy(buf2, 8, 16, 20);
console.log(buf2.toString('ascii', 0, 25));
// !!!!!!!!qrst!!!!!!!!!!!!!
</code></pre>
<h3>buffer.slice(start, end)</h3>
<p>Returns a new buffer which references the
same memory as the old, but offset and cropped by the <code>start</code> and <code>end</code>
indexes.</p>
<p><strong>Modifying the new buffer slice will modify memory in the original buffer!</strong></p>
<p>Example: build a Buffer with the ASCII alphabet, take a slice, then modify one byte
from the original Buffer.</p>
<pre><code>var Buffer = require('buffer').Buffer,
buf1 = new Buffer(26), buf2,
i;
for (i = 0 ; i < 26 ; i += 1) {
buf1[i] = i + 97; // 97 is ASCII a
}
buf2 = buf1.slice(0, 3);
console.log(buf2.toString('ascii', 0, buf2.length));
buf1[0] = 33;
console.log(buf2.toString('ascii', 0, buf2.length));
// abc
// !bc
</code></pre>
<h2 id="EventEmitter">EventEmitter</h2>
<p>Many objects in Node emit events: a TCP server emits an event each time
there is a stream, a child process emits an event when it exits. All
objects which emit events are instances of <code>events.EventEmitter</code>.</p>
<p>Events are represented by a camel-cased string. Here are some examples:
<code>'stream'</code>, <code>'data'</code>, <code>'messageBegin'</code>.</p>
<p>Functions can be then be attached to objects, to be executed when an event
is emitted. These functions are called <em>listeners</em>.</p>
<p><code>require('events').EventEmitter</code> to access the <code>EventEmitter</code> class.</p>
<p>All EventEmitters emit the event <code>'newListener'</code> when new listeners are
added.</p>
<p>When an EventEmitter experiences an error, the typical action is to emit an
<code>'error'</code> event. Error events are special--if there is no handler for them
they will print a stack trace and exit the program.</p>
<h3>Event: 'newListener'</h3>
<p><code>function (event, listener) { }</code></p>
<p>This event is made any time someone adds a new listener.</p>
<h3>Event: 'error'</h3>
<p><code>function (exception) { }</code></p>
<p>If an error was encountered, then this event is emitted. This event is
special - when there are no listeners to receive the error Node will
terminate execution and display the exception's stack trace.</p>
<h3>emitter.addListener(event, listener)</h3>
<p>Adds a listener to the end of the listeners array for the specified event.</p>
<pre><code>server.addListener('stream', function (stream) {
console.log('someone connected!');
});
</code></pre>
<h3>emitter.removeListener(event, listener)</h3>
<p>Remove a listener from the listener array for the specified event.
<strong>Caution</strong>: changes array indices in the listener array behind the listener.</p>
<h3>emitter.removeAllListeners(event)</h3>
<p>Removes all listeners from the listener array for the specified event.</p>
<h3>emitter.listeners(event)</h3>
<p>Returns an array of listeners for the specified event. This array can be
manipulated, e.g. to remove listeners.</p>
<h3>emitter.emit(event, arg1, arg2, ...)</h3>
<p>Execute each of the listeners in order with the supplied arguments.</p>
<h2 id="Streams">Streams</h2>
<p>A stream is an abstract interface implemented by various objects in Node.
For example a request to an HTTP server is a stream, as is stdout. Streams
are readable, writable, or both. All streams are instances of <code>EventEmitter</code>.</p>
<h2 id="Readable Stream">Readable Stream</h2>
<p>A <strong>readable stream</strong> has the following methods, members, and events.</p>
<h3>Event: 'data'</h3>
<p><code>function (data) { }</code></p>
<p>The <code>'data'</code> event emits either a <code>Buffer</code> (by default) or a string if
<code>setEncoding()</code> was used.</p>
<h3>Event: 'end'</h3>
<p><code>function () { }</code></p>
<p>Emitted when the stream has received an EOF (FIN in TCP terminology).
Indicates that no more <code>'data'</code> events will happen. If the stream is also
writable, it may be possible to continue writing.</p>
<h3>Event: 'error'</h3>
<p><code>function (exception) { }</code></p>
<p>Emitted if there was an error receiving data.</p>
<h3>Event: 'close'</h3>
<p><code>function () { }</code></p>
<p>Emitted when the underlying file descriptor has be closed. Not all streams
will emit this. (For example, an incoming HTTP request will not emit
<code>'close'</code>.)</p>
<h3>Event: 'fd'</h3>
<p><code>function (fd) { }</code></p>
<p>Emitted when a file descriptor is received on the stream. Only UNIX streams
support this functionality; all others will simply never emit this event.</p>
<h3>stream.setEncoding(encoding)</h3>
<p>Makes the data event emit a string instead of a <code>Buffer</code>. <code>encoding</code> can be
<code>'utf8'</code>, <code>'ascii'</code>, or <code>'binary'</code>.</p>
<h3>stream.pause()</h3>
<p>Pauses the incoming <code>'data'</code> events.</p>
<h3>stream.resume()</h3>
<p>Resumes the incoming <code>'data'</code> events after a <code>pause()</code>.</p>
<h3>stream.destroy()</h3>
<p>Closes the underlying file descriptor. Stream will not emit any more events.</p>
<h2 id="Writable Stream">Writable Stream</h2>
<p>A <strong>writable stream</strong> has the following methods, members, and events.</p>
<h3>Event: 'drain'</h3>
<p><code>function () { }</code></p>
<p>Emitted after a <code>write()</code> method was called that returned <code>false</code> to
indicate that it is safe to write again.</p>
<h3>Event: 'error'</h3>
<p><code>function (exception) { }</code></p>
<p>Emitted on error with the exception <code>exception</code>.</p>
<h3>Event: 'close'</h3>
<p><code>function () { }</code></p>
<p>Emitted when the underlying file descriptor has been closed.</p>
<h3>stream.write(string, encoding, [fd])</h3>
<p>Writes <code>string</code> with the given <code>encoding</code> to the stream. Returns <code>true</code> if
the string has been flushed to the kernel buffer. Returns <code>false</code> to
indicate that the kernel buffer is full, and the data will be sent out in
the future. The <code>'drain'</code> event will indicate when the kernel buffer is
empty again. The <code>encoding</code> defaults to <code>'utf8'</code>.</p>
<p>If the optional <code>fd</code> parameter is specified, it is interpreted as an integral
file descriptor to be sent over the stream. This is only supported for UNIX
streams, and is silently ignored otherwise. When writing a file descriptor in
this manner, closing the descriptor before the stream drains risks sending an
invalid (closed) FD.</p>
<h3>stream.write(buffer)</h3>
<p>Same as the above except with a raw buffer.</p>
<h3>stream.end()</h3>
<p>Terminates the stream with EOF or FIN.</p>
<h3>stream.end(string, encoding)</h3>
<p>Sends <code>string</code> with the given <code>encoding</code> and terminates the stream with EOF
or FIN. This is useful to reduce the number of packets sent.</p>
<h3>stream.end(buffer)</h3>
<p>Same as above but with a <code>buffer</code>.</p>
<h3>stream.destroy()</h3>
<p>Closes the underlying file descriptor. Stream will not emit any more events.</p>
<h2 id="Global Objects">Global Objects</h2>
<p>These object are available in the global scope and can be accessed from anywhere.</p>
<h3>global</h3>
<p>The global namespace object.</p>
<h3>process</h3>
<p>The process object. Most stuff lives in here. See the <code>'process object'</code>
section.</p>
<h3>require()</h3>
<p>To require modules. See the <code>'Modules'</code> section.</p>
<h3>require.paths</h3>
<p>An array of search paths for <code>require()</code>. This array can be modified to add custom paths.</p>
<p>Example: add a new path to the beginning of the search list</p>
<pre><code>require.paths.unshift('/usr/local/node');
console.log(require.paths);
// /usr/local/node,/Users/mjr/.node_libraries
</code></pre>
<h3>__filename</h3>
<p>The filename of the script being executed. This is the absolute path, and not necessarily
the same filename passed in as a command line argument.</p>
<h3>__dirname</h3>
<p>The dirname of the script being executed.</p>
<p>Example: running <code>node example.js</code> from <code>/Users/mjr</code></p>
<pre><code>console.log(__filename);
console.log(__dirname);
// /Users/mjr/example.js
// /Users/mjr
</code></pre>
<h3>module</h3>
<p>A reference to the current module (of type <code>process.Module</code>). In particular
<code>module.exports</code> is the same as the <code>exports</code> object. See <code>src/process.js</code>
for more information.</p>
<h2 id="process">process</h2>
<p>The <code>process</code> object is a global object and can be accessed from anywhere.
It is an instance of <code>EventEmitter</code>.</p>
<h3>Event: 'exit'</h3>
<p><code>function () {}</code></p>
<p>Emitted when the process is about to exit. This is a good hook to perform
constant time checks of the module's state (like for unit tests). The main
event loop will no longer be run after the 'exit' callback finishes, so
timers may not be scheduled.</p>
<p>Example of listening for <code>exit</code>:</p>
<pre><code>process.addListener('exit', function () {
process.nextTick(function () {
console.log('This will not run');
});
console.log('About to exit.');
});
</code></pre>
<h3>Event: 'uncaughtException'</h3>
<p><code>function (err) { }</code></p>
<p>Emitted when an exception bubbles all the way back to the event loop. If a
listener is added for this exception, the default action (which is to print
a stack trace and exit) will not occur.</p>
<p>Example of listening for <code>uncaughtException</code>:</p>
<pre><code>process.addListener('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
setTimeout(function () {
console.log('This will still run.');
}, 500);
// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
console.log('This will not run.');
</code></pre>
<p>Note that <code>uncaughtException</code> is a very crude mechanism for exception
handling. Using try / catch in your program will give you more control over
your program's flow. Especially for server programs that are designed to
stay running forever, <code>uncaughtException</code> can be a useful safety mechanism.</p>
<h3>Signal Events</h3>
<p><code>function () {}</code></p>
<p>Emitted when the processes receives a signal. See sigaction(2) for a list of
standard POSIX signal names such as SIGINT, SIGUSR1, etc.</p>
<p>Example of listening for <code>SIGINT</code>:</p>
<pre><code>var stdin = process.openStdin();
process.addListener('SIGINT', function () {
console.log('Got SIGINT. Press Control-D to exit.');
});
</code></pre>
<p>An easy way to send the <code>SIGINT</code> signal is with <code>Control-C</code> in most terminal
programs.</p>
<h3>process.stdout</h3>
<p>A writable stream to <code>stdout</code>.</p>
<p>Example: the definition of <code>console.log</code></p>
<pre><code>console.log = function (d) {
process.stdout.write(d + '\n');
};
</code></pre>
<h3>process.openStdin()</h3>
<p>Opens the standard input stream, returns a readable stream.</p>
<p>Example of opening standard input and listening for both events:</p>
<pre><code>var stdin = process.openStdin();
stdin.setEncoding('utf8');
stdin.addListener('data', function (chunk) {
process.stdout.write('data: ' + chunk);
});
stdin.addListener('end', function () {
process.stdout.write('end');
});
</code></pre>
<h3>process.argv</h3>
<p>An array containing the command line arguments. The first element will be
'node', the second element will be the name of the JavaScript file. The
next elements will be any additional command line arguments.</p>
<pre><code>// print process.argv
process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});
</code></pre>
<p>This will generate:</p>
<pre><code>$ node process-2.js one two=three four
0: node
1: /Users/mjr/work/node/process-2.js
2: one
3: two=three
4: four
</code></pre>
<h3>process.execPath</h3>
<p>This is the absolute pathname of the executable that started the process.</p>
<h3>process.chdir(directory)</h3>
<p>Changes the current working directory of the process or throws an exception if that fails.</p>
<pre><code>console.log('Starting directory: ' + process.cwd());
try {
process.chdir('/tmp');
console.log('New directory: ' + process.cwd());
}
catch (err) {
console.log('chdir: ' + err);
}
</code></pre>
<h3>process.compile(code, filename)</h3>
<p>Similar to <code>eval</code> except that you can specify a <code>filename</code> for better
error reporting and the <code>code</code> cannot see the local scope. The value of <code>filename</code>
will be used as a filename if a stack trace is generated by the compiled code.</p>
<p>Example of using <code>process.compile</code> and <code>eval</code> to run the same code:</p>
<pre><code>var localVar = 123,
compiled, evaled;
compiled = process.compile('localVar = 1;', 'myfile.js');
console.log('localVar: ' + localVar + ', compiled: ' + compiled);
evaled = eval('localVar = 1;');
console.log('localVar: ' + localVar + ', evaled: ' + evaled);
// localVar: 123, compiled: 1
// localVar: 1, evaled: 1
</code></pre>
<p><code>process.compile</code> does not have access to the local scope, so <code>localVar</code> is unchanged.
<code>eval</code> does have access to the local scope, so <code>localVar</code> is changed.</p>
<p>In case of syntax error in <code>code</code>, <code>process.compile</code> exits node.</p>
<p>See also: <code>Script</code></p>
<h3>process.cwd()</h3>
<p>Returns the current working directory of the process.</p>
<pre><code>console.log('Current directory: ' + process.cwd());
</code></pre>
<h3>process.env</h3>
<p>An object containing the user environment. See environ(7).</p>
<h3>process.exit(code)</h3>
<p>Ends the process with the specified <code>code</code>. If omitted, exit uses the
'success' code <code>0</code>.</p>
<p>To exit with a 'failure' code:</p>
<pre><code>process.exit(1);
</code></pre>
<p>The shell that executed node should see the exit code as 1.</p>
<h3>process.getgid(), process.setgid(id)</h3>
<p>Gets/sets the group identity of the process. (See setgid(2).) This is the numerical group id, not the group name.</p>
<pre><code>console.log('Current gid: ' + process.getgid());
try {
process.setgid(501);
console.log('New gid: ' + process.getgid());
}
catch (err) {
console.log('Failed to set gid: ' + err);
}
</code></pre>
<h3>process.getuid(), process.setuid(id)</h3>
<p>Gets/sets the user identity of the process. (See setuid(2).) This is the numerical userid, not the username.</p>
<pre><code>console.log('Current uid: ' + process.getuid());
try {
process.setuid(501);
console.log('New uid: ' + process.getuid());
}
catch (err) {
console.log('Failed to set uid: ' + err);
}
</code></pre>
<h3>process.version</h3>
<p>A compiled-in property that exposes <code>NODE_VERSION</code>.</p>
<pre><code>console.log('Version: ' + process.version);
</code></pre>
<h3>process.installPrefix</h3>
<p>A compiled-in property that exposes <code>NODE_PREFIX</code>.</p>
<pre><code>console.log('Prefix: ' + process.installPrefix);
</code></pre>
<h3>process.kill(pid, signal)</h3>
<p>Send a signal to a process. <code>pid</code> is the process id and <code>signal</code> is the
string describing the signal to send. Signal names are strings like
'SIGINT' or 'SIGUSR1'. If omitted, the signal will be 'SIGINT'.
See kill(2) for more information.</p>
<p>Note that just because the name of this function is <code>process.kill</code>, it is
really just a signal sender, like the <code>kill</code> system call. The signal sent
may do something other than kill the target process.</p>
<p>Example of sending a signal to yourself:</p>
<pre><code>process.addListener('SIGHUP', function () {
console.log('Got SIGHUP signal.');