- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- module/typescript TS 模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
Node.js v24.13.0 文档
- Node.js v24.13.0
- 目录
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- module/typescript TS 模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- 其他版本
用法和示例#>
【Usage and example】
使用方法#>
【Usage】
node [options] [V8 options] [script.js | -e "script" | - ] [arguments]
请参阅命令行选项文件以获取更多信息。
【Please see the Command-line options document for more information.】
示例#>
【Example】
使用 Node.js 编写的 网络服务器 示例,它会响应 'Hello, World!' :
【An example of a web server written with Node.js which responds with
'Hello, World!':】
本文档中的命令以 $ 或 > 开头,以模拟它们在用户终端中出现的方式。不要包含 $ 和 > 字符。它们只是用来显示每个命令的开始。
【Commands in this document start with $ or > to replicate how they would
appear in a user's terminal. Do not include the $ and > characters. They are
there to show the start of each command.】
不以 $ 或 > 字符开头的行显示前一个命令的输出。
【Lines that don't start with $ or > character show the output of the previous
command.】
首先,确保已下载并安装 Node.js。有关更多安装信息,请参见 通过包管理器安装 Node.js。
【First, make sure to have downloaded and installed Node.js. See Installing Node.js via package manager for further install information.】
现在,创建一个名为 projects 的空项目文件夹,然后进入该文件夹。
【Now, create an empty project folder called projects, then navigate into it.】
Linux 和 Mac:
【Linux and Mac:】
mkdir ~/projects
cd ~/projects
Windows CMD:
mkdir %USERPROFILE%\projects
cd %USERPROFILE%\projects
Windows PowerShell:
mkdir $env:USERPROFILE\projects
cd $env:USERPROFILE\projects
接下来,在 projects 文件夹中创建一个新的源文件,并将其命名为 hello-world.js。
【Next, create a new source file in the projects
folder and call it hello-world.js.】
在你喜欢的任何文本编辑器中打开 hello-world.js,并粘贴以下内容:
【Open hello-world.js in any preferred text editor and
paste in the following content:】
const http = require('node:http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
保存文件。然后,在终端窗口中,要运行 hello-world.js 文件,输入:
【Save the file. Then, in the terminal window, to run the hello-world.js file,
enter:】
node hello-world.js
这样的输出应该出现在终端中:
【Output like this should appear in the terminal:】
Server running at http://127.0.0.1:3000/
现在,打开任意你喜欢的网页浏览器,然后访问 http://127.0.0.1:3000。
【Now, open any preferred web browser and visit http://127.0.0.1:3000.】
如果浏览器显示字符串 Hello, World!,那就说明服务器正在正常工作。
【If the browser displays the string Hello, World!, that indicates
the server is working.】