1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
4 <style type="text/css">
10 <script type="text/javascript" src="sh_main.js"></script>
11 <script type="text/javascript" src="sh_javascript.min.js"></script>
12 <link type="text/css" rel="stylesheet" href="pipe.css" />
13 <link type="text/css" rel="stylesheet" href="sh_vim-dark.css" />
14 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
15 <title>node.js</title>
17 <body onload="sh_highlightDocument();">
20 <li><a href="#download">Download</a></li>
21 <li><a href="#build">Build</a></li>
22 <li><a href="#about">About</a></li>
23 <li><a href="#demo">Demo</a></li>
24 <li><a href="#community">Community</a></li>
25 <li><a href="#contribute">Contribute</a></li>
26 <li><a href="#benchmarks">Benchmarks</a></li>
27 <li><a href="api.html">Documentation</a></li>
32 <!-- <h1><a href="http://nodejs.org/">Node</a></h1> -->
33 <img id="logo" src="logo.png" alt="node.js"/>
37 <a href="http://code.google.com/p/v8/">V8 javascript</a>.
41 An example of a web server written with Node which responds with
42 "Hello World" after waiting two seconds:
46 var sys = require('sys'),
47 http = require('http');
48 http.createServer(function (req, res) {
49 setTimeout(function () {
50 res.sendHeader(200, {'Content-Type': 'text/plain'});
51 res.sendBody('Hello World');
55 sys.puts('Server running at http://127.0.0.1:8000/');</pre>
59 To run the server, put the code into a file
60 <code>example.js</code> and execute it with the <code>node</code>
65 Server running at http://127.0.0.1:8000/</pre>
68 Here is an example of a simple TCP server which listens on port 7000
69 and echos whatever you send it:
73 var tcp = require('tcp');
74 var server = tcp.createServer(function (socket) {
75 socket.setEncoding("utf8");
76 socket.addListener("connect", function () {
77 socket.write("hello\r\n");
79 socket.addListener("receive", function (data) {
82 socket.addListener("eof", function () {
83 socket.write("goodbye\r\n");
87 server.listen(7000, "localhost");</pre>
90 See the <a href="api.html">API documentation</a> for more
94 <h2 id="download">Download</h2>
97 <a href="http://github.com/ry/node/tree/master">git repo</a>
102 href="http://s3.amazonaws.com/four.livejournal/20100209/node-v0.1.28.tar.gz">node-v0.1.28.tar.gz</a>
105 <h2 id="build">Build</h2>
108 Node eventually wants to support all POSIX operating systems
109 (including Windows with MinGW) but at the moment it is only being
110 tested on <b>Linux</b>, <b>Macintosh</b>, and <b>FreeBSD</b>. The
111 build system requires Python 2.4 or better. V8, on which Node is
112 built, supports only IA-32 and ARM processors. V8 is included in the
113 Node distribution. There are no other dependencies.
116 <pre class="sh_none">
122 Then have a look at the <a href="api.html">API documentation</a>.
125 <p>To run the tests</p>
127 <pre class="sh_none">make test</pre>
129 <h2 id="about">About</h2>
132 Node's goal is to provide an easy way to build scalable network
133 programs. In the above example, the two second delay does not
134 prevent the server from handling new requests. Node tells the
135 operating system (through <code>epoll</code>, <code>kqueue</code>,
136 <code class="sh_none">/dev/poll</code>, or <code>select</code>)
137 that it should be notified when the 2 seconds are up or if a new
138 connection is made—then it goes to sleep. If someone new
139 connects, then it executes the callback, if the timeout expires,
140 it executes the inner callback. Each connection is only a small
145 This is in contrast to today's more common concurrency model where
146 OS threads are employed. Thread-based networking
147 <a href="http://www.sics.se/~joe/apachevsyaws.html">is</a>
148 <a href="http://www.kegel.com/c10k.html">relatively</a>
149 <a href="http://bulk.fefe.de/scalable-networking.pdf">inefficient</a>
150 <!-- TODO needs links -->
151 and very difficult to use.
153 Node will show much better memory efficiency under high-loads
154 <!-- TODO benchmark -->
155 than systems which allocate 2mb thread stacks for each connection.
157 Furthermore, users of Node are free from worries of dead-locking
158 the process—there are no locks. Almost no function in Node
159 directly performs I/O, so the process never blocks. Because
160 nothing blocks, less-than-expert programmers are able to develop
165 Node is similar in design to and influenced by systems like Ruby's <a
166 href="http://rubyeventmachine.com/">Event Machine</a> or Python's <a
167 href="http://twistedmatrix.com/">Twisted</a>. Node takes the event
168 model a bit further—it presents the event loop as a language
169 construct instead of as a library. In other systems there is always
170 a blocking call to start the event-loop. Typically one defines
171 behavior through callbacks at the beginning of a script and at the
172 end starts a server through a blocking call like
173 <code>EventMachine::run()</code>. In Node there is no such
174 start-the-event-loop call. Node simply enters the event loop after
175 executing the input script. Node exits the event loop when there are
176 no more callbacks to perform. This behavior is like browser
177 javascript—the event loop is hidden from the user.
181 HTTP is a first class protocol in Node. Node's HTTP library has
182 grown out of the author's experiences developing and working with
183 web servers. For example, streaming data through most web frameworks
184 is impossible. Node attempts to correct these problems in its HTTP
185 <a href="http://github.com/ry/http-parser/tree/master">parser</a>
186 and API. Coupled with Node's purely evented infrastructure, it makes
187 a good foundation for web libraries or frameworks.
192 But what about multiple-processor concurrency? Threads are
193 necessary to scale programs to multi-core computers.
195 Processes are necessary to scale to multi-core computers, not
196 memory-sharing threads. The fundamentals of scalable systems are
197 fast networking and non-blocking design—the rest is message
198 passing. In future versions, Node will be able to fork new
199 processes (using the <a
200 href="http://www.whatwg.org/specs/web-workers/current-work/"> Web
201 Workers API </a>), but this is something that fits well into the
206 See also: <a href="http://s3.amazonaws.com/four.livejournal/20091117/jsconf.pdf">slides</a> from jsconf.
210 <h2 id="demo">Demo</h2>
212 A chat room demo is running at <a
213 href="http://chat.nodejs.org">chat.nodejs.org</a>. The
214 source code for the chat room is at <a
215 href="http://github.com/ry/node_chat/tree/master">http://github.com/ry/node_chat</a>.
216 The chat room is not stable and might occasionally be down.
219 <h2 id="community">Community</h2>
221 For help and discussion subscribe to the mailing list at
222 <a href="http://groups.google.com/group/nodejs">http://groups.google.com/group/nodejs</a>
223 or send an email to <a href="mailto:nodejs+subscribe@googlegroups.com">nodejs+subscribe@googlegroups.com</a>.
227 For real-time discussion, check irc.freenode.net #node.js.
231 <a href="http://wiki.github.com/ry/node">Projects/libraries which are using/for Node.js</a>
233 <h2 id="contribute">Contribute</h2>
235 Patches are always welcome. The process is simple:
238 <pre class="sh_none">
239 git clone git://github.com/ry/node.git
242 git commit -m "Good description of what your patch does"
243 git format-patch HEAD^
247 The best way for your patch to get noticed is to submit it to the
248 <a href="http://groups.google.com/group/nodejs">mailing list</a> in form
249 of a <a href="http://gist.github.com/">gists</a> or file attachement.
253 Feature patches should usually be discussed before putting in the work.
256 <h2 id="benchmarks">Benchmarks</h2>
258 2009.09.06 <a href="http://four.livejournal.com/1019177.html">narwhal, node, v8cgi, thin/eventmachine</a>
261 <script type="text/javascript">
262 var gaJsHost = (("https:" == document.location.protocol) ?
263 "https://ssl." : "http://www.");
264 document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
266 <script type="text/javascript">
268 var pageTracker = _gat._getTracker("UA-10874194-2");
269 pageTracker._trackPageview();
270 } catch(err) {}</script>