nodejs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNode.js
Node.js
Node.js is a cross-platform JavaScript runtime environment. Node.js 22 (LTS 2025) brings native TypeScript support (experimental), a built-in Test Runner, and a native WebSocket client.
Node.js 是一个跨平台的JavaScript运行时环境。Node.js 22(2025年LTS版本)带来了原生TypeScript支持(实验性)、内置测试运行器(Test Runner)以及原生WebSocket客户端。
When to Use
适用场景
- Real-time Apps: Chat, Gaming, Collaboration (WebSockets).
- API Servers: High concurrency with non-blocking I/O.
- Tooling: The foundation of modern frontend toolchains (Vite, Next.js).
- 实时应用:聊天、游戏、协作类应用(基于WebSockets)。
- API服务器:借助非阻塞I/O实现高并发。
- 工具链:现代前端工具链的基础(如Vite、Next.js)。
Quick Start (Native Features)
快速上手(原生特性)
javascript
// Native Test Runner (No Jest needed)
import { test, assert } from "node:test";
test("synchronous passing test", (t) => {
assert.strictEqual(1, 1);
});
// Native WebSocket
const ws = new WebSocket("ws://example.com/socket");
ws.onopen = () => console.log("Connected");javascript
// Native Test Runner (No Jest needed)
import { test, assert } from "node:test";
test("synchronous passing test", (t) => {
assert.strictEqual(1, 1);
});
// Native WebSocket
const ws = new WebSocket("ws://example.com/socket");
ws.onopen = () => console.log("Connected");Core Concepts
核心概念
Event Loop
事件循环(Event Loop)
Single-threaded, non-blocking I/O. Heavy CPU tasks block the loop (bad), but I/O tasks are offloaded to OS (good).
单线程、非阻塞I/O模型。CPU密集型任务会阻塞事件循环(不推荐),而I/O任务会被卸载到操作系统处理(推荐)。
Streams
流(Streams)
Process huge files piece-by-piece without loading them into memory.
无需将大文件全部加载到内存,即可逐段处理大文件。
Native APIs (2025)
2025年原生API
Node now has , , , and support built-in. You need fewer dependencies than in 2020.
fetchtestwatch.envNode.js 现在内置了、、和支持,相比2020年,你需要的依赖更少了。
fetchtestwatch.envBest Practices (2025)
2025年最佳实践
Do:
- Use : Drop Jest/Mocha for simple projects.
node:test - Use : Drop
node --env-filedependency.dotenv - Use Async/Await: Callbacks are dead. Long live Promises.
Don't:
- Don't block the Event Loop: Don't run Crypto or Image processing on the main thread. Use Worker Threads.
- Don't use : New projects should use ESM (
require).import
推荐做法:
- 使用:简单项目可弃用Jest/Mocha。
node:test - 使用:弃用
node --env-file依赖。dotenv - 使用Async/Await:回调函数已过时,Promise才是主流。
不推荐做法:
- 不要阻塞事件循环:不要在主线程运行加密或图像处理任务,应使用Worker Threads。
- 不要使用:新项目应使用ESM(
require)。import