关于 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 { } = ('node:http');
const = '127.0.0.1';
const = 3000;
const = ((, ) => {
. = 200;
.('Content-Type', 'text/plain');
.('Hello World');
});
.(, , () => {
.(`Server running at http://${}:${}/`);
});
这与如今更常见的并发模型形成对比,在那种模型中使用的是操作系统线程。基于线程的网络处理相对低效且使用起来非常困难。此外,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 非常适合作为网络库或框架的基础。
🌐 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:
- nodejs.org
- nodejs.dev (重定向到 https://nodejs.cn)
- iojs.org (重定向到 https://nodejs.cn)
官方 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.