NODE(1)
=======
Ryan Dahl <ry@tinyclouds.org>
Version, 0.1.12, 2009.09.24
== NAME
node - evented I/O for V8 javascript
== SYNOPSIS
An example of a web server written with Node which responds with "Hello
World":
----------------------------------------
node.http.createServer(function (request, response) {
response.sendHeader(200, {"Content-Type": "text/plain"});
response.sendBody("Hello World\n");
response.finish();
}).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/
----------------------------------------
== API
Node supports 3 string encodings. UTF-8 (+"utf8"+), ASCII (+"ascii"+), and
Binary (+"binary"+). +"ascii"+ and +"binary"+ only look at the first 8 bits
of the 16bit javascript string characters. Both are relatively fast--use
them if you can. +"utf8"+ is slower and should be avoided when possible.
Unless otherwise noted, functions are all asynchronous and do not block
execution.
=== Helpers
+puts(string)+::
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.
+node.exec(command)+::
Executes the command as a child process, buffers the output and returns it
in a promise callback.
+
----------------------------------------
node.exec("ls /").addCallback(function (stdout, stderr) {
puts(stdout);
});
----------------------------------------
+
- on success: stdout buffer, stderr buffer
- on error: exit code, stdout buffer, stderr buffer
+node.cwd()+::
Returns the current working directory of the process.
=== Global Variables
+ARGV+ ::
An array containing the command line arguments.
+ENV+ ::
An object containing the user environment. See environ(7).
+__filename+ ::
The filename of the script being executed.
+process+ ::
A special global object. The +process+ object is like the +window+ object of
browser-side javascript.
=== 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+
All EventEmitters emit the event +"newListener"+ when new listeners are
added.
[cols="1,2,10",options="header"]
|=========================================================
| Event | Parameters | Notes
| +"newListener"+ | +event, listener+| This event is made
any time someone adds
a new listener.
|=========================================================
+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, arg1, arg2, ...)+ ::
Execute each of the listeners in order with the supplied 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.
[cols="1,2,10",options="header"]
|=========================================================
| Event | Parameters | Notes
| +"success"+ | (depends) |
| +"error"+ | (depends) |
|=========================================================
+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.
+promise.emitSuccess(arg1, arg2, ...)+ ::
If you created the promise (by doing +new node.Promise()+) then call
+emitSuccess+ to emit the +"success"+ event with the given arguments.
+
(+promise.emit("success", arg1, arg2, ...)+ should also work, but doesn't at
the moment due to a bug; use +emitSuccess+ instead.)
+promise.emitError(arg1, arg2, ...)+ ::
Emits the +"error"+ event.
+promise.wait()+ ::
Blocks futher execution until the promise emits a success or error event.
Events setup before the call to +promise.wait()+ was made may still be
emitted and executed while +promise.wait()+ is blocking.
+
If there was a single argument to the +"success"+ event then it is returned.
If there were multiple arguments to +"success"+ then they are returned as an
array.
+
If +"error"+ was emitted instead, +wait()+ throws an error.
+
*IMPORTANT* +promise.wait()+ is not a true fiber/coroutine. If any other
promises are created and made to wait while the first promise waits, the
first promise's wait will not return until all others return. The benefit of
this is a simple implementation and the event loop does not get blocked.
Disadvantage is the possibility of situations where the promise stack grows
infinitely large because promises keep getting created and keep being told
to wait(). Use +promise.wait()+ sparingly--probably best used only during
program setup, not during busy server activity.
=== Standard I/O
Standard I/O is handled through a special object +node.stdio+. stdout and
stdin are fully non-blocking (even when piping to files). stderr is
synchronous.
[cols="1,2,10",options="header"]
|=========================================================
| Event | Parameters | Notes
| +"data"+ | +data+ | Made when stdin has received a chunk of data.
Depending on the encoding that stdin was opened
with, +data+ will be a string. This event will
only be emited after +node.stdio.open()+ has
been called.
| +"close"+ | | Made when stdin has been closed.
|=========================================================
+node.stdio.open(encoding="utf8")+::
Open stdin. The program will not exit until +node.stdio.close()+ has been
called or the +"close"+ event has been emitted.
+node.stdio.write(data)+::
Write data to stdout.
+node.stdio.writeError(data)+::
Write data to stderr. Synchronous.
+node.stdio.close()+::
Close stdin.
=== 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");
puts("The area of a circle of radius 4 is " + circle.area(4));
----------------------------------------
The contents of +circle.js+:
----------------------------------------
var PI = 3.14;
e
没有合适的资源?快使用搜索试试~ 我知道了~
node-0.1.12.tar.gz
0 下载量 141 浏览量
2024-05-18
23:34:01
上传
评论
收藏 3.53MB GZ 举报
温馨提示
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.12.tar.gz (1257个子文件)
node.1 40KB
dnsget.1 5KB
rblcheck.1 4KB
ev.3 209KB
eio.3 154KB
udns.3 56KB
configure.ac 331B
configure.ac 292B
Makefile.am 402B
Makefile.am 273B
annotate 149B
AUTHORS 709B
windows-tick-processor.bat 295B
http_parser.c 120KB
ev.c 80KB
dtoa.c 68KB
eio.c 47KB
udns_resolver.c 40KB
evcom.c 36KB
test.c 21KB
dnsget.c 20KB
test.c 20KB
rblcheck.c 10KB
udns_dn.c 10KB
event.c 9KB
ev_select.c 9KB
inet_XtoX.c 8KB
ev_epoll.c 7KB
coupling.c 7KB
udns_init.c 7KB
ev_kqueue.c 6KB
udns_parse.c 5KB
udns_bl.c 5KB
ev_win32.c 5KB
ev_port.c 5KB
demo.c 5KB
udns_rr_srv.c 5KB
ev_poll.c 4KB
udns_rr_naptr.c 4KB
getopt.c 4KB
udns_rr_a.c 4KB
dtoa-config.c 4KB
udns_rr_ptr.c 3KB
ex-rdns.c 3KB
udns_rr_txt.c 3KB
udns_rr_mx.c 3KB
udns_misc.c 2KB
echo.c 2KB
udns_XtoX.c 2KB
test.c 1KB
udns_dntosp.c 1KB
codegen-ia32.cc 275KB
codegen-x64.cc 264KB
objects.cc 263KB
runtime.cc 252KB
test-api.cc 250KB
codegen-arm.cc 209KB
test-debug.cc 177KB
parser.cc 155KB
jsregexp.cc 154KB
unicode.cc 150KB
heap.cc 119KB
api.cc 115KB
debug.cc 86KB
spaces.cc 82KB
macro-assembler-x64.cc 67KB
stub-cache-ia32.cc 65KB
stub-cache-x64.cc 63KB
mark-compact.cc 62KB
bootstrapper.cc 59KB
simulator-arm.cc 56KB
platform-win32.cc 55KB
assembler-x64.cc 53KB
assembler-ia32.cc 51KB
stub-cache-arm.cc 50KB
serialize.cc 49KB
test-regexp.cc 49KB
assembler-arm.cc 47KB
ic.cc 46KB
disasm-x64.cc 45KB
builtins-ia32.cc 43KB
regexp-macro-assembler-x64.cc 42KB
macro-assembler-arm.cc 41KB
stub-cache.cc 41KB
macro-assembler-ia32.cc 41KB
regexp-macro-assembler-arm.cc 40KB
regexp-macro-assembler-ia32.cc 38KB
virtual-frame-ia32.cc 36KB
log.cc 36KB
virtual-frame-x64.cc 35KB
ic-ia32.cc 35KB
disasm-ia32.cc 34KB
ic-x64.cc 33KB
scopes.cc 33KB
factory.cc 32KB
top.cc 32KB
objects-debug.cc 32KB
builtins-arm.cc 29KB
builtins-x64.cc 29KB
prettyprinter.cc 27KB
共 1257 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
程序员Chino的日记
- 粉丝: 3693
- 资源: 5万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功