0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/doc/index.html

232 lines
8.6 KiB
HTML
Raw Normal View History

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
ul {
padding: 0;
margin: 0;
}
2009-08-26 22:03:19 +02:00
</style>
<script type="text/javascript" src="sh_main.js"></script>
<script type="text/javascript" src="sh_javascript.min.js"></script>
<link type="text/css" rel="stylesheet" href="pipe.css" />
<link type="text/css" rel="stylesheet" href="sh_vim-dark.css" />
2011-03-17 21:32:27 +01:00
<link rel="alternate"
type="application/rss+xml"
2011-03-18 23:49:26 +01:00
title="node blog"
href="http://feeds.feedburner.com/nodejs/123123123"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>node.js</title>
</head>
2010-11-18 02:51:07 +01:00
<body>
<div id="toc">
<ol>
<li><a href="#download">Download</a></li>
2011-02-24 01:19:13 +01:00
<li><a href="https://github.com/joyent/node/raw/master/ChangeLog">ChangeLog</a></li>
<li><a href="#about">About</a></li>
2011-04-02 03:00:43 +02:00
<li><a href="http://nodejs.org/docs/v0.4.5/api">v0.4.5 docs</a></li>
2011-01-28 04:47:17 +01:00
<br/>
2011-02-24 01:19:13 +01:00
<li><B><a href="https://github.com/joyent/node/wiki">Wiki</a></B></li>
2011-03-17 21:49:32 +01:00
<li><B><a href="http://blog.nodejs.org/">Blog</a></B></li>
2011-03-20 04:28:31 +01:00
<br/>
2011-03-24 03:23:35 +01:00
<li><B><a href="http://jobs.nodejs.org/">Jobs</a></B></li>
<br/>
2011-03-20 04:28:31 +01:00
<li><a href="http://chat.nodejs.org/">Demo</a></li>
</ol>
</div>
<div id="content">
2010-02-12 09:08:26 +01:00
<!-- <h1><a href="http://nodejs.org/">Node</a></h1> -->
2009-11-02 12:28:08 +01:00
<img id="logo" src="logo.png" alt="node.js"/>
<p id="introduction">
2009-09-03 17:21:08 +02:00
Evented I/O for
2010-06-09 08:25:55 +02:00
<a href="http://code.google.com/p/v8/">V8 JavaScript</a>.
</p>
<p>
2010-06-09 08:25:55 +02:00
An example of a web server written in Node which responds with
"Hello World" for every request.
</p>
2009-08-26 22:03:19 +02:00
<pre>
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
2010-06-09 08:25:55 +02:00
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
</pre>
2009-08-26 22:03:19 +02:00
<p>
To run the server, put the code into a file
<code>example.js</code> and execute it with the <code>node</code>
2010-06-09 08:25:55 +02:00
program:
</p>
<pre class="sh_none">
% node example.js
2010-06-04 03:44:05 +02:00
Server running at http://127.0.0.1:8124/</pre>
2009-08-26 22:03:19 +02:00
<p>
2010-06-04 03:44:05 +02:00
Here is an example of a simple TCP server which listens on port 8124
2010-06-09 08:25:55 +02:00
and echoes whatever you send it:
</p>
<pre>
2010-06-09 08:25:55 +02:00
var net = require('net');
2011-02-23 21:56:50 +01:00
var server = net.createServer(function (socket) {
2010-07-03 10:54:35 +02:00
socket.write("Echo server\r\n");
2011-02-23 21:56:50 +01:00
socket.pipe(socket);
})
server.listen(8124, "127.0.0.1");
2010-06-09 08:25:55 +02:00
</pre>
<p>
2010-11-17 23:51:06 +01:00
See the <a href="/docs">API documentation</a> for more
examples.
</p>
2011-01-28 04:47:17 +01:00
<p>
2011-02-24 01:19:13 +01:00
Go to <a href="https://github.com/joyent/node/wiki">the Wiki</a> for lots more information.
2011-01-28 04:47:17 +01:00
</p>
2011-03-21 02:07:29 +01:00
<h2 id="video">Introduction</h2>
<iframe title="YouTube video player" width="640" height="390"
src="http://www.youtube.com/embed/jo_B4LTHi3I" frameborder="0"
allowfullscreen></iframe>
<h2 id="download">Download</h2>
<p>
2011-02-24 01:19:13 +01:00
<a href="http://github.com/joyent/node/tree/master">git repo</a>
</p>
2010-10-24 00:46:58 +02:00
<p>
2011-04-02 03:00:43 +02:00
2011.04.01
<a href="http://nodejs.org/dist/node-v0.4.5.tar.gz">node-v0.4.5.tar.gz</a>
(<a href="http://nodejs.org/docs/v0.4.5/api/index.html">Documentation</a>)
</p>
2010-04-24 01:18:40 +02:00
<p>Historical: <a href="http://nodejs.org/dist">versions</a>, <a href="http://nodejs.org/docs">docs</a></p>
2010-04-21 22:16:46 +02:00
<p>
2011-01-28 04:47:17 +01:00
For build instructions see
2011-02-24 01:19:13 +01:00
<a href="https://github.com/joyent/node/wiki/Installation">https://github.com/joyent/node/wiki/Installation</a>
</p>
<h2 id="about">About</h2>
<p>
Node's goal is to provide an easy way to build scalable network
programs. In the "hello world" web server example above, many
client connections can be handled concurrently. Node tells the
operating system (through <code>epoll</code>, <code>kqueue</code>,
2009-08-26 22:03:19 +02:00
<code class="sh_none">/dev/poll</code>, or <code>select</code>)
that it should be notified when a new connection is made, and
then it goes to sleep. If someone new connects, then it executes
the callback. Each connection is only a small heap allocation.
</p>
2009-08-26 22:03:19 +02:00
<p>
2009-09-03 17:21:08 +02:00
This is in contrast to today's more common concurrency model where
2010-06-09 08:25:55 +02:00
OS threads are employed. Thread-based networking is relatively
inefficient and very difficult to use. See:
<a href="http://www.kegel.com/c10k.html">this</a> and
2010-06-09 08:25:55 +02:00
<a href="http://bulk.fefe.de/scalable-networking.pdf">this.</a>
2009-08-26 22:03:19 +02:00
Node will show much better memory efficiency under high-loads
<!-- TODO benchmark -->
than systems which allocate 2mb thread stacks for each connection.
Furthermore, users of Node are free from worries of dead-locking
the process&mdash;there are no locks. Almost no function in Node
directly performs I/O, so the process never blocks. Because
nothing blocks, less-than-expert programmers are able to develop
fast systems.
</p>
<p>
2009-09-14 22:44:25 +02:00
Node is similar in design to and influenced by systems like Ruby's <a
href="http://rubyeventmachine.com/">Event Machine</a> or Python's <a
href="http://twistedmatrix.com/">Twisted</a>. Node takes the event
2009-09-03 17:21:08 +02:00
model a bit further&mdash;it presents the event loop as a language
construct instead of as a library. In other systems there is always
a blocking call to start the event-loop. Typically one defines
behavior through callbacks at the beginning of a script and at the
2009-09-03 17:21:08 +02:00
end starts a server through a blocking call like
<code>EventMachine::run()</code>. In Node there is no such
start-the-event-loop call. Node simply enters the event loop after
executing the input script. Node exits the event loop when there are
no more callbacks to perform. This behavior is like browser
javascript&mdash;the event loop is hidden from the user.
</p>
2009-08-26 22:03:19 +02:00
<p>
2009-09-03 17:21:08 +02:00
HTTP is a first class protocol in Node. Node's HTTP library has
grown out of the author's experiences developing and working with
web servers. For example, streaming data through most web frameworks
is impossible. Node attempts to correct these problems in its HTTP
<a href="http://github.com/ry/http-parser/tree/master">parser</a>
and API. Coupled with Node's purely evented infrastructure, it makes
a good foundation for web libraries or frameworks.
</p>
2009-08-26 22:03:19 +02:00
<p>
<i>
2010-06-18 00:38:02 +02:00
But what about multiple-processor concurrency? Aren't threads
necessary to scale programs to multi-core computers?
</i>
2009-09-03 17:21:08 +02:00
Processes are necessary to scale to multi-core computers, not
memory-sharing threads. The fundamentals of scalable systems are
fast networking and non-blocking design&mdash;the rest is message
passing. In future versions, Node will be able to fork new
processes (using the <a
href="http://www.whatwg.org/specs/web-workers/current-work/"> Web
Workers API </a>) which fits well into the current design.
</p>
2009-11-17 14:22:23 +01:00
<p>
See also:
<ul>
<li><a href="http://s3.amazonaws.com/four.livejournal/20091117/jsconf.pdf">slides</a> from JSConf 2009</li>
<li><a href="http://nodejs.org/jsconf2010.pdf">slides</a> from JSConf 2010</li>
<li><a href="http://www.yuiblog.com/blog/2010/05/20/video-dahl/">video</a> from a talk at Yahoo in May 2010</li>
</ul>
2009-11-17 14:22:23 +01:00
</p>
2009-08-26 22:03:19 +02:00
</div>
2010-11-10 00:43:52 +01:00
<div style="float: right;">
<a
href='https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String'
title='JS String match'><img
src='http://static.jsconf.us/promotejsh.gif' height='150' width='180'
alt='JS String match'/></a>
</div>
2011-01-06 20:05:19 +01:00
<a href="http://no.de/"><img src="sponsored.png" height="58" width="120"/></a>
2010-11-10 00:43:52 +01:00
<div style="clear: both; font-size: 8pt">
2010-11-10 00:43:52 +01:00
Copyright 2010 Joyent, Inc
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ?
"https://ssl." : "http://www.");
2009-11-04 00:18:05 +01:00
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10874194-2");
pageTracker._trackPageview();
} catch(err) {}</script>
2010-11-18 02:51:07 +01:00
<script type="text/javascript">highlight(undefined, undefined, 'pre');</script>
</body>
2009-05-13 17:46:00 +02:00
</html>