2009-05-13 17:46:00 +02:00
|
|
|
|
|
|
|
<html>
|
2009-05-14 15:55:28 +02:00
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
background: #22252a;
|
|
|
|
color: #eee;
|
|
|
|
font-size: 16pt;
|
|
|
|
line-height: 150%;
|
|
|
|
font-family: times, Times New Roman, times-roman, georgia, serif;
|
|
|
|
}
|
|
|
|
#content {
|
|
|
|
max-width: 30em;
|
|
|
|
margin: 0 0 5em 10em;
|
|
|
|
}
|
|
|
|
#toc {
|
|
|
|
position: fixed;
|
|
|
|
top: 2em;
|
|
|
|
left: 0;
|
|
|
|
width: 10em;
|
|
|
|
}
|
|
|
|
#toc ol {
|
|
|
|
list-style: none;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
padding-left: 1em;
|
|
|
|
}
|
|
|
|
#toc ol li {
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
#toc a { color: #777; }
|
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
h1, h2, h3, h4 {
|
2009-05-18 23:21:11 +02:00
|
|
|
color: #B0C4DE;
|
2009-05-17 15:54:06 +02:00
|
|
|
margin: 2em 0;
|
2009-05-14 15:55:28 +02:00
|
|
|
}
|
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
h1 a { color: inherit; }
|
2009-05-14 23:47:21 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
|
|
|
|
pre, code {
|
|
|
|
font-family: monospace;
|
2009-05-18 23:21:11 +02:00
|
|
|
font-size: 14pt;
|
|
|
|
color: #dcd;
|
2009-05-14 20:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pre {
|
2009-05-17 15:54:06 +02:00
|
|
|
padding-left: 1em;
|
|
|
|
border-left: 1px solid #444;
|
2009-05-14 15:55:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dl {
|
|
|
|
}
|
|
|
|
|
|
|
|
dt {
|
|
|
|
}
|
|
|
|
|
|
|
|
dd {
|
|
|
|
margin: 1em 0;
|
|
|
|
margin-left: 1em;
|
|
|
|
}
|
|
|
|
|
|
|
|
a { color: #cd5; text-decoration: none; }
|
|
|
|
a:hover { text-decoration: underline; }
|
|
|
|
|
2009-05-14 20:34:14 +02:00
|
|
|
.highlight {
|
|
|
|
background: #733;
|
|
|
|
padding: 0.2em 0;
|
|
|
|
}
|
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
</style>
|
2009-05-18 19:33:05 +02:00
|
|
|
<script type="text/javascript" src="sh_main.js"></script>
|
2009-05-17 15:54:06 +02:00
|
|
|
<script type="text/javascript" src="sh_javascript.min.js"></script>
|
|
|
|
<link type="text/css" rel="stylesheet" href="sh_vim-dark.css">
|
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<title>node.js</title>
|
2009-05-17 15:54:06 +02:00
|
|
|
<body onload="sh_highlightDocument();">
|
2009-05-14 15:55:28 +02:00
|
|
|
<div id="toc">
|
|
|
|
<ol>
|
|
|
|
<li><a href="#motivation">Motivation</a></li>
|
|
|
|
<li><a href="#benchmarks">Benchmarks</a></li>
|
|
|
|
<li><a href="#download">Download</a></li>
|
|
|
|
<li><a href="#install">Build</a></li>
|
|
|
|
<li><a href="#api">API</a>
|
2009-05-13 17:46:00 +02:00
|
|
|
<ol>
|
2009-05-14 15:55:28 +02:00
|
|
|
<li><a href="#timers">Timers</a>
|
2009-05-18 23:21:11 +02:00
|
|
|
<li><a href="#files">File System I/O</a>
|
|
|
|
<li><a href="#tcp">TCP</a>
|
|
|
|
<li><a href="#http">HTTP</a>
|
2009-05-13 17:46:00 +02:00
|
|
|
<ol>
|
2009-05-14 15:55:28 +02:00
|
|
|
<li><a href="#http_server">Server</a>
|
2009-05-18 19:33:05 +02:00
|
|
|
<li><a href="#http_server_request">ServerRequest</a>
|
|
|
|
<li><a href="#http_server_response">ServerResponse</a>
|
2009-05-14 15:55:28 +02:00
|
|
|
<li><a href="#http_client">Client</a>
|
2009-05-13 17:46:00 +02:00
|
|
|
</ol>
|
2009-05-14 15:55:28 +02:00
|
|
|
<li><a href="#modules">Modules</a>
|
2009-05-13 17:46:00 +02:00
|
|
|
</ol>
|
2009-05-14 15:55:28 +02:00
|
|
|
</li>
|
|
|
|
</ol>
|
|
|
|
</div>
|
|
|
|
<div id="content">
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<h1><a href="http://tinyclouds.org/node">Node</a></h1>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<p id="introduction"> Purely asynchronous I/O for <a
|
2009-05-14 20:34:14 +02:00
|
|
|
href="http://code.google.com/p/v8/">V8 javascript</a>.
|
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<p>This is an example of a web server written with Node which responds with
|
|
|
|
"Hello World" after waiting two seconds:
|
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<pre class="sh_javascript">new node.http.Server(function (req, res) {
|
2009-05-13 17:46:00 +02:00
|
|
|
setTimeout(function () {
|
2009-05-17 15:54:06 +02:00
|
|
|
res.sendHeader(200, [["Content-Type", "text/plain"]]);
|
|
|
|
res.sendBody("Hello World");
|
|
|
|
res.finish();
|
2009-05-13 17:46:00 +02:00
|
|
|
}, 2000);
|
2009-05-17 15:54:06 +02:00
|
|
|
}).listen(8000);
|
2009-05-18 19:33:05 +02:00
|
|
|
puts("Server running at http://127.0.0.1:8000/");</pre>
|
|
|
|
|
|
|
|
<p> Execution does not block on <code
|
|
|
|
class="sh_javascript">setTimeout()</code>
|
|
|
|
nor
|
|
|
|
<code
|
|
|
|
class="sh_javascript">listen(8000)</code>.
|
|
|
|
In fact, not a single function in Node blocks execution.
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-18 23:21:11 +02:00
|
|
|
<p>Programming in Node is different. Instead of commanding actions, Node
|
|
|
|
scripts defining behaviors. The entire program is centered around I/O
|
|
|
|
events.
|
2009-05-16 13:53:18 +02:00
|
|
|
|
|
|
|
<p> Check out <a href="#api">the API documentation</a> for more examples.
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<p> Node is free to <a href="#download">download</a>, <a
|
|
|
|
href="#api">use</a>, and <a href="#modules">build upon</a>.</p>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 23:47:21 +02:00
|
|
|
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-18 23:21:11 +02:00
|
|
|
<h2 id="motivation">Motivation</h2>
|
2009-05-14 23:47:21 +02:00
|
|
|
|
2009-05-18 23:21:11 +02:00
|
|
|
<p>
|
|
|
|
I/O is hard and almost no one gets it right.
|
|
|
|
This is an attempt to make you to do it right by taking away all those
|
|
|
|
sharp, dangerous tools called <i>threads</i>.
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Node is forced evented programming—so by default you are doing the
|
|
|
|
right thing.
|
|
|
|
Well, actually, <a
|
|
|
|
href="http://weblogs.mozillazine.org/roadmap/archives/2007/02/threads_suck.html">Javascript
|
|
|
|
itself is forced evented programming</a>. Node brings Javascript, in the way
|
|
|
|
it was meant to be, out of the browser.
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<a
|
|
|
|
href="http://duartes.org/gustavo/blog/post/what-your-computer-does-while-you-wait">There
|
|
|
|
is a major difference in the latency between memory and disk I/O.</a> It looks
|
|
|
|
approximately like this:
|
2009-05-14 23:47:21 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<pre>
|
2009-05-18 23:21:11 +02:00
|
|
|
l1 cache ~ 3 (CPU cycles)
|
2009-05-14 15:55:28 +02:00
|
|
|
l2 cache ~ 14
|
|
|
|
ram ~ 250
|
|
|
|
disk ~ 41000000
|
|
|
|
network ~ 240000000
|
|
|
|
</pre>
|
2009-05-14 23:47:21 +02:00
|
|
|
|
2009-05-18 23:21:11 +02:00
|
|
|
<p>Disk and network I/O need to be treated differently than simple memory
|
|
|
|
operations. But POSIX obscures the latency with system calls like
|
|
|
|
|
|
|
|
<pre>close(file_descriptor);</pre>
|
|
|
|
|
|
|
|
<p> For a TCP file descriptor, this is a round trip message to a remote
|
|
|
|
computer that can cost billions of CPU cycles. For a hard drive file descriptor,
|
|
|
|
<code>close()</code> could mean a couple million cycles of disk spinning.
|
|
|
|
The man pages don't even mention that a call might be preforming very
|
|
|
|
long I/O operations. This ambiguity in POSIX is propagated into higher APIs.
|
|
|
|
|
|
|
|
<p> In the Node API all I/O happens on the event loop and thus requires a
|
|
|
|
callback of some sort. Calls to access foreign database do not look like
|
|
|
|
simple side-effect free functions. The programmer does not need advanced
|
|
|
|
knowledge of POSIX to know that I/O is being performed because it looks
|
|
|
|
differently.
|
|
|
|
|
|
|
|
<p> Some find event programming cumbersome.
|
|
|
|
I find threaded programming
|
|
|
|
cumbersome—it's not a good abstraction of what is really happening.
|
|
|
|
Because of this bad abstraction it's confusing and difficult to get right.
|
|
|
|
Threaded programs only look good in the simpliest and most trivial
|
|
|
|
situations—in real-life applications events lead to better
|
|
|
|
architecture.
|
2009-05-14 23:47:21 +02:00
|
|
|
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<h2 id="benchmarks">Benchmarks</h2>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<p> TODO
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<h2 id="download">Download</h2>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<p> TODO
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<h2 id="build">Build</h2>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<pre>./configure
|
2009-05-14 15:55:28 +02:00
|
|
|
make
|
|
|
|
make install</pre>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<h2 id="api">Application Programming Interface</h2>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<p>Conventions: Callbacks are object members which are prefixed with
|
|
|
|
<code class="sh_javascript">on</code>. All methods and members are camel cased. Constructors
|
|
|
|
always have a capital first letter.
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<h3 id="timers">Timers</h3>
|
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<p>Timers allow one to schedule execution of a function for a later time.
|
|
|
|
|
|
|
|
<p>Timers in Node work as they do in the browser:
|
|
|
|
<code class="sh_javascript">setTimeout</code>,
|
|
|
|
<code class="sh_javascript">setInterval</code>,
|
|
|
|
<code class="sh_javascript">clearTimeout</code>,
|
|
|
|
<code class="sh_javascript">clearInterval</code>.
|
|
|
|
See <a
|
|
|
|
href="https://developer.mozilla.org/en/DOM/window.setTimeout">Mozilla's
|
|
|
|
documentation</a> for more information.
|
2009-05-14 15:55:28 +02:00
|
|
|
|
|
|
|
<h3 id="files">File System</h3>
|
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<h3 id="tcp"><code>node.tcp</code></h3>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<h3 id="http"><code>node.http</code></h3>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<p> Node provides a web server and client interface. The interface is rather
|
|
|
|
low-level but complete. For example, it does not parse
|
|
|
|
<code class="sh_javascript">application/x-www-form-urlencoded</code> message bodies. The interface
|
|
|
|
does abstract the Transfer-Encoding (i.e. chuncked or identity), Message
|
2009-05-18 19:33:05 +02:00
|
|
|
boundaries, and Keep-Alive connections.
|
|
|
|
|
|
|
|
<h4 id="http_server"><code class="sh_javascript">node.http.Server</code></h4>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<dl>
|
2009-05-18 19:33:05 +02:00
|
|
|
<dt><code class="sh_javascript">var server = new node.http.Server(request_handler, options);</code></dt>
|
2009-05-14 15:55:28 +02:00
|
|
|
<dd>
|
2009-05-18 19:33:05 +02:00
|
|
|
<p>Creates a new web server.
|
|
|
|
|
|
|
|
<p>
|
|
|
|
The <code>options</code> argument is optional.
|
|
|
|
The <code
|
|
|
|
class="sh_javascript">options</code> argument accepts the same values
|
|
|
|
as the options argument for <code
|
|
|
|
class="sh_javascript">node.tcp.Server</code> does.
|
|
|
|
|
|
|
|
<p>The <code class="sh_javascript">request_handler</code> is a
|
|
|
|
callback which is made on each request with a
|
|
|
|
<code>ServerRequest</code> and
|
|
|
|
<code>ServerResponse</code> arguments.
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
</dd>
|
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<dt><code class="sh_javascript">server.listen(port, hostname)</code>
|
2009-05-14 15:55:28 +02:00
|
|
|
<dd>
|
|
|
|
<p>Begin accepting connections on the specified port and hostname. If the
|
|
|
|
hostname is omitted, the server will accept connections directed to any
|
|
|
|
address.
|
|
|
|
</dd>
|
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<dt><code class="sh_javascript">server.close()</code>
|
2009-05-14 15:55:28 +02:00
|
|
|
<dd>
|
|
|
|
<p>Stops the server. Requests currently in progress will not be
|
|
|
|
interrupted.
|
|
|
|
</dd>
|
|
|
|
</dl>
|
|
|
|
|
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<h4 id="http_server_request"><code class="sh_javascript">node.http.ServerRequest</code></h4>
|
2009-05-14 15:55:28 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<p> This object is created internally by a HTTP server—not by the user.
|
|
|
|
It is passed as the first argument to the <code
|
|
|
|
class="sh_javascript">request_handler</code> callback.
|
2009-05-17 15:54:06 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<dl>
|
2009-05-18 19:33:05 +02:00
|
|
|
<dt><code class="sh_javascript">req.method</code>
|
2009-05-17 15:54:06 +02:00
|
|
|
<dd>The request method as a string. Read only. Example: <code class="sh_javascript">"GET"</code>,
|
|
|
|
<code class="sh_javascript">"DELETE"</code>.</dd>
|
2009-05-14 15:55:28 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<dt><code class="sh_javascript">req.uri</code>
|
|
|
|
<dd> URI object. Has many fields.
|
|
|
|
<dt><code>req.uri.toString()</code>
|
|
|
|
<dd> The original URI found in the status line.
|
|
|
|
<dt><code>req.uri.anchor</code>
|
|
|
|
<dt><code>req.uri.query</code>
|
|
|
|
<dt><code>req.uri.file</code>
|
|
|
|
<dt><code>req.uri.directory</code>
|
|
|
|
<dt><code>req.uri.path</code>
|
|
|
|
<dt><code>req.uri.relative</code>
|
|
|
|
<dt><code>req.uri.port</code>
|
|
|
|
<dt><code>req.uri.host</code>
|
|
|
|
<dt><code>req.uri.password</code>
|
|
|
|
<dt><code>req.uri.user</code>
|
|
|
|
<dt><code>req.uri.authority</code>
|
|
|
|
<dt><code>req.uri.protocol</code>
|
|
|
|
<dt><code>req.uri.source</code>
|
|
|
|
<dt><code>req.uri.queryKey</code>
|
|
|
|
|
|
|
|
<dt><code class="sh_javascript">req.headers</code>
|
2009-05-14 15:55:28 +02:00
|
|
|
<dd>The request headers expressed as an array of 2-element arrays. Read only.
|
|
|
|
Example:
|
2009-05-17 15:54:06 +02:00
|
|
|
<pre class="sh_javascript">
|
2009-05-14 15:55:28 +02:00
|
|
|
[ ["Content-Length", "123"]
|
|
|
|
, ["Content-Type", "text/plain"]
|
|
|
|
, ["Connection", "keep-alive"]
|
|
|
|
, ["Accept", "*/*"]
|
|
|
|
]
|
|
|
|
</pre>
|
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<dt><code class="sh_javascript">req.http_version</code></dt>
|
2009-05-17 15:54:06 +02:00
|
|
|
<dd>The HTTP protocol version as a string. Read only. Examples: <code class="sh_javascript">"1.1"</code>,
|
|
|
|
<code class="sh_javascript">"1.0"</code>
|
2009-05-14 15:55:28 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<dt><code class="sh_javascript">req.onBody</code></dt>
|
2009-05-14 15:55:28 +02:00
|
|
|
<dd>Callback. Should be set by the user to be informed of when a piece
|
|
|
|
of the message body is received. Example:
|
2009-05-17 15:54:06 +02:00
|
|
|
<pre class="sh_javascript">
|
2009-05-18 19:33:05 +02:00
|
|
|
req.onBody = function (chunk) {
|
2009-05-14 15:55:28 +02:00
|
|
|
puts("part of the body: " + chunk);
|
2009-05-18 19:33:05 +02:00
|
|
|
};
|
2009-05-14 15:55:28 +02:00
|
|
|
</pre>
|
2009-05-14 17:36:25 +02:00
|
|
|
A chunk of the body is given as the single argument. The transfer-encoding
|
2009-05-14 15:55:28 +02:00
|
|
|
has been removed.
|
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<p>The body chunk is either a String in the case of UTF-8 encoding or an
|
|
|
|
array of numbers in the case of raw encoding. The body encoding is set with
|
|
|
|
<code class="sh_javascript">req.setBodyEncoding()</code>.
|
|
|
|
|
|
|
|
<dt><code class="sh_javascript">req.onBodyComplete</code></dt>
|
2009-05-14 15:55:28 +02:00
|
|
|
<dd>Callback. Made exactly once for each message. No arguments. After
|
2009-05-17 15:54:06 +02:00
|
|
|
<code class="sh_javascript">onBodyComplete</code> is executed <code class="sh_javascript">onBody</code> will no longer be called.
|
2009-05-14 15:55:28 +02:00
|
|
|
</dd>
|
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<dt><code class="sh_javascript">req.setBodyEncoding(encoding)</code></dt>
|
2009-05-14 17:36:25 +02:00
|
|
|
<dd>
|
2009-05-17 15:54:06 +02:00
|
|
|
Set the encoding for the request body. Either <code class="sh_javascript">"utf8"</code> or
|
|
|
|
<code class="sh_javascript">"raw"</code>. Defaults to raw.
|
2009-05-14 17:36:25 +02:00
|
|
|
<big>TODO</big>
|
2009-05-18 19:33:05 +02:00
|
|
|
</dl>
|
2009-05-14 17:36:25 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<h4 id="http_server_response"><code class="sh_javascript">node.http.ServerResponse</code></h4>
|
|
|
|
|
|
|
|
<dl>
|
|
|
|
<dt><code class="sh_javascript">res.sendHeader(status_code, headers)</code></dt>
|
2009-05-14 15:55:28 +02:00
|
|
|
<dd>
|
|
|
|
Sends a response header to the request. The status code is a 3-digit
|
2009-05-17 15:54:06 +02:00
|
|
|
HTTP status code, like <code class="sh_javascript">404</code>. The second argument,
|
|
|
|
<code class="sh_javascript">headers</code>, should be an array of 2-element arrays,
|
2009-05-14 15:55:28 +02:00
|
|
|
representing the response headers.
|
|
|
|
|
|
|
|
<p>Example:
|
2009-05-17 15:54:06 +02:00
|
|
|
<pre class="sh_javascript">
|
2009-05-14 15:55:28 +02:00
|
|
|
var body = "hello world";
|
2009-05-18 19:33:05 +02:00
|
|
|
res.sendHeader(200, [ ["Content-Length", body.length]
|
|
|
|
, ["Content-Type", "text/plain"]
|
|
|
|
]);
|
2009-05-14 15:55:28 +02:00
|
|
|
</pre>
|
|
|
|
This method must only be called once on a message and it must be called
|
2009-05-18 19:33:05 +02:00
|
|
|
before <code class="sh_javascript">res.finish()</code> is called.
|
2009-05-14 15:55:28 +02:00
|
|
|
</dd>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<dt><code class="sh_javascript">res.sendBody(chunk)</code></dt>
|
2009-05-14 15:55:28 +02:00
|
|
|
<dd>
|
2009-05-17 15:54:06 +02:00
|
|
|
This method must be called after <code class="sh_javascript">sendHeader</code> was called. It
|
2009-05-14 15:55:28 +02:00
|
|
|
sends a chunk of the response body. This method may be called multiple
|
|
|
|
times to provide successive parts of the body.
|
|
|
|
</dd>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
<dt><code class="sh_javascript">res.finish()</code></dt>
|
2009-05-14 15:55:28 +02:00
|
|
|
<dd>
|
|
|
|
This method signals that all of the response headers and body has been
|
|
|
|
sent; that server should consider this message complete.
|
2009-05-18 19:33:05 +02:00
|
|
|
The method, <code class="sh_javascript">res.finish()</code>, MUST be called on each response.
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
</dl>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
<h3 id="modules">Modules</h3>
|
2009-05-13 17:46:00 +02:00
|
|
|
|
2009-05-16 13:53:18 +02:00
|
|
|
<p>Node has a simple module loading system. In Node, files and modules are
|
|
|
|
in one-to-one correspondence.
|
|
|
|
|
|
|
|
<p> As an example,
|
2009-05-17 15:54:06 +02:00
|
|
|
<code class="sh_javascript">foo.js</code> loads the module <code class="sh_javascript">mjsunit.js</code>.
|
2009-05-16 13:53:18 +02:00
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<p>The contents of <code class="sh_javascript">foo.js</code>:
|
2009-05-16 13:53:18 +02:00
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<pre class="sh_javascript">
|
2009-05-14 17:36:25 +02:00
|
|
|
include("mjsunit");
|
|
|
|
function onLoad () {
|
|
|
|
assertEquals(1, 2);
|
|
|
|
}
|
|
|
|
</pre>
|
2009-05-17 15:54:06 +02:00
|
|
|
<p>The contents of <code class="sh_javascript">mjsunit.js</code>:
|
2009-05-14 17:36:25 +02:00
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<pre class="sh_javascript">
|
2009-05-14 17:36:25 +02:00
|
|
|
function fail (expected, found, name_opt) {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
function deepEquals (a, b) {
|
|
|
|
// ...
|
|
|
|
}
|
2009-05-14 20:34:14 +02:00
|
|
|
<span class="highlight">exports</span>.assertEquals = function (expected, found, name_opt) {
|
2009-05-14 17:36:25 +02:00
|
|
|
if (!deepEquals(found, expected)) {
|
|
|
|
fail(expected, found, name_opt);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</pre>
|
2009-05-16 13:53:18 +02:00
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<p>Here the module <code class="sh_javascript">mjsunit.js</code> has exported the function
|
|
|
|
<code class="sh_javascript">assertEquals()</code>. <code class="sh_javascript">mjsunit.js</code> must be in the
|
|
|
|
same directory as <code class="sh_javascript">foo.js</code> for <code class="sh_javascript">include()</code> to find it.
|
|
|
|
The module path is relative to the file calling <code class="sh_javascript">include()</code>.
|
|
|
|
The module path does not include filename extensions like <code class="sh_javascript">.js</code>.
|
2009-05-16 13:53:18 +02:00
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<p> <code class="sh_javascript">include()</code> inserts the exported objects
|
2009-05-16 13:53:18 +02:00
|
|
|
from the specified module into the global namespace.
|
|
|
|
|
|
|
|
<p> Because file loading does not happen instantaneously, and because Node
|
2009-05-18 23:21:11 +02:00
|
|
|
has a policy of never blocking, the callback <code
|
|
|
|
class="sh_javascript">onLoad</code> can be set and will notify the user
|
|
|
|
when all the included modules are loaded. Each file/module can have an <code
|
|
|
|
class="sh_javascript">onLoad</code> callback.
|
2009-05-16 13:53:18 +02:00
|
|
|
|
|
|
|
<p> To export an object, add to the special <code
|
|
|
|
class="highlight">exports</code> object.
|
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<p> The functions <code class="sh_javascript">fail</code> and <code class="sh_javascript">deepEquals</code> are not
|
2009-05-14 17:36:25 +02:00
|
|
|
exported and remain private to the module.
|
|
|
|
|
2009-05-18 23:21:11 +02:00
|
|
|
<p> <code>require()</code> is like <code>include()</code> except does not
|
|
|
|
polute the global namespace. It returns a namespace object. The exported objects
|
2009-05-17 15:54:06 +02:00
|
|
|
can only be guaranteed to exist after the <code class="sh_javascript">onLoad()</code> callback is
|
2009-05-16 13:53:18 +02:00
|
|
|
made. For example:
|
2009-05-17 15:54:06 +02:00
|
|
|
<pre class="sh_javascript">
|
2009-05-14 17:36:25 +02:00
|
|
|
var mjsunit = require("mjsunit");
|
|
|
|
function onLoad () {
|
|
|
|
mjsunit.assertEquals(1, 2);
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
|
2009-05-17 15:54:06 +02:00
|
|
|
<p> <code class="sh_javascript">include()</code> and <code class="sh_javascript">require()</code> cannot be used after
|
|
|
|
<code class="sh_javascript">onLoad()</code> is called. So put them at the beginning of your file.
|
2009-05-14 17:36:25 +02:00
|
|
|
|
2009-05-14 15:55:28 +02:00
|
|
|
</body>
|
2009-05-13 17:46:00 +02:00
|
|
|
</html>
|