开始之前,需要:
- 正确安装和配置 VS Code (以下简称 code)
- 正确安装和配置 node.js 16
打开 PowerShell ,(创建并)进入工作目录。
命令行或者手动打开 code 并编写 hello-world.js 文件。
code hello-world.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(`Server running at http://${hostname}:${port}/`);});
CTRL + S 保存文件。
回到 PowerShell , 输入 node hello-world.js 并回车(此时工作目录应当与之前一致)。
应当看到如下图所示的内容:
现在,打开任何首选的网络浏览器并访问 http://127.0.0.1:3000。
