关于 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.

官方 Node.js 资源

¥Official Node.js Resources

为确保使用 Node.js 时的真实性和安全性,请始终使用官方来源。避免信任来自非官方来源的电子邮件、二进制文件或下载内容。

¥To ensure authenticity and security when working with Node.js, always use official sources. Avoid trusting emails, binaries, or downloads from unofficial sources.

官方 Node.js 域名

¥Official Node.js Domains

要下载 Node.js 二进制文件并访问官方文档,请仅使用以下域名:

¥For downloading Node.js binaries and accessing official documentation, use only these domains:

官方 npm 软件包

¥Official npm Packages

Node.js 团队维护以下官方 npm 软件包范围:

¥The Node.js team maintains the following official npm package scopes:

此外,Node.js 团队维护由 nodejs-foundation npm 账户发布的软件包,但其他与 Node.js 相关的软件包(如 undici)也可能由与项目密切相关的贡献者维护。

¥Additionally, the Node.js team maintains packages published by the nodejs-foundation npm account, though other Node.js-related packages (like undici) may also be maintained by contributors closely tied to the project.

使用 Node.js 团队提供的软件包可确保你使用的是官方支持的 Node.js 组件。

¥Using packages from the Node.js team guarantees that you are working with officially supported Node.js components.

官方 GitHub 组织

¥Official GitHub Organizations

Node.js 及相关项目由以下官方 GitHub 组织维护:

¥Node.js and related projects are maintained under these official GitHub organizations:

官方沟通渠道

¥Official Communication Channels

Node.js 和 OpenJS 基金会通过各种官方和社区支持的渠道进行沟通。你可以在 参与其中 页面上找到有关如何参与的详细信息。

¥Node.js and the OpenJS Foundation communicate through various official and community-supported channels. You can find details on how to get involved on the Get Involved page.

报告网站问题和宕机

¥Reporting Website Issues & Downtime

如果你在使用 Node.js 网站时遇到问题,请在 Node.js 网站仓库 上报告。如需获取中断的实时更新,请访问 Node.js 状态页面

¥If you encounter issues with the Node.js website, report them at the Node.js website repository. For real-time updates on outages, visit the Node.js Status Page.