概述

使用 Node.js 创建 web 服务器:

  1. const http = require('http')
  2. http.createServer( (req, res) => {
  3. res.writeHead(200, {
  4. 'Content-Type': 'text/plain'
  5. })
  6. res.end('Hello World\n')
  7. }).listen(8124)
  8. console.log('Server running at http://localhost:8124/')

将上述代码保存到 example.js 文件中,然后在终端中使用 node 命令解析执行:

  1. $ node example.js
  2. Server running at http://localhost:8124/

文档中的所有示例都可以使用这种方法进行测试。