Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
update node.html
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 14, 2009
1 parent 028c278 commit 28f86c7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
69 changes: 65 additions & 4 deletions node.html
Expand Up @@ -43,21 +43,21 @@

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 {
Expand Down Expand Up @@ -298,14 +298,22 @@ <h4>HTTP Request Message (<code>node.http.Message</code>)</h4>
puts("part of the body: " + chunk);
}
</pre>
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.
<p>The body chunk is either a String in the case of utf8 encoding or an
array of numbers in the case of raw encoding.

<dt><code>msg.onBodyComplete</code></dt>
<dd>Callback. Made exactly once for each message. No arguments. After
<code>onBodyComplete</code> is executed <code>onBody</code> will no longer be called.
</dd>

<dt><code>msg.setBodyEncoding(encoding)</code></dt>
<dd>
Set the encoding for the request body. Either <code>"utf8"</code> or
<code>"raw"</code>. Defaults to raw.
<big>TODO</big>

<dt><code>msg.sendHeader(status_code, headers)</code></dt>
<dd>
Sends a response header to the request. The status code is a 3-digit
Expand Down Expand Up @@ -343,5 +351,58 @@ <h4>HTTP Request Message (<code>node.http.Message</code>)</h4>

<h3 id="modules">Modules</h3>

<p>Node has simple module loading. Here is an example of loading a module:
<pre>
include("mjsunit");

function onLoad () {
assertEquals(1, 2);
}
</pre>
<p>Here the module <code>mjsunit</code> has provided the function
<code>assertEquals()</code>.

<p> The file <code>mjsunit.js</code> must be in the same directory for this
to work. The <code>include()</code> 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 <code>onLoad()</code> is provided to notify the
user when all the exported functions are completely loaded.

<p> To export an object, add to the special object <code>exports</code>.
Let's look at how <code>mjsunit.js</code> does this

<pre>
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);
}
};
</pre>
<p> The functions <code>fail</code> and <code>deepEquals</code> are not
exported and remain private to the module.

<p> In addition to <code>include()</code> a module can use
<code>require()</code>. 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
<code>onLoad()</code> callback is made. For example:
<pre>
var mjsunit = require("mjsunit");

function onLoad () {
mjsunit.assertEquals(1, 2);
}
</pre>


</body>
</html>
8 changes: 8 additions & 0 deletions 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");

0 comments on commit 28f86c7

Please sign in to comment.