From 9c0db09d950c3cd3a6f5403021b9ebd609294fc8 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 16 May 2009 13:53:18 +0200 Subject: [PATCH] Add to module documentation. --- node.html | 63 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/node.html b/node.html index 06883089a08..7e8d6ff34c3 100644 --- a/node.html +++ b/node.html @@ -138,7 +138,10 @@ new node.http.Server(function (msg) {

This script can handle hundreds of concurrent requests while using little CPU or memory—see benchmarks. -Check out the documentation for more examples. + + + +

Check out the API documentation for more examples.

Node is free to download, use, and build upon.

@@ -147,7 +150,7 @@ Check out the documentation for more examples.

Evented Programming Makes More Sense

-difference between blocking/non-blocking design +Difference between blocking/non-blocking design

There are many methods to write internet servers but they can fundamentally be divided into two camps: evented and threaded; non-blocking @@ -371,54 +374,62 @@ msg.sendHeader( 200

Modules

-

Node has simple module loading. Here is an example. This is the file -foo.js: +

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 mjsunit.js. + +

The contents of foo.js: +

 include("mjsunit");
-
 function onLoad () {
   assertEquals(1, 2);
 }
 
-

Here the module mjsunit has provided the function -assertEquals(). - -

The module file, mjsunit.js, must be in the same directory -as foo.js for include() to work. The -include() function will insert all the exported objects from the -module into the global namespace. - -

Because file loading does not happen instantaneously, and because Node -has a policy of never blocking, the callback onLoad() is -provided to notify the user when all the exported functions are completely -loaded. - -

To export an object, add to the special object exports. -Let's look at how mjsunit.js does this +

The contents of mjsunit.js:

 function fail (expected, found, name_opt) {
   // ...
 }
-
 function deepEquals (a, b) {
   // ...
 }
-
 exports.assertEquals = function (expected, found, name_opt) {
   if (!deepEquals(found, expected)) {
     fail(expected, found, name_opt);
   }
 };
 
+ +

Here the module mjsunit.js has exported the function +assertEquals(). mjsunit.js must be in the +same directory as foo.js for include() to find it. +The module path is relative to the file calling include(). +The module path does not include filename extensions like .js. + +

include() inserts the exported objects +from the specified module into the global namespace. + +

Because file loading does not happen instantaneously, and because Node +has a policy of never blocking, the callback onLoad() is +provided to notify the user when all the included modules are loaded. +Each file can have its own onLoad() callback. +onLoad() will always be called exactly once for each file. + +

To export an object, add to the special exports object. +

The functions fail and deepEquals are not exported and remain private to the module.

In addition to include() a module can use require(). Instead of loading the exported objects into the -global namespace, it will return a namespace object. Again, the members of -the namespace object can only be guaranteed to exist after the -onLoad() callback is made. For example: +global namespace, it will return a namespace object. The exported objects +can only be guaranteed to exist after the onLoad() callback is +made. For example:

 var mjsunit = require("mjsunit");
 
@@ -427,6 +438,8 @@ function onLoad () {
 }
 
+

include() and require() cannot be used after +onLoad() is called. So put them at the beginning of your file.