mirror of
https://github.com/nodejs/node.git
synced 2024-11-29 15:06:33 +01:00
126 lines
6.4 KiB
HTML
126 lines
6.4 KiB
HTML
|
<!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;
|
||
|
}
|
||
|
</style>
|
||
|
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js?ver=3.1.3'></script>
|
||
|
<script type="text/javascript" src="../sh_main.js"></script>
|
||
|
<script type="text/javascript" src="../sh_javascript.min.js"></script>
|
||
|
<link type="image/x-icon" rel="icon" href="../favicon.ico" />
|
||
|
<link type="image/x-icon" rel="shortcut icon" href="../favicon.ico" />
|
||
|
<link type="text/css" rel="stylesheet" href="../pipe.css" />
|
||
|
<link type="text/css" rel="stylesheet" href="../sh_vim-dark.css" />
|
||
|
<link rel="alternate"
|
||
|
type="application/rss+xml"
|
||
|
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>
|
||
|
<body>
|
||
|
<div id="intro">
|
||
|
<img id="logo" src="../logo.png" alt="node.js"/>
|
||
|
</div>
|
||
|
<div id="content" class="clearfix">
|
||
|
<div id="column1" class="interior">
|
||
|
<h2>About</h2>
|
||
|
|
||
|
<pre>
|
||
|
var http = require('http');
|
||
|
http.createServer(function (req, res) {
|
||
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||
|
res.end('Hello World\n');
|
||
|
}).listen(1337, "127.0.0.1");
|
||
|
console.log('Server running at http://127.0.0.1:1337/');</pre>
|
||
|
|
||
|
<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>, <code>/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>
|
||
|
|
||
|
<p>This is in contrast to today's more common concurrency
|
||
|
model where 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 <a
|
||
|
href="http://bulk.fefe.de/scalable-networking.pdf">this</a>.
|
||
|
Node will show much better memory efficiency under high-loads
|
||
|
than systems which allocate 2mb thread stacks for each
|
||
|
connection. Furthermore, users of Node are free from worries
|
||
|
of dead-locking the process—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>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 model a bit further—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 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—the event loop is
|
||
|
hidden from the user.</p>
|
||
|
|
||
|
<p>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="https://github.com/joyent/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>
|
||
|
|
||
|
<p>But what about multiple-processor concurrency? Aren't
|
||
|
threads necessary to scale programs to multi-core computers?
|
||
|
You can start new processes via <code><a
|
||
|
href="http://nodejs.org/docs/latest/api/child_processes.html#child_process.fork">child_process.fork()</a></code>
|
||
|
these other processes will be scheduled in parallel. For load
|
||
|
balancing incoming connections across multiple processes use
|
||
|
<a href="http://nodejs.org/docs/latest/api/cluster.html">the
|
||
|
cluster module</a>.</p>
|
||
|
|
||
|
<p>See also:</p>
|
||
|
<ul>
|
||
|
<li><a href="http://s3.amazonaws.com/four.livejournal/20091117/jsconf.pdf">Slides from JSConf 2009</a></li>
|
||
|
<li><a href="http://nodejs.org/jsconf2010.pdf">Slides from JSConf 2010</a></li>
|
||
|
<li><a href="http://www.yuiblog.com/blog/2010/05/20/video-dahl/">Video from a talk at Yahoo in May 2010</a></li>
|
||
|
</ul>
|
||
|
<p><a href="/">Go back to the home page</a></p>
|
||
|
</div>
|
||
|
<div id="column2" class="interior">
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="footer">
|
||
|
<p>Copyright <a href="http://joyent.com">Joyent, Inc</a>, Node.js
|
||
|
is a <a href="trademark-policy.pdf">trademark of Joyent, Inc</a>.
|
||
|
</div>
|
||
|
|
||
|
<script type="text/javascript">
|
||
|
var gaJsHost = (("https:" == document.location.protocol) ?
|
||
|
"https://ssl." : "http://www.");
|
||
|
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>
|
||
|
<script type="text/javascript">highlight(undefined, undefined, 'pre');</script>
|
||
|
</body>
|
||
|
</html>
|