Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 185 lines (119 sloc) 6.973 kb
2a420a7 @tj No longer gitignoring html
tj authored
1 <div class='mp'>
2 <h1>Globals</h1>
ef20620 Clean up flow of Globals chapter intro paragraph
Grant Heaslip authored
3 <p> As we have learnt, node's module system discourages the use of globals; however node provides a few important globals for use to utilize. The first and most important is the <code>process</code> global, which exposes process manipulation such as signalling, exiting, the process id (pid), and more. Other globals, such as the <code>console</code> object, are provided to those used to writing JavaScript for web browsers.</p>
2a420a7 @tj No longer gitignoring html
tj authored
4
5 <h2 id="console">console</h2>
6
fdf60a0 Clean up some console object descriptions
Grant Heaslip authored
7 <p>The <code>console</code> object contains several methods which are used to output information to <em>stdout</em> or <em>stderr</em>. Let's take a look at what each method does:</p>
2a420a7 @tj No longer gitignoring html
tj authored
8
9 <h3 id="console-log-">console.log()</h3>
10
fdf60a0 Clean up some console object descriptions
Grant Heaslip authored
11 <p>The most frequently used console method is <code>console.log()</code>, which simply writes to <em>stdout</em> and appends a line feed (<code>\n</code>). Currently aliased as <code>console.info()</code>.</p>
2a420a7 @tj No longer gitignoring html
tj authored
12
13 <pre><code>console.log('wahoo');
14 // =&gt; wahoo
15
16 console.log({ foo: 'bar' });
17 // =&gt; [object Object]
18 </code></pre>
19
20 <h3 id="console-error-">console.error()</h3>
21
22 <p>Identical to <code>console.log()</code>, however writes to <em>stderr</em>. Aliased as <code>console.warn()</code> as well.</p>
23
24 <pre><code>console.error('database connection failed');
25 </code></pre>
26
27 <h3 id="console-dir-">console.dir()</h3>
28
29 <p>Utilizes the <em>sys</em> module's <code>inspect()</code> method to pretty-print the object to
30 <em>stdout</em>.</p>
31
32 <pre><code>console.dir({ foo: 'bar' });
33 // =&gt; { foo: 'bar' }
34 </code></pre>
35
36 <h3 id="console-assert-">console.assert()</h3>
37
38 <p>Asserts that the given expression is truthy, or throws an exception.</p>
39
40 <pre><code>console.assert(connected, 'Database connection failed');
41 </code></pre>
42
43 <h2 id="process">process</h2>
44
65f2cdd Clean up process object descriptions
Grant Heaslip authored
45 <p>The <code>process</code> object is plastered with goodies. First we will take a look
46 at some properties that provide information about the node process itself:</p>
2a420a7 @tj No longer gitignoring html
tj authored
47
48 <h3 id="process-version">process.version</h3>
49
65f2cdd Clean up process object descriptions
Grant Heaslip authored
50 <p>The node version string, for example "v0.1.103".</p>
2a420a7 @tj No longer gitignoring html
tj authored
51
52 <h3 id="process-installPrefix">process.installPrefix</h3>
53
65f2cdd Clean up process object descriptions
Grant Heaslip authored
54 <p>The installation prefix. In my case "<em>/usr/local</em>", as node's binary was installed to "<em>/usr/local/bin/node</em>".</p>
2a420a7 @tj No longer gitignoring html
tj authored
55
56 <h3 id="process-execPath">process.execPath</h3>
57
65f2cdd Clean up process object descriptions
Grant Heaslip authored
58 <p>The path to the executable itself "<em>/usr/local/bin/node</em>".</p>
2a420a7 @tj No longer gitignoring html
tj authored
59
60 <h3 id="process-platform">process.platform</h3>
61
65f2cdd Clean up process object descriptions
Grant Heaslip authored
62 <p>The platform you are running on. For example, "darwin".</p>
2a420a7 @tj No longer gitignoring html
tj authored
63
64 <h3 id="process-pid">process.pid</h3>
65
66 <p>The process id.</p>
67
68 <h3 id="process-cwd-">process.cwd()</h3>
69
65f2cdd Clean up process object descriptions
Grant Heaslip authored
70 <p>Returns the current working directory. For example:</p>
2a420a7 @tj No longer gitignoring html
tj authored
71
72 <pre><code>cd ~ &amp;&amp; node
73 node&gt; process.cwd()
74 "/Users/tj"
75 </code></pre>
76
77 <h3 id="process-chdir-">process.chdir()</h3>
78
79 <p>Changes the current working directory to the path passed.</p>
80
81 <pre><code>process.chdir('/foo');
82 </code></pre>
83
84 <h3 id="process-getuid-">process.getuid()</h3>
85
86 <p>Returns the numerical user id of the running process.</p>
87
88 <h3 id="process-setuid-">process.setuid()</h3>
89
90 <p>Sets the effective user id for the running process. This method accepts both a numerical id, as well as a string. For example both <code>process.setuid(501)</code>, and <code>process.setuid('tj')</code> are valid.</p>
91
92 <h3 id="process-getgid-">process.getgid()</h3>
93
94 <p>Returns the numerical group id of the running process.</p>
95
96 <h3 id="process-setgid-">process.setgid()</h3>
97
65f2cdd Clean up process object descriptions
Grant Heaslip authored
98 <p>Similar to <code>process.setuid()</code> however operates on the group, also accepting a numerical value or string representation. For example, <code>process.setgid(20)</code> or <code>process.setgid('www')</code>.</p>
2a420a7 @tj No longer gitignoring html
tj authored
99
100 <h3 id="process-env">process.env</h3>
101
65f2cdd Clean up process object descriptions
Grant Heaslip authored
102 <p>An object containing the user's environment variables. For example:</p>
2a420a7 @tj No longer gitignoring html
tj authored
103
104 <pre><code>{ PATH: '/Users/tj/.gem/ruby/1.8/bin:/Users/tj/.nvm/current/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin'
105 , PWD: '/Users/tj/ebooks/masteringnode'
106 , EDITOR: 'mate'
107 , LANG: 'en_CA.UTF-8'
108 , SHLVL: '1'
109 , HOME: '/Users/tj'
110 , LOGNAME: 'tj'
111 , DISPLAY: '/tmp/launch-YCkT03/org.x:0'
112 , _: '/usr/local/bin/node'
113 , OLDPWD: '/Users/tj'
114 }
115 </code></pre>
116
117 <h3 id="process-argv">process.argv</h3>
118
119 <p>When executing a file with the <code>node</code> executable <code>process.argv</code> provides access to the argument vector, the first value being the node executable, second being the filename, and remaining values being the arguments passed.</p>
120
65f2cdd Clean up process object descriptions
Grant Heaslip authored
121 <p>For example, our source file <em>./src/process/misc.js</em> can be executed by running:</p>
2a420a7 @tj No longer gitignoring html
tj authored
122
123 <pre><code>$ node src/process/misc.js foo bar baz
124 </code></pre>
125
126 <p>in which we call <code>console.dir(process.argv)</code>, outputting the following:</p>
127
128 <pre><code>[ 'node'
129 , '/Users/tj/EBooks/masteringnode/src/process/misc.js'
130 , 'foo'
131 , 'bar'
132 , 'baz'
133 ]
134 </code></pre>
135
136 <h3 id="process-exit-">process.exit()</h3>
137
65f2cdd Clean up process object descriptions
Grant Heaslip authored
138 <p>The <code>process.exit()</code> method is synonymous with the C function <code>exit()</code>, in which an exit code > 0 is passed to indicate failure, or 0 is passed to indicate success. When invoked, the <em>exit</em> event is emitted, allowing a short time for arbitrary processing to occur before <code>process.reallyExit()</code> is called with the given status code.</p>
2a420a7 @tj No longer gitignoring html
tj authored
139
140 <h3 id="process-on-">process.on()</h3>
141
65f2cdd Clean up process object descriptions
Grant Heaslip authored
142 <p>The process itself is an <code>EventEmitter</code>, allowing you to do things like listen for uncaught exceptions via the <em>uncaughtException</em> event:</p>
2a420a7 @tj No longer gitignoring html
tj authored
143
144 <pre><code>process.on('uncaughtException', function(err){
145 console.log('got an error: %s', err.message);
146 process.exit(1);
147 });
148
149 setTimeout(function(){
150 throw new Error('fail');
151 }, 100);
152 </code></pre>
153
154 <h3 id="process-kill-">process.kill()</h3>
155
65f2cdd Clean up process object descriptions
Grant Heaslip authored
156 <p><code>process.kill()</code> method sends the signal passed to the given <em>pid</em>, defaulting to <strong>SIGINT</strong>. In the example below, we send the <strong>SIGTERM</strong> signal to the same node process to illustrate signal trapping, after which we output "terminating" and exit. Note that the second timeout of 1000 milliseconds is never reached.</p>
2a420a7 @tj No longer gitignoring html
tj authored
157
158 <pre><code>process.on('SIGTERM', function(){
159 console.log('terminating');
160 process.exit(1);
161 });
162
163 setTimeout(function(){
164 console.log('sending SIGTERM to process %d', process.pid);
165 process.kill(process.pid, 'SIGTERM');
166 }, 500);
167
168 setTimeout(function(){
169 console.log('never called');
170 }, 1000);
171 </code></pre>
172
173 <h3 id="errno">errno</h3>
174
65f2cdd Clean up process object descriptions
Grant Heaslip authored
175 <p>The <code>process</code> object is host of the error numbers, which reference what you would find in C-land. For example, <code>process.EPERM</code> represents a permission based error, while <code>process.ENOENT</code> represents a missing file or directory. Typically these are used within bindings to bridge the gap between C++ and JavaScript, but they're useful for handling exceptions as well:</p>
2a420a7 @tj No longer gitignoring html
tj authored
176
177 <pre><code>if (err.errno === process.ENOENT) {
178 // Display a 404 "Not Found" page
179 } else {
180 // Display a 500 "Internal Server Error" page
181 }
182 </code></pre>
183
184 </div>
Something went wrong with that request. Please try again.