diff --git a/node.html b/node.html index 280ce04cc8..fc72e5ec9d 100644 --- a/node.html +++ b/node.html @@ -43,21 +43,21 @@ h1 a { color: inherit; } h2 { margin: 2em 0; - font-size: inherit; + font-size: 45px; line-height: inherit; font-weight: bold; } h3 { margin: 1em 0; - font-size: inherit; + font-size: 30px; line-height: inherit; font-weight: inherit; } pre, code { font-family: monospace; - font-size: 12pt; + font-size: 13pt; } dl { @@ -298,14 +298,22 @@ msg.onBody = function (chunk) { puts("part of the body: " + chunk); } - A piece of the body is given as the single argument. The transfer-encoding + A chunk of the body is given as the single argument. The transfer-encoding has been removed. +

The body chunk is either a String in the case of utf8 encoding or an + array of numbers in the case of raw encoding.

msg.onBodyComplete
Callback. Made exactly once for each message. No arguments. After onBodyComplete is executed onBody will no longer be called.
+
msg.setBodyEncoding(encoding)
+
+ Set the encoding for the request body. Either "utf8" or + "raw". Defaults to raw. + TODO +
msg.sendHeader(status_code, headers)
Sends a response header to the request. The status code is a 3-digit @@ -343,5 +351,58 @@ msg.sendHeader( 200

Modules

+

Node has simple module loading. Here is an example of loading a module: +

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

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

The file mjsunit.js must be in the same directory for this +to work. The include() function will take all the exported +objects from the file and put them into the global namespace. Because file +loading does not happen instantaniously, 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 + +

+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);
+  }
+};
+
+

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 guarenteed to exist after the +onLoad() callback is made. For example: +

+var mjsunit = require("mjsunit");
+
+function onLoad () {
+  mjsunit.assertEquals(1, 2);
+}
+
+ + diff --git a/test/test_http.js b/test/test_http.js new file mode 100644 index 0000000000..9445a13b08 --- /dev/null +++ b/test/test_http.js @@ -0,0 +1,8 @@ +puts(JSON.stringify({hello: "world"})); +new node.http.Server(function (msg) { + setTimeout(function () { + msg.sendHeader(200, [["Content-Type", "text/plain"]]); + msg.sendBody("Hello World"); + msg.finish(); + }, 2000); +}).listen(8000, "localhost");