示例


使用 Node.js 编写 web服务器的示例,响应返回 'Hello World'

本文档中显示的命令以 $> 开头显示,以复制它们在用户终端中的显示方式。 实际使用时不要包含 $> 字符。 它们用于表明每个命令的开始。

还有许多遵循此约定的教程和示例:$> 用于作为常规用户运行的命令, 用于作为管理员执行的命令。

不以 $> 字符开头的代码行通常显示上一个命令的输出。

首先,确保已经下载并安装了 Node.js。 有关更多安装信息,请参阅本指南

现在,创建一个名为 projects 的空项目文件夹,然后导航到它。 可以根据用户当前的项目标题命名项目文件夹,但此示例将使用 projects 作为项目文件夹。

在 Linux 和 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

在 Node.js 中,使用连字符 (-) 或下划线 (_) 分隔文件名中的多个单词被认为是好的风格。

在任何首选文本编辑器中打开 hello-world.js 并粘贴以下内容:

const http = require('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(`服务器运行在 http://${hostname}:${port}/`);
});

保存文件,返回终端窗口输入以下命令:

$ node hello-world.js

终端中会输出以下内容,表明 Node.js 服务器正在运行:

服务器运行在 http://127.0.0.1:3000/

现在,打开任何首选的 Web 浏览器并访问 http://127.0.0.1:3000

如果浏览器显示字符串 Hello,World!,则表明服务器正在运行。

文档中的许多示例都可以类似地运行。

An example of a web server written with Node.js which responds with 'Hello, World!':

Commands displayed in this document are shown starting with $ or > to replicate how they would appear in a user's terminal. Do not include the $ and > characters. They are there to indicate the start of each command.

There are many tutorials and examples that follow this convention: $ or > for commands run as a regular user, and # for commands that should be executed as an administrator.

Lines that don’t start with $ or > character are typically showing the output of the previous command.

Firstly, make sure to have downloaded and installed Node.js. See this guide for further install information.

Now, create an empty project folder called projects, then navigate into it. The project folder can be named based on the user's current project title, but this example will use projects as the project folder.

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

Next, create a new source file in the projects folder and call it hello-world.js.

In Node.js it is considered good style to use hyphens (-) or underscores (_) to separate multiple words in filenames.

Open hello-world.js in any preferred text editor and paste in the following content:

const http = require('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}/`);
});

Save the file, go back to the terminal window enter the following command:

$ node hello-world.js

An output like this should appear in the terminal to indicate Node.js server is running:

Server running at http://127.0.0.1:3000/

Now, open any preferred web browser and visit http://127.0.0.1:3000.

If the browser displays the string Hello, World!, that indicates the server is working.

Many of the examples in the documentation can be run similarly.