关于 Node.js ®
¥About Node.js®
作为异步事件驱动的 JavaScript 运行时,Node.js 旨在构建可扩展的网络应用。在下面的 "hello world" 示例中,可以同时处理许多连接。每次连接时,都会触发回调,但如果没有工作要做,Node.js 就会进入休眠状态。
¥As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications. In the following "hello world" example, many connections can be handled concurrently. Upon each connection, the callback is fired, but if there is no work to be done, Node.js will sleep.
const { createServer } = require('node:http');
const hostname = '127.0.0.1';
const port = 3000;
const server = createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
这与当今更常见的并发模型形成对比,在该模型中采用 OS 线程。基于线程的网络效率相对较低,并且很难使用。此外,由于没有锁,Node.js 的用户无需担心进程死锁。Node.js 中几乎没有函数直接执行 I/O,因此除非使用 Node.js 标准库的同步方法执行 I/O,否则进程绝不会阻塞。由于没有阻塞,因此在 Node.js 中开发可扩展系统非常合理。
¥This is in contrast to today's more common concurrency model, in which OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use. Furthermore, users of Node.js are free from worries of dead-locking the process, since there are no locks. Almost no function in Node.js directly performs I/O, so the process never blocks except when the I/O is performed using synchronous methods of Node.js standard library. Because nothing blocks, scalable systems are very reasonable to develop in Node.js.
如果你不熟悉该语言的某些部分,可以阅读有关 阻塞与非阻塞 的完整文章。
¥If some of this language is unfamiliar, there is a full article on Blocking vs. Non-Blocking.
Node.js 在设计上与 Ruby 的 Event Machine 和 Python 的 Twisted 等系统相似,并受其影响。Node.js 将事件模型更进一步。它将事件循环作为运行时构造而不是库来渲染。在其他系统中,始终有一个阻塞调用来启动事件循环。通常,行为是通过脚本开头的回调来定义的,最后通过 EventMachine::run()
之类的阻塞调用启动服务器。在 Node.js 中,没有这样的启动事件循环调用。Node.js 在执行输入脚本后直接进入事件循环。当没有更多回调要执行时,Node.js 会退出事件循环。此行为类似于浏览器 JavaScript — 事件循环对用户是隐藏的。
¥Node.js is similar in design to, and influenced by, systems like Ruby's
Event Machine and Python's Twisted. Node.js takes the event model a bit
further. It presents an event loop as a runtime construct instead of as a library. In other systems,
there is always a blocking call to start the event-loop.
Typically, behavior is defined through callbacks at the beginning of a script, and
at the end a server is started through a blocking call like EventMachine::run()
.
In Node.js, there is no such start-the-event-loop call. Node.js simply enters the event loop after executing the input script. Node.js
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.
HTTP 是 Node.js 中的一等公民,设计时考虑到了流式传输和低延迟。这使得 Node.js 非常适合作为 Web 库或框架的基础。
¥HTTP is a first-class citizen in Node.js, designed with streaming and low latency in mind. This makes Node.js well suited for the foundation of a web library or framework.
Node.js 没有线程设计并不意味着你无法利用环境中的多个核心。可以使用我们的 child_process.fork()
API 生成子进程,并且这些子进程易于通信。基于同一接口构建的是 cluster
模块,它允许你在进程之间共享套接字,从而实现内核上的负载平衡。
¥Node.js being designed without threads doesn't mean you can't take
advantage of multiple cores in your environment. Child processes can be spawned
by using our child_process.fork()
API, and are designed to be easy to
communicate with. Built upon that same interface is the cluster
module,
which allows you to share sockets between processes to enable load balancing
over your cores.