Revision 9c0db09d node.html
node.html | ||
---|---|---|
138 | 138 |
|
139 | 139 |
<p> This script can handle hundreds of concurrent requests while using |
140 | 140 |
little CPU or memory—<a href="#benchmarks">see benchmarks</a>. |
141 |
Check out <a href="#api">the documentation</a> for more examples. |
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
<p> Check out <a href="#api">the API documentation</a> for more examples. |
|
142 | 145 |
|
143 | 146 |
<p> Node is free to <a href="#download">download</a>, <a |
144 | 147 |
href="#api">use</a>, and <a href="#modules">build upon</a>.</p> |
... | ... | |
147 | 150 |
|
148 | 151 |
<h3>Evented Programming Makes More Sense</h3> |
149 | 152 |
|
150 |
difference between blocking/non-blocking design
|
|
153 |
Difference between blocking/non-blocking design
|
|
151 | 154 |
|
152 | 155 |
<p> There are many methods to write internet servers but they can |
153 | 156 |
fundamentally be divided into two camps: evented and threaded; non-blocking |
... | ... | |
371 | 374 |
|
372 | 375 |
<h3 id="modules">Modules</h3> |
373 | 376 |
|
374 |
<p>Node has simple module loading. Here is an example. This is the file |
|
375 |
<code>foo.js</code>: |
|
377 |
<p>Node has a simple module loading system. In Node, files and modules are |
|
378 |
in one-to-one correspondence. |
|
379 |
|
|
380 |
<p> As an example, |
|
381 |
<code>foo.js</code> loads the module <code>mjsunit.js</code>. |
|
382 |
|
|
383 |
<p>The contents of <code>foo.js</code>: |
|
384 |
|
|
376 | 385 |
<pre> |
377 | 386 |
include("mjsunit"); |
378 |
|
|
379 | 387 |
function onLoad () { |
380 | 388 |
assertEquals(1, 2); |
381 | 389 |
} |
382 | 390 |
</pre> |
383 |
<p>Here the module <code>mjsunit</code> has provided the function |
|
384 |
<code>assertEquals()</code>. |
|
385 |
|
|
386 |
<p> The module file, <code>mjsunit.js</code>, must be in the same directory |
|
387 |
as <code>foo.js</code> for <code>include()</code> to work. The |
|
388 |
<code>include()</code> function will insert all the exported objects from the |
|
389 |
module into the global namespace. |
|
390 |
|
|
391 |
<p> Because file loading does not happen instantaneously, and because Node |
|
392 |
has a policy of never blocking, the callback <code>onLoad()</code> is |
|
393 |
provided to notify the user when all the exported functions are completely |
|
394 |
loaded. |
|
395 |
|
|
396 |
<p> To export an object, add to the special object <code class="highlight">exports</code>. |
|
397 |
Let's look at how <code>mjsunit.js</code> does this |
|
391 |
<p>The contents of <code>mjsunit.js</code>: |
|
398 | 392 |
|
399 | 393 |
<pre> |
400 | 394 |
function fail (expected, found, name_opt) { |
401 | 395 |
// ... |
402 | 396 |
} |
403 |
|
|
404 | 397 |
function deepEquals (a, b) { |
405 | 398 |
// ... |
406 | 399 |
} |
407 |
|
|
408 | 400 |
<span class="highlight">exports</span>.assertEquals = function (expected, found, name_opt) { |
409 | 401 |
if (!deepEquals(found, expected)) { |
410 | 402 |
fail(expected, found, name_opt); |
411 | 403 |
} |
412 | 404 |
}; |
413 | 405 |
</pre> |
406 |
|
|
407 |
<p>Here the module <code>mjsunit.js</code> has exported the function |
|
408 |
<code>assertEquals()</code>. <code>mjsunit.js</code> must be in the |
|
409 |
same directory as <code>foo.js</code> for <code>include()</code> to find it. |
|
410 |
The module path is relative to the file calling <code>include()</code>. |
|
411 |
The module path does not include filename extensions like <code>.js</code>. |
|
412 |
|
|
413 |
<p> <code>include()</code> inserts the exported objects |
|
414 |
from the specified module into the global namespace. |
|
415 |
|
|
416 |
<p> Because file loading does not happen instantaneously, and because Node |
|
417 |
has a policy of never blocking, the callback <code>onLoad()</code> is |
|
418 |
provided to notify the user when all the included modules are loaded. |
|
419 |
Each file can have its own <code>onLoad()</code> callback. |
|
420 |
<code>onLoad()</code> will always be called exactly once for each file. |
|
421 |
|
|
422 |
<p> To export an object, add to the special <code |
|
423 |
class="highlight">exports</code> object. |
|
424 |
|
|
414 | 425 |
<p> The functions <code>fail</code> and <code>deepEquals</code> are not |
415 | 426 |
exported and remain private to the module. |
416 | 427 |
|
417 | 428 |
<p> In addition to <code>include()</code> a module can use |
418 | 429 |
<code>require()</code>. Instead of loading the exported objects into the |
419 |
global namespace, it will return a namespace object. Again, the members of
|
|
420 |
the namespace object can only be guaranteed to exist after the
|
|
421 |
<code>onLoad()</code> callback is made. For example:
|
|
430 |
global namespace, it will return a namespace object. The exported objects
|
|
431 |
can only be guaranteed to exist after the <code>onLoad()</code> callback is
|
|
432 |
made. For example: |
|
422 | 433 |
<pre> |
423 | 434 |
var mjsunit = require("mjsunit"); |
424 | 435 |
|
... | ... | |
427 | 438 |
} |
428 | 439 |
</pre> |
429 | 440 |
|
441 |
<p> <code>include()</code> and <code>require()</code> cannot be used after |
|
442 |
<code>onLoad()</code> is called. So put them at the beginning of your file. |
|
430 | 443 |
|
431 | 444 |
</body> |
432 | 445 |
</html> |
Also available in: Unified diff