NODE(1)
=======
Ryan Dahl <ry@tinyclouds.org>
Version, 0.1.1, 2009.07.27
== NAME
node - purely event-based I/O for V8 javascript
== SYNOPSIS
An example of a web server written with Node which responds with "Hello
World" after waiting two seconds:
----------------------------------------
node.http.createServer(function (request, response) {
setTimeout(function () {
response.sendHeader(200, [["Content-Type", "text/plain"]]);
response.sendBody("Hello World");
response.finish();
}, 2000);
}).listen(8000);
puts("Server running at http://127.0.0.1:8000/");
----------------------------------------
To run the server, put the code into a file called +example.js+ and execute
it with the node program
----------------------------------------
> node example.js
Server running at http://127.0.0.1:8000/
----------------------------------------
== DESCRIPTION
Node provides an easy way to build scalable network programs. In the above
example, the 2 second delay does not prevent the server from handling new
requests. Node tells the operating system (through +epoll+, +kqueue+,
+/dev/poll+, or +select+) that it should be notified when the 2 seconds are
up or if a new connection is made--then it goes to sleep. If someone new
connects, then it executes the callback, if the timeout expires, it executes
the inner callback. Each connection is only a small heap allocation.
This is in contrast to today's more common model where OS threads are employed
for concurrency. Thread-based networking
http://www.sics.se/~joe/apachevsyaws.html[is]
http://www.kegel.com/c10k.html[relatively]
http://bulk.fefe.de/scalable-networking.pdf[inefficient]
and very difficult to use. 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. In fact, no function in Node
directly performs I/O. Because nothing blocks, less-than-expert programmers
are able to develop fast systems.
Node is similar in design to systems like Ruby's
http://rubyeventmachine.com/[Event Machine]
or Python's http://twistedmatrix.com/[Twisted].
Node takes the event model a bit further. For example, 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 call like +EventMachine::run()+. In Node it works differently.
By default Node enters the event loop after executing the input script. Node
exits the event loop when there are no more callbacks to perform. Like in
traditional browser javascript, the event loop is hidden from the user.
Node's HTTP API has grown out of my difficulties developing and working with
web servers. For example, streaming data through most web frameworks is
impossible. Or the oft-made false assumption that all message headers have
unique fields. Node attempts to correct these and other problems in its API.
Coupled with Node's purely evented infrastructure, it will make a more
comprehensive foundation for future web libraries/frameworks.
_But what about multiple-processor concurrency? Threads are necessary to scale
programs to multi-core computers._ The name _Node_ should give some hint at how
it is envisioned being used. 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--the rest is message passing. In the
future, I'd like Node to be able to spawn new processes (probably using the
http://www.whatwg.org/specs/web-workers/current-work/[Web Workers API]),
but this is something that fits well into the current design.
== API
Node supports 3 byte-string encodings: ASCII (+"ascii"+), UTF-8 (+"utf8"+),
and raw binary (+"raw"+). It uses strings to represent ASCII and UTF-8
encoded data. For the moment, arrays of integers are used to represent raw
binary data--this representation is rather inefficient. This will
change in the future, when
http://code.google.com/p/v8/issues/detail?id=270[V8 supports Blob objects].
Unless otherwise noted, functions are all asynchronous and do not block
execution.
=== Helpers
+puts(string)+::
Alias for +stdout.puts()+. Outputs the +string+ and a trailing new-line to
+stdout+.
+
Everything in node is asynchronous; +puts()+ is no exception. This might
seem ridiculous but, if for example, one is piping +stdout+ into an NFS
file, +printf()+ will block from network latency. There is an internal
queue for +puts()+ output, so you can be assured that output will be
displayed in the order it was called.
+node.debug(string)+::
A synchronous output function. Will block the process and
output the string immediately to stdout.
+p(object)+ ::
Print the JSON representation of +object+ to the standard output.
+print(string)+::
Like +puts()+ but without the trailing new-line.
+node.exit(code)+::
Immediately ends the process with the specified code.
=== Global Variables
+ARGV+ ::
An array containing the command line arguments.
+stdout+, +stderr+, and +stdin+ ::
Objects of type +node.fs.File+. (See below.)
+__filename+ ::
The filename of the script being executed.
=== Events
Many objects in Node emit events: a TCP server emits an event each time
there is a connection, a child process emits an event when it exits. All
objects which emit events are are instances of +node.EventEmitter+.
Events are represented by a snakecased string. Here are some examples:
+"connection"+, +"receive"+, +"message_begin"+.
Functions can be then be attached to objects, to be executed when an event
is emitted. These functions are called _listeners_.
Some asynchronous file operations return an +EventEmitter+ called a
_promise_. A promise emits just a single event when the operation is
complete.
==== +node.EventEmitter+
+emitter.addListener(event, listener)+ ::
Adds a listener to the end of the listeners array for the specified event.
+
----------------------------------------
server.addListener("connection", function (socket) {
puts("someone connected!");
});
----------------------------------------
+emitter.listeners(event)+ ::
Returns an array of listeners for the specified event. This array can be
manipulated, e.g. to remove listeners.
+emitter.emit(event, args)+ ::
Execute each of the listeners in order with the array +args+ as arguments.
==== +node.Promise+
+node.Promise+ inherits from +node.eventEmitter+. A promise emits one of two
events: +"success"+ or +"error"+. After emitting its event, it will not
emit anymore events.
+promise.addCallback(listener)+ ::
Adds a listener for the +"success"+ event. Returns the same promise object.
+promise.addErrback(listener)+ ::
Adds a listener for the +"error"+ event. Returns the same promise object.
=== Modules
Node has a simple module loading system. In Node, files and modules are in
one-to-one correspondence. As an example, +foo.js+ loads the module
+circle.js+.
The contents of +foo.js+:
----------------------------------------
var circle = require("circle.js");
function onLoad () {
puts("The area of a cirlce of radius 4 is " + circle.area(4));
}
----------------------------------------
The contents of +circle.js+:
----------------------------------------
var PI = 3.14;
exports.area = function (r) {
return PI * r * r;
};
exports.circumference = function (r) {
return 2 * PI * r;
};
----------------------------------------
The module +circle.js+ has exported the functions +area()+ and
+circumference()+. To export an object, add to the special +exports+
object. (Alternatively, one can use +this+ instead of +exports+.) Variables
local to the module will be private. In this example the variable +PI+ is
private to +circle.js+.
The module path is relative to the file calling +require()+. That is,
+circle.js+ must be in the same directory as +foo.js+ for +require
没有合适的资源?快使用搜索试试~ 我知道了~
node-0.1.1.tar.gz
0 下载量 27 浏览量
2024-05-18
23:33:22
上传
评论
收藏 3.23MB GZ 举报
温馨提示
共1142个文件
js:472个
py:202个
cc:172个
Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
资源推荐
资源详情
资源评论
收起资源包目录
node-0.1.1.tar.gz (1142个子文件)
ev.3 207KB
eio.3 154KB
configure.ac 331B
configure.ac 292B
Makefile.am 402B
Makefile.am 273B
annotate 149B
AUTHORS 635B
windows-tick-processor.bat 295B
http_parser.c 120KB
ev.c 76KB
dtoa.c 68KB
eio.c 36KB
evcom.c 24KB
test.c 21KB
test.c 10KB
event.c 9KB
ev_select.c 8KB
ev_epoll.c 7KB
ev_kqueue.c 6KB
ev_win32.c 5KB
ev_port.c 5KB
demo.c 5KB
ev_poll.c 4KB
dtoa-config.c 4KB
echo.c 2KB
codegen-ia32.cc 272KB
objects.cc 256KB
runtime.cc 245KB
codegen-x64.cc 237KB
test-api.cc 223KB
codegen-arm.cc 213KB
test-debug.cc 175KB
jsregexp.cc 154KB
unicode.cc 150KB
parser.cc 147KB
heap.cc 114KB
api.cc 109KB
spaces.cc 81KB
debug.cc 80KB
mark-compact.cc 64KB
bootstrapper.cc 59KB
platform-win32.cc 55KB
simulator-arm.cc 52KB
assembler-ia32.cc 50KB
stub-cache-ia32.cc 50KB
test-regexp.cc 49KB
assembler-x64.cc 49KB
serialize.cc 48KB
ic.cc 46KB
regexp-macro-assembler-ia32.cc 46KB
assembler-arm.cc 45KB
stub-cache-arm.cc 45KB
disasm-x64.cc 38KB
stub-cache.cc 38KB
virtual-frame-ia32.cc 36KB
macro-assembler-ia32.cc 35KB
virtual-frame-x64.cc 35KB
macro-assembler-arm.cc 35KB
disasm-ia32.cc 34KB
log.cc 33KB
ic-ia32.cc 33KB
scopes.cc 32KB
factory.cc 32KB
top.cc 31KB
objects-debug.cc 30KB
macro-assembler-x64.cc 28KB
prettyprinter.cc 27KB
builtins-ia32.cc 26KB
ic-arm.cc 26KB
handles.cc 26KB
disasm-arm.cc 26KB
scanner.cc 24KB
test-heap.cc 24KB
builtins.cc 24KB
builtins-x64.cc 23KB
frames.cc 23KB
d8.cc 22KB
builtins-arm.cc 22KB
d8-posix.cc 22KB
conversions.cc 22KB
interpreter-irregexp.cc 21KB
test-log.cc 21KB
rewriter.cc 21KB
process.cc 20KB
execution.cc 20KB
assembler.cc 20KB
scopeinfo.cc 20KB
platform-linux.cc 19KB
codegen.cc 18KB
accessors.cc 17KB
test-decls.cc 17KB
string-stream.cc 17KB
platform-freebsd.cc 16KB
platform-macos.cc 16KB
test-strings.cc 16KB
net.cc 16KB
flags.cc 16KB
compilation-cache.cc 15KB
compiler.cc 15KB
共 1142 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
程序员Chino的日记
- 粉丝: 2954
- 资源: 4万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功