diff --git a/node.html b/node.html index a06fbe9380..280ce04cc8 100644 --- a/node.html +++ b/node.html @@ -1,99 +1,115 @@ - -
Node is a purely evented I/O framework for V8 javascript. It enables easy development of - highly concurrent, efficent network programs. For example, this is a - simple web server which responds with "Hello World" after waiting two - seconds: +
Node is a purely evented I/O framework for V8 javascript. For example, this is a +simple web server which responds with "Hello World" after waiting two +seconds:
-node.http.server(function (msg) { +new node.http.Server(function (msg) { setTimeout(function () { msg.sendHeader(200, [["Content-Type", "text/plain"]]); msg.sendBody("Hello World"); @@ -102,60 +118,67 @@ node.http.server(function (msg) { }).listen(8000, "localhost");-
While one request is waiting the server will continue to accept and - serve other requests. This little server can handle hundreds of - concurrent requests while using little CPU or memory—see benchmarks. The example demonstrates - efficency that is needed for handling long held "comet" requests. +
This script can handle hundreds of concurrent requests while using +little CPU or memory—see benchmarks. +Check out the documentation for more examples. -
Node is free to download, use, and build upon.
+Node is free to download, use, and build upon.
-There are many methods to write internet servers but they can + fundamentally be divided into two camps: evented and threaded; non-blocking + and blocking. A blocking server accepts a connection and launches a new + thread to handle the connection. Because the concurrency is handled by + the thread scheduler, a blocking server can make function calls which + preform full network requests. - Threads are a bad model. +
var response = db.execute("SELECT * FROM table"); +// do something-
- I/O latency -
- l1 cache ~ 3 - l2 cache ~ 14 - ram ~ 250 - disk ~ 41000000 - network ~ 240000000 -+
An evented server manages its concurrency itself. All connections + are handled in a single thread and callbacks are executed on certain + events: "socket 23 is has data to read", "socket 65's write buffer is + empty". An evented server executes small bits of code but never + blocks the process. In the evented world callbacks are used + instead of functions +
db.execute("SELECT * FROM table", function (response) { +// do something +});+
+l1 cache ~ 3 +l2 cache ~ 14 + ram ~ 250 + disk ~ 41000000 + network ~ 240000000 ++
Javascript is without I/O. In the browser the DOM provides I/O, but non-browser javascript interpreters have only non-standardized functions to allow them print to console or access the network. - -
Javascript is without the concept of threads. - +
Contemporary computer infrastructure has two irreplaceable languages: C and Javascript. C is the language of operating systems. POSIX, the universal operating system API, is defined in C. So while you @@ -166,59 +189,159 @@ node.http.server(function (msg) { end browsers must be interfaced with in Javascript. Portable low-level systems tend to be written in C and portable web-level systems are written in Javascript. +
There are many methods to write internet servers but they can - fundamentally be divided into two camps: evented and threaded; non-blocking - and blocking. A blocking server accepts a connection and launches a new - thread to handle the connection. Because the concurrency is handled by - the thread scheduler, a blocking server can make function calls which - preform full network requests. +
TODO -
var response = db.execute("SELECT * FROM table"); -// do something+
An evented server manages its concurrency itself. All connections - are handled in a single thread and callbacks are executed on certain - events: "socket 23 is has data to read", "socket 65's write buffer is - empty". An evented server executes small bits of code but never - blocks the process. In the evented world callbacks are used - instead of functions +
TODO -
db.execute("SELECT * FROM table", function (response) { - // do something -});+
configure +make +make install-
The node executable should be given an argument pointing to a +javascript file. -
configure -make -make install+
The timer API is the same as in the browser. The functions
+setTimeout, setInterval, clearTimeout, and clearInterval
+
+
node.http
) Node provides a fast web server and client interface. The interface is
+rather low-level. Node, for example, will not parse
+application/x-www-form-urlencoded
message bodies—that is
+for higher level code to manage. The interface does abstract the
+Transfer-Encoding (i.e. chuncked or identity), Message boundarys, and
+Keep-Alive connections.
-
The node executable should be given an argument pointing to a - javascript file. +
node.http.Server
)new Server(request_handler, options)
Creates a new web server. The options
argument accepts
+ the same values as the options argument for
+ node.tcp.Server
does. The options argument is optional.
-
The request_handler
is a function which is called on
+ each request with a Message
object argument.
+
server.listen(port, hostname)
+ Begin accepting connections on the specified port and hostname. If the + hostname is omitted, the server will accept connections directed to any + address. +
server.close()
+ Stops the server. Requests currently in progress will not be + interrupted. +
node.http.Message
) This object is only created internally—not by the user. It is passed
+as an argument to the request_handler
function in a web server.
+
msg.method
+ "GET"
,
+ "DELETE"
.msg.uri
+ "/index.html?hello=world"
.msg.headers
+ +[ ["Content-Length", "123"] +, ["Content-Type", "text/plain"] +, ["Connection", "keep-alive"] +, ["Accept", "*/*"] +] ++ +
msg.http_version
"1.1"
,
+ "1.0"
+
+ msg.connection
node.tcp.Connection
object. Read
+ only. Note that multiple messages can be sent on a single connection.
+ msg.onBody
+msg.onBody = function (chunk) { + puts("part of the body: " + chunk); +} ++ A piece of the body is given as the single argument. The transfer-encoding + has been removed. + +
msg.onBodyComplete
onBodyComplete
is executed onBody
will no longer be called.
+ msg.sendHeader(status_code, headers)
404
. The second argument,
+ headers
, should be an array of 2-element arrays,
+ representing the response headers.
+
+ Example: +
+var body = "hello world"; +msg.sendHeader( 200 + , [ ["Content-Length", body.length] + , ["Content-Type", "text/plain"] + ] + ); ++ This method must only be called once on a message and it must be called + before
msg.finish()
is called.
+ The timer API is the same as in the browser. The functions
- setTimeout, setInterval, cancleTimeout, and cancleInterval
+
msg.sendBody(chunk)
sendHeader
was called. It
+ sends a chunk of the response body. This method may be called multiple
+ times to provide successive parts of the body.
+ msg.finish()
msg.finish()
, MUST be called on each response.
-